【发布时间】:2018-09-10 11:44:57
【问题描述】:
我有一个不列颠群岛县的geojson file,我正在尝试用一个合并的县替换伦敦的各个县,然后将结果保存为 geojson。 (可以识别伦敦县,因为它们的TYPE_2 属性设置为London Borough。)
我认为我可以通过以下方式执行此任务:
from shapely.geometry import Polygon, MultiPolygon, asShape
from shapely.ops import unary_union
import json, geojson
j = json.load(open('british-isles.geojson'))
# find the london counties
indices = [idx for idx, i in enumerate(j['features']) if \
i['properties']['TYPE_2'] == 'London Borough']
# transform each london county into a shapely polygon
polygons = [asShape(j['features'][i]['geometry']) for i in indices]
# get the metadata for the first county
properties = j['features'][indices[0]]['properties']
properties['NAME_2'] = 'London'
# get the union of the polygons
joined = unary_union(polygons)
# delete the merged counties
d = j
for i in indices:
del d['features'][i]
# add the new polygon to the features
feature = geojson.Feature(geometry=joined, properties=properties)
d['features'].append(feature)
# save the geojson
with open('geojson-british-isles-merged-london.geojson', 'w') as out:
json.dump(d, out)
但是,这并不能正确地合并伦敦县 - 它会导致伦敦县过去所在的多边形系列碎片化。
其他人知道我如何在 Python 中完成这项任务吗?任何建议都会非常有帮助!
【问题讨论】:
标签: python json gis geojson shapely