s2maps-gpu
Version:
S2 Maps GPU - An open source, high-performance, and GPU-accelerated map engine for rendering large-scale, interactive maps.
52 lines (51 loc) • 1.75 kB
JavaScript
import { pointFromST as fromST, pointToLonLat as toLonLat } from './point.js';
/**
* Convert an S2Feature to a GeoJSON Feature
* @param data - S2Feature
* @returns - GeoJSON Feature
*/
export function toWM(data) {
const { id, face, properties, metadata, geometry } = data;
convertGeometry(face, geometry);
return {
id,
type: 'VectorFeature',
properties,
metadata,
geometry,
};
}
/**
* Underlying conversion mechanic to move S2Geometry to GeoJSON Geometry
* @param face - Face
* @param geometry - S2 Geometry
*/
function convertGeometry(face, geometry) {
const { type, coordinates } = geometry;
if (type === 'Point')
convertGeometryPoint(face, coordinates);
else if (type === 'MultiPoint')
coordinates.forEach((point) => convertGeometryPoint(face, point));
else if (type === 'LineString')
coordinates.forEach((point) => convertGeometryPoint(face, point));
else if (type === 'MultiLineString')
coordinates.forEach((line) => line.forEach((point) => convertGeometryPoint(face, point)));
else if (type === 'Polygon')
coordinates.forEach((line) => line.forEach((point) => convertGeometryPoint(face, point)));
else if (type === 'MultiPolygon')
coordinates.forEach((polygon) => polygon.forEach((line) => line.forEach((point) => convertGeometryPoint(face, point))));
else {
throw new Error('Invalid S2Geometry type');
}
}
/**
* Mutate an S2 Point to a GeoJSON Point
* @param face - Face
* @param point - S2 Point
*/
function convertGeometryPoint(face, point) {
const { x: s, y: t } = point;
const { x: lon, y: lat } = toLonLat(fromST(face, s, t));
point.x = lon;
point.y = lat;
}