1 标准格式
{
"type": "FeatureCollection",
"features": [
{"type":"Feature",
"properties":{},
"geometry":{
"type":"Polygon",
"coordinates":[
[
[106.10595703125,33.33970700424026]
]
]
}
}
]}
2 利用pandas 和 shapely.geometry.MultiPolygon 构造
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon
# 构造pd.DataFrame
properties = {} # 定义 properties
properties['City'] = ['GD'] # 如果后面要再添加数据,就用properties['City'].append()
properties['Country'] = ['CN']
# ... (其它属性)
df = pd.DataFrame(properties)
# 定义geometry
multipolygon = []
poly1 = [[12734661.99535758793354, 3200451.046031608711928], [12734661.136936636641622, 3200463.744973039720207],
[12734691.826045095920563, 3200465.819490339607], [12734692.684466047212481, 3200453.120548908598721],
[12734661.99535758793354, 3200451.046031608711928]]
poly2 = [[12734692.859871655702591, 3200502.218297245912254], [12734654.788363058120012, 3200499.292509638238698],
[12734653.975644279271364, 3200509.867928693071008], [12734692.047152876853943, 3200512.793716300744563],
[12734692.859871655702591, 3200502.218297245912254]]
multipolygon.append(MultiPolygon([[poly1, []], [poly2, []]])) # 构造MultiPolygon
df['geometry'] = multipolygon
gdf = gpd.GeoDataFrame(df, geometry='geometry') # 构造gpd.GeoDataFrame
gdf.to_file('demo.geojson', driver='GeoJSON', encoding="utf-8") # 保存为json文件
print(gdf)
保存的文件:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "City": "GD", "Country": "CN" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 12734661.99535758793354, 3200451.046031608711928 ], [ 12734661.136936636641622, 3200463.744973039720207 ], [ 12734691.826045095920563, 3200465.819490339607 ], [ 12734692.684466047212481, 3200453.120548908598721 ], [ 12734661.99535758793354, 3200451.046031608711928 ] ] ], [ [ [ 12734692.859871655702591, 3200502.218297245912254 ], [ 12734654.788363058120012, 3200499.292509638238698 ], [ 12734653.975644279271364, 3200509.867928693071008 ], [ 12734692.047152876853943, 3200512.793716300744563 ], [ 12734692.859871655702591, 3200502.218297245912254 ] ] ] ] } }
]
}