我终于找到了一种使用 ogr2ogr 将 JSON(GEOJSON 格式)文件转换为 shapefile 的方法。但是我遇到了一个新问题。
您会看到我的 JSON 文件中的一些特征实际上是一个几何集合,该集合可以是多边形或按类型划分的多多边形。我在下面发布了一些示例:但重点是 OGR2OGR 无法转换这些,所以我如何在不丢失数据的情况下以正确的类型“分解”它们?
我用来保存的代码:
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])
这是正常功能的示例:
"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"
}
}
}
}
}
},
这是几何集合的示例:
"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"
}
}
},
作为非多多边形示例的示例:
"type": "Polygon",
"crs": null
}
],
"type": "GeometryCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::31370"
}
}