geolocation - 地理点超出预期边界
问题描述
我有一个包含在 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()
在图表上检查后,这些点超出了我的预期范围。有什么我想念的吗?我是否需要创建边界来限制点的分散?
解决方案
剧情看起来不错。我想你想排除美国本土以外的点。这些点显然在夏威夷、阿拉斯加和加拿大。
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")
推荐阅读
- javascript - 如何在 sapui5 中手动调整 sap.m.TextArea 的大小
- powershell - Powershell 脚本未以管理员身份运行
- excel - 在 if thens 之间滑入非条件代码
- php - 在 FPDF 中设置自定义纸张尺寸
- connection-pooling - HikariCP 使用 SSD 的最大池大小
- c# - 2结帐库问题
- mysql - Laravel/Lumen MySQL '准备好的语句需要重新准备' 错误
- python - “管道”正则表达式操作
- sql - SQL如何显示退货和购买日期之间的月差
- c# - 如何通过计算属性过滤 mongodb 集合