-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow replacing fetch tile and decode image for local Dem manager #359
Conversation
CC: @acalcutt |
The following script seems to work as expected I think, I tried to use the exported parts of this library import { writeFileSync } from "fs";
import type { DemTile, Encoding } from "./dist/types";
import {default as mlcontour} from "./dist/index.mjs";
import { PNG } from "pngjs";
async function decodeImageNode(
blob: Blob,
encoding: Encoding,
abortController: AbortController,
): Promise<DemTile> {
const buffer = await blob.arrayBuffer();
const png = PNG.sync.read(Buffer.from(buffer));
const parsed = mlcontour.decodeParsedImage(png.width, png.height, encoding, png.data as any as Uint8ClampedArray);
if (Boolean(abortController?.signal?.aborted)) return null as any as DemTile;
return parsed;
}
let manager = new mlcontour.LocalDemManager({
demUrlPattern: "https://www.example.com/{z}/{x}/{y}.png", // This is the URL of the DEM tiles
cacheSize: 100,
encoding: "mapbox",
maxzoom: 12,
timeoutMs: 10000,
decodeImage: decodeImageNode,
});
manager.fetchContourTile(12,2446,1655, {levels: [10]}, new AbortController()).then((tile) => {
writeFileSync("12-2446-1655.mvt", Buffer.from(tile.arrayBuffer));
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall! A couple of small suggestions, but can you also run npm run format
and add a test with a local DEM manager with custom function for fetch and resolve?
I've done the formatting and added a e2e test, although it might be better to move it to "local-dem-manager.test.ts" as a unit test for that class, not sure, let me know what you prefer... |
Thank you! Do you think it would make sense to upstream a top-level node.js-ready entrypoint like #359 (comment) ? Or would it be easier to just to keep it in the other repo? |
I think @acalcutt is working on a script that can allow running this using a shell script to be added to the bin folder of this package. |
I made this code to get all the tiles under a specific parent tile which is based off similar code to the first post that @HarelM had given me and this batch script the gets all tiles at a specific level and runs the above script for each in parallel
I used it to make this z5-z11 contour at 10m It shouldn't be hard to move that to this new method of fetch tile method I wouldn't think. |
Would it be possible to override 'fetchTile' in the same way as decodeImage? |
It is part of this PR, yes. |
ah, nice. i think i see in the tests how it would be used |
I was trying to use your maplibre-contour changes with this code,
but when i look at those index files the do seem like they have a default export... I did try to delete dist and recreated but it didn't help The other thing is I noticed $url only seems to be the only parameter for getTile,. It would be nice if zxy parameters where given to so it doesn't need to be split out of the url |
Did you make sure to rebuild dist folder? I'll take a look later tonight to see if I can make it work. |
I've updated the above comment to something that is working for me, I'm not sure what's not well defined in terms of typescript, but it can run using tsx, vscode doesn't like what I wrote though... |
Nice, I was able to get my local pmtiles script to work with this change I modified the code I posted above that builds all tiles under a parent tile to work with this new feature I also put the node/pmtiles functions I had made into it's own file that can be referenced In 'generate-countour-tile-pyramid.ts' I added a switch if demUrl starts with pmtiles:// so it should be able to either work with pmtiles or a regular http(s) url https://github.com/WifiDB/maplibre-contour/blob/fetch_test/scripts/generate-countour-tile-pyramid.ts#L218-L240, it should start with something like this for pmtiles or for a tile url |
OK I just published a 0.1.0 release: https://www.npmjs.com/package/maplibre-contour/v/0.1.0 |
Thanks @msbarry! I was able to create a new project (external to maplibre-contour) and use the following code to generate a tile locally. import { writeFileSync } from "fs";
import mlcontour from "maplibre-contour";
import { DemTile, Encoding } from "maplibre-contour/dist/types";
import { PNG } from "pngjs";
async function decodeImageNode(
blob: Blob,
encoding: Encoding,
abortController: AbortController,
): Promise<DemTile> {
const buffer = await blob.arrayBuffer();
const png = PNG.sync.read(Buffer.from(buffer));
const parsed = mlcontour.decodeParsedImage(png.width, png.height, encoding, png.data as any as Uint8ClampedArray);
if (Boolean(abortController?.signal?.aborted)) return null as unknown as DemTile;
return parsed;
}
let manager = new mlcontour.LocalDemManager({
demUrlPattern: "https://www.expample.com/{z}/{x}/{y}.png", // This is the URL of the DEM tiles
cacheSize: 100,
encoding: "mapbox",
maxzoom: 12,
timeoutMs: 10000,
decodeImage: decodeImageNode,
});
manager.fetchContourTile(12,2446,1655, {levels: [10]}, new AbortController()).then((tile) => {
writeFileSync("12-2446-1655.mvt", Buffer.from(tile.arrayBuffer));
}); |
I've added the ability to replace decode image and fetch tile.
I've also moved some stuff around and did some minor renaming.
Let me know what you think.
This can facilitate for #212 for the case of local dem manager, but not for remote dem manager.