我有一个问题,我想知道大地点是否在美国境内。我发现this great data source,它具有美国边境,州和县。各州和各县以Polygon
或MultiPolygon
的形式给出,这使调用polygon.contains(point)
变得容易,但是美国边界以一系列LineString
的形式给出。
如何将这些LineString
一起加入MultiPolygon
? LineString
的顺序无关紧要吗?数据中没有明显的顺序。
美国轮廓.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))
如果要在美国全境使用多面体,则可以使用美国.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))
如果要使用状态子集,则可以执行此操作。
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))
要获得有条理的解决方案,您可以这样做。
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