3

Right now I have a GeoJson file with a structure like this:

{
"geometry": {
    "type": "MultiLineString",
    "coordinates": [
        [  <Coords line 1>
        ],
        [  <Coords line 2>
        ],
        [  <Coords line 3>
        ] 
    ]
},
"type": "Feature",
"properties": {
    "time": 960,
    "value": 0.5
} }

I want to create a MultiPolygon shapefile from that but ogr2ogr throws an error using this command:

ogr2ogr -f "ESRI Shapefile" -nlt MULTIPOLYGON <out_file.shp> <file.shp> OGRGeoJson


ERROR 1: Attempt to write non-polygon (MULTILINESTRING) geometry to POLYGON type shapefile.

I've been searching other possibilities to achieves this and for example with QGis is quite simple, but I need a solution that can be used from terminal.

As a side note, all the lines in the geoJson starts and ends at the same point, so they make a closed line.

1 Answer 1

1

Use the SQLite/SpatiaLite SQL dialect http://www.gdal.org/ogr_sql_sqlite.html and you can utilize all that is supported by SpatiaLite http://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html.

Test with ogrinfo

ogrinfo -dialect sqlite -sql "select BuildArea(geometry) from multiline" multiline.json
INFO: Open of `multiline.json'
      using driver `GeoJSON' successful.

Layer name: SELECT
Geometry: Unknown (any)
Feature Count: 1
Extent: (0.000000, 20.000000) - (20.000000, 40.000000)
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"]]
Geometry Column = BuildArea(geometry)
OGRFeature(SELECT):0
  POLYGON ((0 20,0 40,20 40,20 20,0 20))

And then convert with ogr2ogr. I left out the -nlt multipolygon because in shapefiles polygons and multipolygons are the same.

ogr2ogr -f "ESRI Shapefile" -dialect sqlite -sql "select ST_BuildArea(geometry) from multiline" out.shp multiline.json

It gets boring to write answers about all that is possible to do with ogrinfo, ogr2ogr, and SQLite dialect. There have already quite many https://gis.stackexchange.com/search?q=dialect+sqlite. Do we have FAQ?

1
  • Thanks, I'll try to test this solution. But now I have to update the gdal version I'm using since it does not support -dialect sqlite (GDAL 1.9.0, released 2011/12/29)
    – CRE
    Commented Feb 13, 2018 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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