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.
2 Answers 2
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– Justin Dearing2015年07月10日 13:06:15 +00:00Commented Jul 10, 2015 at 13:06
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 ] }