2

Lets say I just wanted to turn WKT to GeoJSON on the command line I could do this:

/opt/gdal-custom/bin/ogr2ogr -f CSV /vsistdout/  foo.sqlite \
-dialect sqlite \
-sql "SELECT AsGeoJSON(GeomFromText('POLYGON ((-94.1748 26.90248,-94.1748 27.60567,-93.07617 27.60567,-93.07617 26.90248,-94.1748 26.90248))', 4326)) AS wktIntersection"

However I don't actually need the sqlite file. Is there some sort of null datasource I could use? I tried replacing foo.sqlite with /dev/null and /vsimemory/. Its just a minor annoyance.

4

Use the SQLite in-memory database: :memory:

ogr2ogr -f CSV /vsistdout/  ":memory:" -dialect sqlite \
-sql "SELECT AsGeoJSON(GeomFromText('POLYGON ((-94.1748 26.90248,-94.1748 27.60567,-93.07617 27.60567,-93.07617 26.90248,-94.1748 26.90248))', 4326)) AS wktIntersection"
  • Ok that seems to be the cleanest way to do it. – Justin Dearing Jul 10 '15 at 13:06
2

You could make a tiny Python script, e.g. wkt2json.py

#!/usr/bin/env python
import sys
from osgeo import ogr
ogr.UseExceptions()
print(ogr.CreateGeometryFromWkt(sys.argv[1]).ExportToJson())

And use it with the WKT in double quotes as the first and only argument:

$ python wkt2json.py "POINT(4 5)"
{ "type": "Point", "coordinates": [ 4.0, 5.0 ] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.