首页 > 解决方案 > 地理点超出预期边界

问题描述

我有一个包含在 GeoDataFrame 中的美国位置的点几何。我想将其绘制为美国地图上的散点图。我的代码是:

import numpy as np
import geopandas as gpd
import libpysal
import contextily as ctx
import matplotlib.pyplot as plt
from shapely.ops import cascaded_union

gdf = gpd.GeoDataFrame(point_geometry, geometry='geometry')
boundary = gpd.read_file(libpysal.examples.get_path('us48.shp'))
fig, ax = plt.subplots(figsize=(50, 50))
boundary.plot(ax=ax, color="gray")
gdf.plot(ax=ax, markersize=3.5, color="black")
ax.axis("off")
plt.axis("equal")
plt.show()

在图表上检查后,这些点超出了我的预期范围。有什么我想念的吗?我是否需要创建边界来限制点的分散?

在此处输入图像描述

标签: geolocationgisgeospatialgeopandas

解决方案


剧情看起来不错。我想你想排除美国本土以外的点。这些点显然在夏威夷、阿拉斯加和加拿大。

point从带有几何图形gdf ​​和几何图形边界的地理数据框中,您可以创建一个适当的边界,该边界可用于限制点的分散。polygon

# need this module
from shapely.ops import cascaded_union

# create the conterminous USA polygon
poly_union = cascaded_union([poly for poly in boundary.geometry])

# get a selection from `gdf`, taking points within `poly_union` 
points_within = gdf[gdf.geometry.within(poly_union)]

现在,points_within是一个地理数据框,您可以使用它来绘制而不是gdf.

points_within.plot(ax=ax, markersize=3.5, color="black")

推荐阅读