Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
geo / geo-geojson-to-wktOpenRuns on Liberas

GeoJSON to WKT

An RFC 7946 GeoJSON geometry object (JSON text) as WKT (OGC Simple Features 1.2.1, 2D). The input is one RFC 8259 JSON value with optional surrounding whitespace, at most 1,048,576 bytes; invalid JSON, duplicate member names in any object, lone surrogate escapes, numbers that overflow binary64 or nesting deeper than 128 are errors. It must be an object whose "type" is Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon or GeometryCollection (case-sensitive; Feature and anything else are errors); the first six need "coordinates", GeometryCollection needs "geometries" (an array of geometry objects, at most 32 collections deep); other members are ignored. A position is an array of exactly 2 numbers (altitude is an error). Point: a position, or [] for empty; LineString, MultiPoint: positions; Polygon, MultiLineString: arrays of those; MultiPolygon: polygons, each with at least one ring. A line string (alone or in a MultiLineString) has at least 2 positions; a ring has at least 4 and ends at its first position (x = x and y = y numerically); members of a multi-geometry are never empty; else an error. Nothing else is checked (no orientation, self-intersection or coordinate range). Output: the type in uppercase (POINT, LINESTRING, …, GEOMETRYCOLLECTION), then " EMPTY" when it has no coordinates (or members), else a space and the parenthesized text: POINT (x y); LINESTRING (x y, x y); POLYGON ((x y, …), (…)); MULTIPOINT ((x y), (x y)); MULTILINESTRING ((…), (…)); MULTIPOLYGON (((…)), ((…))); GEOMETRYCOLLECTION (POINT (x y), …) — items separated by ", ", x and y by one space. Numbers as ECMAScript Number::toString (shortest round-trip digits, e.g. 1e+21, 1e-7, −0 as 0), so geo-wkt-to-geojson reads every output back to the same geometry.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init geo-geojson-to-wkt --lang rust
cd geo-geojson-to-wkt && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)11 lines›
geo-geojson-to-wkt.wit
package root:component;

world root {
  export liberas:geo/geojson-to-wkt@0.1.0;
}
package liberas:geo@0.1.0 {
  interface geojson-to-wkt {
    to-wkt: func(geojson: string) -> result<string, string>;
  }
}
Test vectors26›
geo-geojson-to-wkt.cases.json
[
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[30,10]}"
    }
  },
  {
    "args": {
      "geojson": "{\"coordinates\":[-0.5,1e-7],\"type\":\"Point\",\"bbox\":[0,0,1,1],\"properties\":{\"a\":[1,{\"b\":null}]}}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"LineString\",\"coordinates\":[[30,10],[10,30],[40,40]]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Polygon\",\"coordinates\":[[[35,10],[45,45],[15,40],[10,20],[35,10]],[[20,30],[35,35],[30,20],[20,30]]]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"MultiPoint\",\"coordinates\":[[10,40],[40,30]]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"MultiLineString\",\"coordinates\":[[[10,10],[20,20]],[[40,40],[30,30]]]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[40,40],[20,45],[45,30],[40,40]]]]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[40,10]},{\"type\":\"Polygon\",\"coordinates\":[]}]}"
    }
  },
  {
    "args": {
      "geojson": " {\"type\" : \"Point\", \"coordinates\" : [ 1.5E+21 , -0.0 ] } "
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"GeometryCollection\",\"geometries\":[]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[123456789012345678901234567890,0.1],\"x\":\"\\u00e9\\ud83d\\ude00\"}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[1,2,3]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[1,2],\"type\":\"Point\"}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Feature\",\"geometry\":null,\"properties\":{}}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"point\",\"coordinates\":[1,2]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[1e400,0]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"LineString\",\"coordinates\":[[1,2]]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Polygon\",\"coordinates\":[[[0,0],[1,0],[1,1],[0,1]]]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"MultiPolygon\",\"coordinates\":[[]]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[01,2]}"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[1,2]} x"
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"Point\",\"coordinates\":[1,2],\"x\":\"\\ud800\"}"
    }
  },
  {
    "args": {
      "geojson": "[1,2]"
    }
  },
  {
    "args": {
      "geojson": ""
    }
  },
  {
    "args": {
      "geojson": "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"GeometryCollection\",\"geometries\":[]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 26 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 4,500,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.