如何将美国边界加载为一个多边形

2024-09-21 03:23:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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


Tags: 数据sourcedata顺序this形式point边界
1条回答
网友
1楼 · 发布于 2024-09-21 03:23:31

US outline.shp是线字符串,因为它们不包含整个US outline

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

若你们想得到一个多边形,对于我们所有人来说,你们可以和美国各州一起做

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

相关问题 更多 >

    热门问题