如何在Shapely中将美国边界作为多多边形加载

问题描述 投票:0回答:1

我有一个问题,我想知道大地点是否在美国境内。我发现this great data source,它具有美国边境,州和县。各州和各县以PolygonMultiPolygon的形式给出,这使调用polygon.contains(point)变得容易,但是美国边界以一系列LineString的形式给出。

如何将这些LineString一起加入MultiPolygonLineString的顺序无关紧要吗?数据中没有明显的顺序。

python shapely
1个回答
1
投票

美国轮廓.shps是线串,因为它们不包含整个美国轮廓。

outline_df = gpd.read_file('gz_2010_us_outline_20m.shp')
outline_df = outline_df.loc[outline_df['TYPE'] == 'COASTAL']
outline_df.plot(figsize=(12,12))

US outline from gz_2010_us_outline_20m.shp

如果要在美国全境使用多面体,则可以使用美国.shp来完成。

states_gdf = gpd.read_file('gz_2010_us_040_00_500k.shp')
states_gdf['dissolve_field'] = 1
states_gdf = states_gdf.dissolve(by='dissolve_field')
states_gdf.plot(figsize=(12,12))

enter image description here

如果要使用状态子集,则可以执行此操作。

states_gdf = gpd.read_file('gz_2010_us_040_00_500k.shp')

state_subset_list = ['California', 'Washington', 'Oregon', 'Texas']
states_gdf = states_gdf.loc[states_gdf['NAME'].isin(state_subset_list)]

states_gdf['dissolve_field'] = 1
states_gdf = states_gdf.dissolve(by='dissolve_field')
states_gdf.plot(figsize=(12,12))

subset of states

要获得有条理的解决方案,您可以这样做。

import fiona
from shapely.geometry import shape
from shapely.ops import unary_union
uu_mp = unary_union([shape(poly['geometry']) for poly in fiona.open('gz_2010_us_040_00_500k.shp')])

uu_mp

unary union mp

© www.soinside.com 2019 - 2024. All rights reserved.