GIS:在 Python 中合并多边形
GIS: Merging MultiPolygons in Python
我有不列颠群岛的 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 中完成这项任务吗?任何建议都会非常有帮助!
上面有两个问题。第一个纯粹是疏忽:从 d['features']
中删除时,我需要以相反的顺序删除数组成员(删除索引 0 然后 1 与删除 1 然后删除 0 不同)。
更根本的是,上面的geojson已经是有损的了。坐标值的小数位数有限,以减少 JSON 文件大小的字节数。但这使得合并的几何体只是近似的,并导致合并的多边形之间的间隙很小:
所以我得到的工作流程是获得高分辨率 topojson file,将其转换为 geojson,使用下面的代码合并几何图形,然后 限制小数精度(如有必要)。
from shapely.geometry import Polygon, MultiPolygon, asShape
from shapely.ops import unary_union, cascaded_union
from geojson import Feature
import json
j = json.load(open('GBR_adm2.json'))
# find the london counties
indices = [idx for idx, i in enumerate(j['features']) if \
'London Borough' in i['properties']['TYPE_2']]
# 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 reversed(sorted(indices)):
del d['features'][i]
# add the new polygon to the features
feature = Feature(geometry=joined, properties=properties)
d['features'].append(feature)
# save the geojson
with open('british-isles-merged-london.geojson', 'w') as out:
json.dump(d, out)
结果:
我有不列颠群岛的 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 中完成这项任务吗?任何建议都会非常有帮助!
上面有两个问题。第一个纯粹是疏忽:从 d['features']
中删除时,我需要以相反的顺序删除数组成员(删除索引 0 然后 1 与删除 1 然后删除 0 不同)。
更根本的是,上面的geojson已经是有损的了。坐标值的小数位数有限,以减少 JSON 文件大小的字节数。但这使得合并的几何体只是近似的,并导致合并的多边形之间的间隙很小:
所以我得到的工作流程是获得高分辨率 topojson file,将其转换为 geojson,使用下面的代码合并几何图形,然后 限制小数精度(如有必要)。
from shapely.geometry import Polygon, MultiPolygon, asShape
from shapely.ops import unary_union, cascaded_union
from geojson import Feature
import json
j = json.load(open('GBR_adm2.json'))
# find the london counties
indices = [idx for idx, i in enumerate(j['features']) if \
'London Borough' in i['properties']['TYPE_2']]
# 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 reversed(sorted(indices)):
del d['features'][i]
# add the new polygon to the features
feature = Feature(geometry=joined, properties=properties)
d['features'].append(feature)
# save the geojson
with open('british-isles-merged-london.geojson', 'w') as out:
json.dump(d, out)
结果: