[GIS] GeoJSON convert Polygons and MultiPolygons to LineStrings and MultiLineStrings

geojsonlinestringpolygonrasterio

Is there a gdal or python based tool for converting Polygons and MultiPolygons inside of GeoJSON to LineStrings and MultiLineStrings? I'm using rasterio / rio rasterize or gdal_rasterize to rasterize GeoTIFF w/ GeoJSON.

I need to read in GeoJSON files and be able to convert any (Multi)Polygons into LineStrings and MultiLineStrings so I can rasterize just the boundary or outline of the (Multi)Polygons onto GeoTIFFs (this includes exterior and interior polygons) and not have it fill in the entire shape.

Any ideas?

Best Answer

With ogr2ogr you can do it with the SQLite dialect and the ExteriorRing function from SpatiaLite http://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html

Test with ogrinfo and shapefile

ogrinfo polygons.shp -dialect SQLite -sql "select exteriorring(geometry) from polygons limit
 1"
INFO: Open of `polygons.shp'
      using driver `ESRI Shapefile' successful.

Layer name: SELECT
Geometry: Unknown (any)
Feature Count: 1
Extent: (453.000000, 636.000000) - (598.000000, 736.000000)
Layer SRS WKT:
(unknown)
Geometry Column = exteriorring(geometry)
OGRFeature(SELECT):0
  LINESTRING (453 683,598 736,564 636,453 683)

This should work for you:

ogr2ogr -f GeoJSON -dialect SQLite -sql "select exteriorring(geometry) from my_layer" output.json my_file.json

Check first with ogrinfo my_file.json what is the layer name in your source data.

I believe that you can use the same SQL of SQlite dialect directly as an input for gdal_rasterize http://www.gdal.org/gdal_rasterize.html without creating an interim file.