wellknown

repository·master·Indexed 42 days ago

https://github.com/mapbox/wellknown

A utility for converting between Well-Known Text (WKT) and GeoJSON formats. Version 0.5.0 supports 2D, 3D, and 4D geometries, including Point, LineString, Polygon, and GeometryCollection. It provides a CLI for piping WKT to GeoJSON and a JavaScript API featuring parse() for WKT to GeoJSON conversion and stringify() for GeoJSON to WKT conversion.

Tokens
1K
Snippets
8
Records
8
Agent score
55%

What's inside wellknown

  1. Install wellknown via npm or CLI

    master

    You can install wellknown as a global CLI tool or as a local dependency for your project.

    Global CLI installation:

    $ npm install -g wellknown

    Local npm installation:

    $ npm install wellknown

    Bower installation:

    $ bower install wellknown --save
    npm install -g wellknown
  2. Parse WKT into GeoJSON with `parse(wkt)`

    master

    The parse(wkt) method converts a WKT string into a GeoJSON geometry object. If the parsing fails, it returns null.

    Supported Geometries:

    • Point + MultiPoint
    • LineString + MultiLineString
    • Polygon + MultiPolygon
    • GeometryCollection
    • 2D, 3D, and 4D geometries
    var parse = require('wellknown');
    
    parse('POINT(1 2)');
  3. Stringify GeoJSON to WKT with `stringify(geojson)`

    master

    The stringify(geojson) method converts a GeoJSON geometry object or Feature object into its WKT string representation.

    Note: This method will throw an error if you provide a FeatureCollection or an unknown input type.

    var stringify = require('wellknown');
    
    stringify(geojsonObject);
  4. Convert WKT to GeoJSON using the CLI

    master

    The wellknown CLI can be used to pipe Well-Known Text (WKT) into a GeoJSON file. Use standard input to provide the WKT string.

    Example usage:

    echo "MultiPoint(0 0, 1 1, 3 3)" | wellknown > multipoint.geojson
  5. Stringify GeoJSON into WKT with stringify()

    master

    The stringify(gj) function converts a GeoJSON geometry object or a GeoJSON Feature into a Well-Known Text (WKT) string.

    Supported GeoJSON types include:

    • Point
    • LineString
    • Polygon
    • MultiPoint
    • MultiLineString
    • MultiPolygon
    • GeometryCollection
    • Feature (the function will extract the geometry property from the feature)

    If the input is not a valid GeoJSON object or a supported type, the function throws an error: stringify requires a valid GeoJSON Feature or geometry object as input.

    const stringify = require('wellknown');
    const wkt = stringify({
      type: 'Point',
      coordinates: [10, 20]
    });
    // Returns 'POINT (10 20)'
  6. Parse WKT into GeoJSON with parse()

    master

    The parse(input) function converts a Well-Known Text (WKT) string into a GeoJSON geometry object.

    If the input string contains an SRID (Spatial Reference System Identifier) defined via a semicolon (e.g., SRID=4326;POINT(0 0)), the resulting GeoJSON object will include a crs property following the OGC standard:

    {
      "type": "Point",
      "coordinates": [0, 0],
      "crs": {
        "type": "name",
        "properties": {
          "name": "urn:ogc:def:crs:EPSG::4326"
        }
      }
    }

    If the WKT is malformed, the function returns null.

    const parse = require('wellknown');
    const geojson = parse('SRID=4326;POINT(10 20)');
  7. Use the wellknown CLI to convert WKT to GeoJSON

    master

    The wellknown CLI tool converts Well-Known Text (WKT) into GeoJSON. It accepts input either from a file path provided as a command-line argument or via stdin. The resulting GeoJSON is printed to stdout as a formatted JSON string.

    Usage

    From a file:

    wellknown path/to/input.wkt

    From stdin:

    echo 'POINT(0 0)' | wellknown

    If the input is invalid or cannot be parsed, the CLI will output an error message to stderr and display the help documentation.

    # Example: Convert a WKT file to GeoJSON
    wellknown input.wkt
    
    # Example: Pipe WKT string via stdin
    echo "LINESTRING(30 10, 10 30, 40 40)" | wellknown