Python: Cannot convert json with geometry collections to shapefile OGR2OGR: a way to explode those features?

426 views Asked by At

I finally found a way to convert JSON (GEOJSON formated) file to a shapefile using ogr2ogr. However I am encountering a new problem.

You see some of the features in my JSON file are actually a geometry collection, and this collection can either be a polygon or multipolygon by type. I am posting some examples here below:. But the main point is OGR2OGR cannot connvert these so how do I "explode" them in the right types without losing data?

Code I use to save:

import requests
import flask
import json
import os
import time
from datetime import timedelta
from datetime import datetime
from datetime import date
import pandas as pd
import shutil
import zipfile
import smtplib, ssl
from tqdm import tqdm
from time import sleep
from osgeo import ogr
from osgeo import gdal
import ogr2ogr
import geopandas
import fiona

def convert_json_to_shapefile(fc_file):
   total_start_time = start_time_measure()
   start_time = start_time_measure(
   'opening geojson file')
   export_folder = os.path.join("exported_data","geographic_information","details","Shapefile")
   shapefile = os.path.join(export_folder + "geographic_information_details"+".shp")
   ogr2ogr.main(["-f", "ESRI Shapefile", export_folder , fc_file])

This is example of a normal feature:

"type": "Feature",
            "geometry": {
                "coordinates": [
                    [
                        [
                            148162.37,
                            199935.45
                        ],
                        [
                            148128.19,
                            199917.9
                        ],
                        [
                            148120.8,
                            199915.13
                        ],
                        [
                            148114.79,
                            199916.51
                        ],
                        [
                            148112.94,
                            199918.36
                        ],
                        [
                            148100.01,
                            199910.51
                        ],
                        [
                            148099.55,
                            199904.96
                        ],
                        [
                            148097.7,
                            199902.19
                        ],
                        [
                            148092.16,
                            199899.88
                        ],
                        [
                            148088.46,
                            199897.11
                        ],
                        [
                            148087.54,
                            199899.42
                        ],
                        [
                            148056.59,
                            199890.64
                        ],
                        [
                            148057.51,
                            199887.41
                        ],
                        [
                            148036.73,
                            199891.11
                        ],
                        [
                            148030.72,
                            199892.95
                        ],
                        [
                            148027.03,
                            199894.8
                        ],
                        [
                            148025.18,
                            199897.11
                        ],
                        [
                            148021.48,
                            199894.8
                        ],
                        [
                            148023.33,
                            199892.03
                        ],
                        [
                            148044.12,
                            199887.41
                        ],
                        [
                            148061.21,
                            199885.1
                        ],
                        [
                            148099.09,
                            199896.65
                        ],
                        [
                            148128.19,
                            199914.66
                        ],
                        [
                            148151.74,
                            199924.83
                        ],
                        [
                            148164.68,
                            199930.83
                        ],
                        [
                            148162.37,
                            199935.45
                        ]
                    ]
                ],
                "type": "Polygon",
                "crs": {
                    "type": "name",
                    "properties": {
                        "name": "urn:ogc:def:crs:EPSG::31370"
                    }
                }
            },
            "properties": {
                "gipodId": 4108418,
                "StartDateTime": "1899-12-31T07:00:00",
                "EndDateTime": "2018-11-22T17:00:00",
                "sper_date_time": "2023-11-22T17:00:00",
                "state": "Concreet gepland",
                "type": "wegeniswerken (her)aanleg",
                "owner": "Gemeente Niel",
                "reference": "NIE-0000000081",
                "location": {
                    "cities": [
                        "Niel"
                    ],
                    "coordinate": {
                        "coordinates": [
                            148108.39039977788,
                            199907.735
                        ],
                        "type": "Point",
                        "crs": {
                            "type": "name",
                            "properties": {
                                "name": "urn:ogc:def:crs:EPSG::31370"
                            }
                        }
                    }
                }
            }
        },

And here is the example of a geometry collection as:

"type": "Feature",
            "geometry": {
                "geometries": [
                    {
                        "coordinates": [
                            [
                                [Made empty because of charc limit
                                ]
                            ],
                            [ Made empty because of charcter limit                                                    ]
                            ]
                        ]
                    ],
                    "type": "MultiPolygon",
                    "crs": null
                }
            ],
            "type": "GeometryCollection",
            "crs": {
                "type": "name",
                "properties": {
                    "name": "urn:ogc:def:crs:EPSG::31370"
                }
            }
        },

As an example for a example of a not multi Polygon:

                "type": "Polygon",
                "crs": null
            }
        ],
        "type": "GeometryCollection",
        "crs": {
            "type": "name",
            "properties": {
                "name": "urn:ogc:def:crs:EPSG::31370"
            }
        }
0

There are 0 answers