Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Latest commit

 

History

History
101 lines (78 loc) · 2.45 KB

wkt-parser.md

File metadata and controls

101 lines (78 loc) · 2.45 KB

WKT Parser

Well Known Text is a format used by databases like PostGIS. With Terraformer's WKT parser you can convert between this format and GeoJSON.

Using the WKT Parser

The WKT parser can be used client-side in a browser and server-side via Node.js.

// parse a WKT string, converting it into a Terraformer.Primitive
var geojson = Terraformer.WKT.parse("LINESTRING (30 10, 10 30, 40 40)");

// take a primitive and convert it into a WKT representation
var polygon = Terraformer.WKT.convert({
  type: "Polygon",
  coordinates: [
    [
      [100.0, 0.0],
      [101.0, 0.0],
      [101.0, 1.0],
      [100.0, 1.0],
      [100.0, 0.0],
    ],
    [
      [100.2, 0.2],
      [100.8, 0.2],
      [100.8, 0.8],
      [100.2, 0.8],
      [100.2, 0.2],
    ],
  ],
});

Using in the Browser

In the browser, the core Terraformer library is required.

<script src="terraformer.min.js"></script>
<script src="terraformer-wkt-parser.min.js"></script>
<script>
  //Terraformer and Terraformer.WKT will be defined.
</script>

You can also use Bower to install the components if you like, or download them and host them yourself.

$ bower install terraformer-wkt-parser

Using in Node.js

Just install the package from NPM with $ npm install terraformer-wkt-parser. Then include it in your application.

var WKT = require("terraformer-wkt-parser");

// Start using the parse and convert methods!

Methods

WKT.parse(string)

Terraformer.WKT.parse(string) - Used to convert a POINT, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON or MULTIPOLYGON WKT string into a Terraformer.Primitive.

var geojson = Terraformer.WKT.parse("LINESTRING (30 10, 10 30, 40 40)");

WKT.convert(geojson)

Terraformer.WKT.convert(geoJSON) will turn a GeoJSON Point, MultiPoint, LineString, MultiLineString, Polygon or MultiPolygon geometry object or a Terraformer Primitive into WKT.

Example
var polygon = Terraformer.WKT.convert({
  type: "Polygon",
  coordinates: [
    [
      [100.0, 0.0],
      [101.0, 0.0],
      [101.0, 1.0],
      [100.0, 1.0],
      [100.0, 0.0],
    ],
    [
      [100.2, 0.2],
      [100.8, 0.2],
      [100.8, 0.8],
      [100.2, 0.8],
      [100.2, 0.2],
    ],
  ],
});