[GIS] Combining GeoJSON Features

geojsonmergeogr2ogr

I haven't been able to find a solution with the source so am trying to work on the output file. I have also asked this question on SO

I have a large (200MB) geoJSON file that has a lot of complex polygons and multipolygons. A very truncated example is at https://gist.github.com/jinky32/81f61e1fc118822ba103?short_path=d16949b

As you can see this file is comprised of polygons and multipolygons that have a String property of either 1 or 2. Below is an example of how these shapes look on mapshaper.org when highlighting a multipolygon of either value in the same tile (essentially c.90+% of this tile is made up of a multipolygon with one value or the other)

Same tile with different values selected

I do not need to differentiate between these different values and polygons / multipolygons with a String value of either 1 or 2 can be combined together which I hope will reduce the file size.

Can anyone advise how I can achieve this – preferably with a cli tool?

Best Answer

That's easy to do with ogr2ogr http://www.gdal.org/ogr2ogr.html and GDAL SQLite dialect http://www.gdal.org/ogr_sql_sqlite.html.

An example using your sample data:

ogr2ogr -f "GeoJSON" -dialect sqlite -sql "select st_union(geometry) as geometry from OGRGeoJSON where string in ('1','2')" gj_union_test.json geojsontest.json

Check the result with ogrinfo:

ogrinfo gj_union_test.json -al -so
INFO: Open of `gj_union_test.json'
      using driver `GeoJSON' successful.

Layer name: OGRGeoJSON
Geometry: Multi Polygon
Feature Count: 1
Extent: (50600.010000, 301849.995000) - (653900.010000, 576205.560000)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]

As you can see there is now only one MultiPolygon feature. Another thing to notice is that if your GeoJSON don't use WGS84 coordinates you should add the CRS object http://geojson.org/geojson-spec.html#coordinate-reference-system-objects.