Convert and parse between Well-Known-Text (WKT) and GeoJSON
Using npm npm i wkt-parser-helper
Using yarn yarn add wkt-parser-helper
In CommonJS env
const { parseFromWK } = require('wkt-parser-helper');
const geojson = parseFromWK(
'POLYGON ((-3.706512451171875 40.420074462890625, -3.70513916015625 40.420074462890625, -3.70513916015625 40.42144775390625, -3.706512451171875 40.42144775390625, -3.706512451171875 40.420074462890625))'
);
// geojson is a Polygon Geometry
Using imports
import { convertToWK } from 'wkt-parser-helper';
const myFeature: Feature = {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[-3.706512451171875, 40.420074462890625],
[-3.70513916015625, 40.420074462890625],
[-3.70513916015625, 40.42144775390625],
[-3.706512451171875, 40.42144775390625],
[-3.706512451171875, 40.420074462890625],
],
],
},
};
const myFeatureAsWKT = convertToWK(myFeature);
// myFeatureAsWKT is 'POLYGON ((-3.706512451171875 40.420074462890625, -3.70513916015625 40.420074462890625, -3.70513916015625 40.42144775390625, -3.706512451171875 40.42144775390625, -3.706512451171875 40.420074462890625))'
From v4.0.0 onwards, support for converting GeoJSON to WKB is dropped.
- convertFeatureCollection
- convertFeatureToWK
- convertGeometryToWK
- convertFeatureCollectionToWktCollection
- convertToWK
- parseFromWK
▸ convertFeatureCollection(featureCollection
: FeatureCollection): string
Converts a GeoJSON FeatureCollection to WKT GeometryCollection
export
Name | Type | Description |
---|---|---|
featureCollection |
FeatureCollection | The FeatureCollection to convert to WKT |
Returns: string
The GeoJSON converted to well known representation
▸ convertFeatureToWK(geojson
: Feature): string
Converts GeoJSON Feature to WKT
export
Name | Type | Description |
---|---|---|
geojson |
Feature | Feature object to convert |
Returns: string
The GeoJSON converted to well known text representation
▸ convertGeometryToWK(geojson
: Geometry): string
Converts GeoJSON Geometry to WKT
export
Name | Type | Description |
---|---|---|
geojson |
Geometry | Geometry object to convert |
Returns: string
The GeoJSON converted to well known text representation
▸ convertFeatureCollectionToWktCollection<P>(geojson
: FeatureCollection<Geometry, P>): P & { wkt
: string }[]
Converts a GeoJSON FeatureCollection into an array of objects where each object contains a WKT (Well-Known Text) string representing the geometry and the properties from the original GeoJSON feature
Name | Description |
---|---|
P |
The type of properties in the GeoJSON features |
Name | Type | Description |
---|---|---|
geojson |
FeatureCollection<Geometry, P> | The GeoJSON FeatureCollection to be converted |
Returns: P & { wkt
: string }[]
)[]} An array of objects where each object contains a wkt
string
representing the geometry and all the properties from the original GeoJSON feature
▸ convertToWK(geojson
: GeoJSON): string
Shorthand to convert GeoJSON Features, Geometries or FeatureCollections to WKT or WKB
export
Name | Type | Description |
---|---|---|
geojson |
GeoJSON | The GeoJSON to convert |
Returns: string
The GeoJSON as WKT
▸ parseFromWK(item
: string, asFeature?
: boolean, properties?
: GeoJsonProperties): Feature | Geometry
Parse a WKT or WKB into a GeoJSON Feature or Geometry
export
Name | Type | Default value | Description |
---|---|---|---|
item |
string | - | The WKT to convert to GeoJSON |
asFeature |
boolean | false | - |
properties |
GeoJsonProperties | - | - |
Returns: Feature | Geometry
The WKT as GeoJSON