Skip to content

Commit

Permalink
Adding support for async functions as layers
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Fish <[email protected]>
  • Loading branch information
fishcharlie committed Aug 25, 2024
1 parent de4a7db commit 90709ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const fs = require("fs");
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
// https://mesonet.agron.iastate.edu/GIS/ridge.phtml
"https://mesonet.agron.iastate.edu/cache/tile.py/1.0.0/ridge::USCOMP-N0Q-0/{z}/{x}/{y}.png",
async (z, x, y) => {
const buffer = await fs.promises.readFile("tile.png");
return buffer;
}
]
}
});
Expand Down
31 changes: 20 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface MapToImageSettings {
*
* @example ["https://tile.openstreetmap.org/{z}/{x}/{y}.png"]
*/
"layers": (string | {"url": string, "opacity"?: number})[]
"layers": (string | {"url": string, "opacity"?: number} | ((z: number, x: number, y: number) => Buffer | Promise<Buffer>))[]
}
}

Expand Down Expand Up @@ -110,7 +110,7 @@ export async function mapToImage(settings: MapToImageSettings) {
"y": settings.image.dimensions.height / 2
};

let images: { input: string, left: number, top: number, opacity: number }[] = [];
let images: { input: string | (() => Buffer | Promise<Buffer>), left: number, top: number, opacity: number }[] = [];

for (const layer of settings.map.layers) {
const tile = coordinatesToTile(settings.map.center.lat, settings.map.center.lng, settings.map.zoom);
Expand All @@ -119,15 +119,24 @@ export async function mapToImage(settings: MapToImageSettings) {
"y": (tile.y - Math.floor(tile.y)) * 256
};

const layerURL = typeof layer === "string" ? layer : layer.url;
const layerOpacity = typeof layer === "string" ? 1 : (layer.opacity ?? 1);
function createImageObject(x: number, y: number, zoom: number, left: number, top: number) {
return {
"input": downloadTileURL(layerURL, Math.floor(x), Math.floor(y), zoom),
"left": left,
"top": top,
"opacity": layerOpacity,
};
if (typeof layer === "function") {
return {
"input": (): Buffer | Promise<Buffer> => layer(zoom, x, y),
"left": left,
"top": top,
"opacity": 1
}
} else {
const layerURL = typeof layer === "string" ? layer : layer.url;
const layerOpacity = typeof layer === "string" ? 1 : (layer.opacity ?? 1);
return {
"input": downloadTileURL(layerURL, Math.floor(x), Math.floor(y), zoom),
"left": left,
"top": top,
"opacity": layerOpacity,
};
}
}

const img = createImageObject(tile.x, tile.y, settings.map.zoom, Math.round((imageCenter.x - (256 / 2))) + Math.round((256 / 2) - offsetOfCoordinates.x), Math.round((imageCenter.y - (256 / 2))) + Math.round((256 / 2) - offsetOfCoordinates.y));
Expand Down Expand Up @@ -178,7 +187,7 @@ export async function mapToImage(settings: MapToImageSettings) {
}).map(async (img) => {
return {
...img,
"input": await downloadTile(img.input, img.opacity)
"input": typeof img.input === "string" ? await downloadTile(img.input, img.opacity) : await img.input()
}
})));

Expand Down

0 comments on commit 90709ae

Please sign in to comment.