I'm trying to convert SHP files to GeoJson using ogr2ogr and that works well with Polygon.

Now I'm struggling to convert multiple Polygon geometry features to a single one (MultiPolygon) for each CODE_COM. Actually, the file looks like this:

"type": "FeatureCollection",
"name": "FEUILLE",
"features": [
{ "type": "Feature", "properties": { "FEUILLE": 1, "SECTION": "0A", "CODE_DEP": "01", "NOM_COM": "L'Abergement-Clémenciat", "CODE_COM": "001", "COM_ABS": "000", "ECHELLE": "2500", "EDITION": "2019-04-01", "CODE_ARR": "000" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 4.9307031, 46.173755 ], [ 4.9308624, 46.1737449 ], [ 4.9307031, 46.173755 ] ] ] } },
{ "type": "Feature", "properties": { "FEUILLE": 1, "SECTION": "0A", "CODE_DEP": "01", "NOM_COM": "Andert-et-Condon", "CODE_COM": "009", "COM_ABS": "000", "ECHELLE": "1250", "EDITION": "2019-04-01", "CODE_ARR": "000" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 5.6565458, 45.7883894 ], [ 5.6565725, 45.7883458 ],

I guess I need to join the data using an SQL query, the problem is that I don't really know how to do that. According to the output, it seems obvious to me that the features should be grouped by the "CODE_COM" property, that helps referencing the data.

Here is the ogr2ogr command I'm using to get this result:

ogr2ogr -f "GeoJSON" -lco RFC7946=YES -t_srs "EPSG:4326"

I've tried adding an SQL query like that but something is missing:

ogr2ogr -f "GeoJSON" -lco RFC7946=YES -t_srs "EPSG:4326" -dialect sqlite -sql "select st_union(geometry) as geometry from FEUILLE"

EDIT: I finally managed to make it work changing the SQL query like this:


But I now have a big issue: the Polygons aren't converted as MultiPolygons features, so there are a lot of missing lines.

Here is the result without the SQL query (what's expected but with features grouped as MultiPolygons): without the SQL query And here is the same result with the SQL query: enter image description here

  • Does it need to be done with SQL? I could give you a solution using python and geopandas. – ImanolUr Sep 3 '19 at 15:27
  • @ImanolUr What would be your solution with python and geopandas? The SQL statement is needed to merge the features. But anyway, I'm about to get rid of the geojson step and I think I will store the shp data in a pgsql database (using postgis) and perform transformation queries on it. – egdavid Sep 4 '19 at 7:26
  • another question. What do you expect to do with the other fields? If you group them by "CODE_COM" to make a MultiPolygon, you can't keep different values for each of the other fields. – ImanolUr Sep 4 '19 at 9:10
  • @ImanolUr I knew I should have updated my question with a more talkative example. In fact, there are so-called "sections", each "code_com" can have an unlimited number of "sections". Each section is a polygonal entity (feature). The idea was to get all sections for a "code_com" in a single query: a multi-polygon result instead of multiple polygon results. "code_com" is unique for every department, that was the reason I decided to use it to group the data but i will still need the others attribute in my result. – egdavid Sep 4 '19 at 15:20
  • 1
    Then I'm afraid that it's impossible what you want. By definition, a Multipolygon is one Feature, therefore, it can have just one entry for each column. That being said there are "workarounds". You could create for each value in a column, a list of the values that had the Polygons; or use the attribute table as a relational DB and create another file with each small polygon and the ID to link to the Multipolygon file. – ImanolUr Sep 5 '19 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.