@loaders.gl/wkt
Version:
Loader and Writer for the WKT (Well Known Text) Format
48 lines (47 loc) • 1.64 kB
JavaScript
// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
// Fork of https://github.com/mapbox/wellknown under ISC license (MIT/BSD-2-clause equivalent)
/**
* Stringifies a GeoJSON object into WKT
* @param geojson
* @returns string
*/
export function encodeWKT(geometry) {
if (geometry.type === 'Feature') {
geometry = geometry.geometry;
}
switch (geometry.type) {
case 'Point':
return `POINT ${wrapParens(pairWKT(geometry.coordinates))}`;
case 'LineString':
return `LINESTRING ${wrapParens(ringWKT(geometry.coordinates))}`;
case 'Polygon':
return `POLYGON ${wrapParens(ringsWKT(geometry.coordinates))}`;
case 'MultiPoint':
return `MULTIPOINT ${wrapParens(ringWKT(geometry.coordinates))}`;
case 'MultiPolygon':
return `MULTIPOLYGON ${wrapParens(multiRingsWKT(geometry.coordinates))}`;
case 'MultiLineString':
return `MULTILINESTRING ${wrapParens(ringsWKT(geometry.coordinates))}`;
case 'GeometryCollection':
return `GEOMETRYCOLLECTION ${wrapParens(geometry.geometries.map(encodeWKT).join(', '))}`;
default:
throw new Error('stringify requires a valid GeoJSON Feature or geometry object as input');
}
}
function pairWKT(c) {
return c.join(' ');
}
function ringWKT(r) {
return r.map(pairWKT).join(', ');
}
function ringsWKT(r) {
return r.map(ringWKT).map(wrapParens).join(', ');
}
function multiRingsWKT(r) {
return r.map(ringsWKT).map(wrapParens).join(', ');
}
function wrapParens(s) {
return `(${s})`;
}