Wednesday, 7 September 2016

ogr2ogr - How to convert curved features into geojson


I'm using a MapGuide system which provides some CURVEPOLYGON(s).


Using python shapely's loads() raises a WKTReadingError.


Tried to find a tool that can approximate curved shapes into a polygon (as a set of points) but no luck.


I suspect ogr2ogr to be able to do this (not sure if I'm stupid) but wasn't able to do so...


Has anyone been successful when dealing with such shapes?



Answer




You're on the right track. Curved geometries were brought into GDAL/OGR 2.x. You can approximate curved geometries as a linear geometry. Both OGR and Shapely can export as a GeoJSON-like dict (via mapping):


from osgeo import ogr
from shapely import wkb
from shapely.geometry import mapping

# Import as an OGR curved geometry
wkt_in = 'CURVEPOLYGON(CIRCULARSTRING(1 3, 3 5, 4 7, 7 3, 1 3))'
g1 = ogr.CreateGeometryFromWkt(wkt_in)

# Approximate as linear geometry, and export to GeoJSON

g1l = g1.GetLinearGeometry()
geojson_out = g1l.ExportToJson()

# Or transfer to shapely, then export as GeoJSON
g2 = wkb.loads(g1l.ExportToWkb())
geojson_dict = mapping(g2)

wkt_out


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...