feat: enable encodedpath in query to load encoded polyline
Signed-off-by: Vinayak Kulkarni <19776877+vinayakkulkarni@users.noreply.github.com>
This commit is contained in:
parent
96a3614fd6
commit
a1f60e16b5
1 changed files with 43 additions and 30 deletions
|
|
@ -15,6 +15,7 @@ import sanitize from 'sanitize-filename';
|
||||||
import SphericalMercator from '@mapbox/sphericalmercator';
|
import SphericalMercator from '@mapbox/sphericalmercator';
|
||||||
import mlgl from '@maplibre/maplibre-gl-native';
|
import mlgl from '@maplibre/maplibre-gl-native';
|
||||||
import MBTiles from '@mapbox/mbtiles';
|
import MBTiles from '@mapbox/mbtiles';
|
||||||
|
import polyline from '@mapbox/polyline';
|
||||||
import proj4 from 'proj4';
|
import proj4 from 'proj4';
|
||||||
import request from 'request';
|
import request from 'request';
|
||||||
import { getFontsPbf, getTileUrls, fixTileJSONCenter } from './utils.js';
|
import { getFontsPbf, getTileUrls, fixTileJSONCenter } from './utils.js';
|
||||||
|
|
@ -147,47 +148,59 @@ const parseCoordinates = (coordinatePair, query, transformer) => {
|
||||||
* @param {Function} transformer Optional transform function.
|
* @param {Function} transformer Optional transform function.
|
||||||
*/
|
*/
|
||||||
const extractPathsFromQuery = (query, transformer) => {
|
const extractPathsFromQuery = (query, transformer) => {
|
||||||
// Return an empty list if no paths have been provided
|
|
||||||
if (!query.path) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const paths = [];
|
const paths = [];
|
||||||
|
// Return an empty list if no paths have been provided
|
||||||
|
if (
|
||||||
|
('path' in query && !query.path) ||
|
||||||
|
('encodedpath' in query && !query.encodedpath)
|
||||||
|
) {
|
||||||
|
return paths;
|
||||||
|
}
|
||||||
|
// Parse paths provided via path query argument
|
||||||
|
if ('path' in query) {
|
||||||
|
// Check if multiple paths have been provided and mimic a list if it's a
|
||||||
|
// single path.
|
||||||
|
const providedPaths = Array.isArray(query.path) ? query.path : [query.path];
|
||||||
|
|
||||||
// Check if multiple paths have been provided and mimic a list if it's a
|
// Iterate through paths, parse and validate them
|
||||||
// single path.
|
for (const providedPath of providedPaths) {
|
||||||
const providedPaths = Array.isArray(query.path) ? query.path : [query.path];
|
const currentPath = [];
|
||||||
|
|
||||||
// Iterate through paths, parse and validate them
|
// Extract coordinate-list from path
|
||||||
for (const provided_path of providedPaths) {
|
const pathParts = (providedPath || '').split('|');
|
||||||
const currentPath = [];
|
|
||||||
|
|
||||||
// Extract coordinate-list from path
|
// Iterate through coordinate-list, parse the coordinates and validate them
|
||||||
const pathParts = (provided_path || '').split('|');
|
for (const pair of pathParts) {
|
||||||
|
// Extract coordinates from coordinate pair
|
||||||
|
const pairParts = pair.split(',');
|
||||||
|
|
||||||
// Iterate through coordinate-list, parse the coordinates and validate them
|
// Ensure we have two coordinates
|
||||||
for (const pair of pathParts) {
|
if (pairParts.length === 2) {
|
||||||
// Extract coordinates from coordinate pair
|
const pair = parseCoordinates(pairParts, query, transformer);
|
||||||
const pairParts = pair.split(',');
|
|
||||||
|
|
||||||
// Ensure we have two coordinates
|
// Ensure coordinates could be parsed and skip them if not
|
||||||
if (pairParts.length === 2) {
|
if (pair === null) {
|
||||||
const pair = parseCoordinates(pairParts, query, transformer);
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure coordinates could be parsed and skip them if not
|
// Add the coordinate-pair to the current path if they are valid
|
||||||
if (pair === null) {
|
currentPath.push(pair);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add the coordinate-pair to the current path if they are valid
|
// Extend list of paths with current path if it contains coordinates
|
||||||
currentPath.push(pair);
|
if (currentPath.length) {
|
||||||
|
paths.push(currentPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Extend list of paths with current path if it contains coordinates
|
// Check if encoded paths have been provided
|
||||||
if (currentPath.length) {
|
if ('encodedpath' in query) {
|
||||||
paths.push(currentPath);
|
// silly lat/lng switch 🤷♂️
|
||||||
}
|
const coords = polyline
|
||||||
|
.decode(query.encodedpath)
|
||||||
|
.map(([lat, lng]) => [lng, lat]);
|
||||||
|
paths.push(coords);
|
||||||
}
|
}
|
||||||
return paths;
|
return paths;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue