add elevatppy lint:js:fix

This commit is contained in:
Miko 2025-01-17 00:40:00 +01:00
parent f55a4791c2
commit a6123d1b6f
4 changed files with 44 additions and 27 deletions

View file

@ -249,9 +249,20 @@ export const serve_data = {
if (fetchTile == null) return res.status(204).send(); if (fetchTile == null) return res.status(204).send();
let data = fetchTile.data; let data = fetchTile.data;
var param = { long: bbox[0], lat: bbox[1], encoding, format, tile_size: TILE_SIZE, z: zoom, x: xy[0], y: xy[1] }; var param = {
long: bbox[0],
lat: bbox[1],
encoding,
format,
tile_size: TILE_SIZE,
z: zoom,
x: xy[0],
y: xy[1],
};
res.status(200).send(await serve_rendered.getTerrainElevation(data, param)); res
.status(200)
.send(await serve_rendered.getTerrainElevation(data, param));
} catch (err) { } catch (err) {
return res return res
.status(500) .status(500)

View file

@ -6,5 +6,8 @@ export const serve_rendered = {
init: (options, repo, programOpts) => {}, init: (options, repo, programOpts) => {},
add: (options, repo, params, id, programOpts, dataResolver) => {}, add: (options, repo, params, id, programOpts, dataResolver) => {},
remove: (repo, id) => {}, remove: (repo, id) => {},
getTerrainElevation: (data, param) => { param["elevation"] = "not supported in light"; return param; }, getTerrainElevation: (data, param) => {
param['elevation'] = 'not supported in light';
return param;
},
}; };

View file

@ -1459,43 +1459,43 @@ export const serve_rendered = {
delete repo[id]; delete repo[id];
}, },
/** /**
* Get the elevation of terrain tile data by rendering it to a canvas image * Get the elevation of terrain tile data by rendering it to a canvas image
* @param {object} data `The background color (or empty string for transparent). * @param {object} data The background color (or empty string for transparent).
* @param {object} format A sharp format. * @param {object} param Required parameters (coordinates e.g.)
* @returns {object} * @returns {object}
*/ */
getTerrainElevation: async function(data, param) { getTerrainElevation: async function (data, param) {
return await new Promise(async (resolve, reject) => { return await new Promise(async (resolve, reject) => {
const image = new Image(); const image = new Image();
image.onload = async () => { image.onload = async () => {
const canvas = createCanvas(param["tile_size"], param["tile_size"]); const canvas = createCanvas(param['tile_size'], param['tile_size']);
const context = canvas.getContext('2d'); const context = canvas.getContext('2d');
context.drawImage(image, 0, 0); context.drawImage(image, 0, 0);
// calculate pixel coordinate of tile, // calculate pixel coordinate of tile,
// see https://developers.google.com/maps/documentation/javascript/examples/map-coordinates // see https://developers.google.com/maps/documentation/javascript/examples/map-coordinates
let siny = Math.sin((param["lat"] * Math.PI) / 180); let siny = Math.sin((param['lat'] * Math.PI) / 180);
// Truncating to 0.9999 effectively limits latitude to 89.189. This is // Truncating to 0.9999 effectively limits latitude to 89.189. This is
// about a third of a tile past the edge of the world tile. // about a third of a tile past the edge of the world tile.
siny = Math.min(Math.max(siny, -0.9999), 0.9999); siny = Math.min(Math.max(siny, -0.9999), 0.9999);
const xWorld = param["tile_size"] * (0.5 + param["long"] / 360); const xWorld = param['tile_size'] * (0.5 + param['long'] / 360);
const yWorld = const yWorld =
param["tile_size"] * param['tile_size'] *
(0.5 - Math.log((1 + siny) / (1 - siny)) / (4 * Math.PI)); (0.5 - Math.log((1 + siny) / (1 - siny)) / (4 * Math.PI));
const scale = 1 << param["z"]; const scale = 1 << param['z'];
const xTile = Math.floor((xWorld * scale) / param["tile_size"]); const xTile = Math.floor((xWorld * scale) / param['tile_size']);
const yTile = Math.floor((yWorld * scale) / param["tile_size"]); const yTile = Math.floor((yWorld * scale) / param['tile_size']);
const xPixel = Math.floor(xWorld * scale) - xTile * param["tile_size"]; const xPixel = Math.floor(xWorld * scale) - xTile * param['tile_size'];
const yPixel = Math.floor(yWorld * scale) - yTile * param["tile_size"]; const yPixel = Math.floor(yWorld * scale) - yTile * param['tile_size'];
if ( if (
xPixel < 0 || xPixel < 0 ||
yPixel < 0 || yPixel < 0 ||
xPixel >= param["tile_size"] || xPixel >= param['tile_size'] ||
yPixel >= param["tile_size"] yPixel >= param['tile_size']
) { ) {
return reject('Out of bounds Pixel'); return reject('Out of bounds Pixel');
} }
@ -1504,21 +1504,21 @@ export const serve_rendered = {
const green = imgdata.data[1]; const green = imgdata.data[1];
const blue = imgdata.data[2]; const blue = imgdata.data[2];
let elevation; let elevation;
if (param["encoding"] === 'mapbox') { if (param['encoding'] === 'mapbox') {
elevation = -10000 + (red * 256 * 256 + green * 256 + blue) * 0.1; elevation = -10000 + (red * 256 * 256 + green * 256 + blue) * 0.1;
} else if (param["encoding"] === 'terrarium') { } else if (param['encoding'] === 'terrarium') {
elevation = red * 256 + green + blue / 256 - 32768; elevation = red * 256 + green + blue / 256 - 32768;
} else { } else {
elevation = 'invalid encoding'; elevation = 'invalid encoding';
} }
param["elevation"] = elevation; param['elevation'] = elevation;
param["red"] = red; param['red'] = red;
param["green"] = green; param['green'] = green;
param["blue"] = blue; param['blue'] = blue;
resolve(param); resolve(param);
}; };
image.onerror = (err) => reject(err); image.onerror = (err) => reject(err);
if (param["format"] === 'webp') { if (param['format'] === 'webp') {
try { try {
const img = await sharp(data).toFormat('png').toBuffer(); const img = await sharp(data).toFormat('png').toBuffer();
image.src = img; image.src = img;

View file

@ -14,6 +14,9 @@ export const allowedTileSizes = allowedOptions(['256', '512']);
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
/**
*
*/
export function isLight() { export function isLight() {
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);