6

I'm trying to import a JSON file I got from a customer and convert it to a shapefile. The JSON file appears to have two Geometry types (Polygon and GeometryCollection). I can get the Polygons to import properly, however I cannot seem to get the GeometryCollection to import the Polygons they have in them. I am running QGIS 2.18.7 which I believe can handle multiple types.

I've stored the file I am trying to import here: Sample File

Is the file corrrupt or am I missing something?

3
  • 1
    OpenJUMP can import geojson with geometrycollections. You can then explode the collections and split the layer by geometry types for saving data into shapefiles.
    – user30184
    Commented Apr 27, 2017 at 6:19
  • I gave OpenJUMP a shot. However it is giving me errors trying to open the geometry collections as well. Commented Apr 27, 2017 at 15:53
  • Spatialite supports GeometryCollections if you want to keep the collection intact.
    – klewis
    Commented Apr 27, 2017 at 21:55

2 Answers 2

9

The file is not corrupt. QGIS does not support GeometryCollection as a valid geometry type. You will have to parse it. Here is a related answer. Something like this will get you to the data:

with open(json_file, 'r') as data_file:
    data = json.load(data_file)
    for feature in data['features']:
        for geometry in feature['geometry']['geometries']:
            if geometry['type'] == "Polygon":
                polygon = geometry['coordinates']
2
  • According to the QGIS import dialog, there are 3 polygons and 16 Geometry Collections. You are correct if you look at the first ones, the Geometry Collections have a polygon and a point. However if you go to the end of the file, you will find the 3 plain polygon items. QGIS will load those three, I just can't get it to properly load the other 16. Commented Apr 26, 2017 at 23:51
  • Ok, I edited my answer.
    – gisdude
    Commented Apr 27, 2017 at 2:11
4

You are right with OpenJUMP, the GeoJSON driver seems to have a bug. You can use ogr2ogr for converting the data into JML format and do the rest with OpenJUMP.

ogr2ogr -f jml tommy.jml tommy.json

EDIT OpenJUMP can open GeoJSON with GeometryCollections directly since revision r5436. Download snapshot from https://sourceforge.net/projects/jump-pilot/files/OpenJUMP_snapshots/.

Open the JML (or GeoJSON if you have version r5436 or above) that contains polygons and GeometryCollections with OpenJUMP.

enter image description here

Find the Extract Layers by Geometry Type tool.

enter image description here

Select options for extracting geometries for shapefiles and exploding GeometryCollections.

enter image description here

The tool will create two layers, one for points and another for polygons. Save them into shapefiles and you have done it.

enter image description here

1
  • Perfect. Only thing I had to research was how to run the org2org command. Never used the "OSGeo4W Shell" Under the QGIS program list before, so had to find it. Thanks for your help. Commented Apr 28, 2017 at 17:06

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.