如何在 Shapely 中将美国边界加载为 MultiPolygon

How to load the US borders as a MultiPolygon in Shapely

我有一个问题,我想知道一个大地测量点是否落在美国境内。我找到了 this great data source,其中包含美国边界、州和县。州和县以 Polygons 或 MultiPolygons 给出,这使得调用 polygon.contains(point) 变得容易,但美国边界以一系列 LineStrings 给出。

如何将这些 LineString 连接成一个 MultiPolygonLineString 的顺序不重要吗?数据中没有明显的排序。

美国大纲 .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