3

I have a geojson file containing a feature with geometrycollection type (polygon and linestring). I need to produce two ESRI Shapefile from the geojson geometrycollection: one with Polygon only and a second with Linestring only.

The code must be written in python.

There is any solution?


I met another problem in the "from geojson to shapefile" process. My geojson file include some features with geometrytype = MultiPolygon. These MultiPolygon are constructed in such a way that the first element includes the following elements and these never intersect. I enclose a picture , showing the geojson file opened into QGIS, that clarifies the situation:

enter image description here

When I use the ogr2ogr to convert geojson to shapefile I obtain the following result, shown in the following figure:

enter image description here

There is an ogr2ogr function allowing to explode the multipolygon into polygon?

I can't use the QGIS ftools "from multi parts to single parts" because I must have the instance of python I use to develop separeted from the QGIS folder. I try with fiona, shapely and json:

    with fiona.open(settings.L_json, 'r') as data_file:

    data = json.load(data_file)
    for f in data_file:
        next = f.next()
        multipol = shape(next['type']['geometry']['properties'])
        for poly in multipol:
            feature = {}
            feature['type'] = poly['type']
            feature['geometry']['type'] = 'Polygon'
            feature['geometry']['coordinates'] = poly['geometry']['coordinates']
            feature['properties'] = poly['properties']
            data['features'].append(feature)
            if settings.L_json['*L_*']:
                with open('G:\\geojson\\geojson_Rep\\LOW_'+settings.now+'.geo.json', 'w') as f:
                    f.write(json.dumps(data))

but it does not work. Any simplest solution? ogr2ogr can do it? It is ok also obtaining from the first geojson other geojson splitting the multipolygon geometry.

Simplifying, I need to get n polygon from a multipolygon with n elements, in one of the following formats: .geo.json, .shp, .wkt.

Any hint?


Here my solution:

with fiona.open(input, 'r') as source:

    count = 0
    for f in source:
        c = f['geometry']['coordinates']

        if count == 0:
            outputfile = 'G:\\SVN EO\\Python\\Utility\\geojson\\tmp\\mondometeo_20160930_145814_MEDIUM.geo.json'
            with fiona.open(outputfile, 'w', driver=source.driver, schema=source.schema) as output:

                output.write({
                    "type": f['type'],
                    "geometry": {"type": "Polygon", "coordinates": c[count]},
                    "properties": f['properties']})
            return outputfile
        elif count >0:
            outputfile = 'G:\\SVN EO\\Python\\Utility\\geojson\\tmp\\mondometeo_20160930_145814_TOP'+str(count)+'.geo.json'
            with fiona.open(outputfile, 'w', driver=source.driver, schema=source.schema) as output:
                output.write({
                    "type": f['type'],
                    "geometry": {"type": "Polygon", "coordinates": c[count]},
                    "properties": f['properties']})

            return outputfile
        count +=1

Anyone has some other solution or hint (also performing the if-elif cycle)?

  • Here on GIS Stack, we usually don't write the code for you, instead we like to help you when your code isn't working. So try to write something on your own and reform your question. PS: Some general links to help you with parsing json files in python -> stackoverflow.com/questions/2835559/… and resources.arcgis.com/en/help/main/10.1/index.html#/… will help you with exporting to shapefiles and also pro.arcgis.com/en/pro-app/tool-reference/data-management/… – Piskr Sep 28 '16 at 10:25
  • I'm sorry. I do not need the code but just an hint about some module or function. In fact, I was hoping there was a module for managing geojson files that can help me to solve the problem subject of my question, directly or also indirectly. – lorenzo Sep 28 '16 at 10:29
  • My hint is to use GDAL with SQLite SQL dialect and SpatiaLite functions, explode GeometryCollection to elementary geometries and then filter by geometry type. – user30184 Sep 28 '16 at 11:06
  • 1
    I solved using {gdal -f geojson -dialect sqlite -mapFiledType Date=String,Time=String -sql "SELECT * FROM OGRGeoJSON WHERE GeometryType(geometry)='MULTPOLIGON'" output.geo.json input.geo.json}. The following step is to convert this geojson to a shpefile. I use ogr2ogr function and it works very well. Just a question: all features have properties with the UTC date expressed into the ISO-8601 format (yyyy-mm-ddThh:mm:ss.xxxZ). ESRI Shapefile driver does not recognize this format. I would store this properties into an attribute (string format). Any hint? Thanks – lorenzo Sep 29 '16 at 12:33
  • i just wrote a script that converts GeoJSON to a feature class, would that be of anyhelp. you can just add the arcpy.FeatureClassToShapefile_conversion() and you would be good to go – ziggy Sep 30 '16 at 15:42

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.