UNPKG

s2-tools

Version:

A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.

52 lines 1.74 kB
import { fromST, toLonLat } from './point'; /** * 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 [lon, lat] = toLonLat(fromST(face, s, t)); point.x = lon; point.y = lat; } //# sourceMappingURL=convert.js.map