geopandas - Geopandas:获取一个覆盖 geopandas GeoDataFrame 区域的框,以使用它来反转地图
问题描述
我正在尝试反转地图。
import geopandas as gpd
import geoplot as gplt
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
denmark = world[world.name == 'Denmark']
我想找出“丹麦”数据框的边界,以便我可以创建一个覆盖整个丹麦的箱形 GeoDataFrame。
然后我将它与“丹麦”相交以获得所有不是丹麦的形状,我以后可以用它来覆盖我不想显示的地图部分。
我尝试通过 GeoDataFrame 手动创建此框,但效果不佳。
cords = [c3
for c in mapping(denmark['geometry'])['features']
for c2 in c['geometry']['coordinates']
for c3 in c2
]
xcords = [x[0] for x in cords if isinstance(x[0], float)]
ycords = [y[1] for y in cords if isinstance(y[1], float)]
w3 = gpd.GeoDataFrame(
[Polygon([[max(xcords), max(ycords)],
[max(xcords), min(ycords)],
[min(xcords), min(ycords)],
[min(xcords), max(ycords)]
])],
columns = ['geometry'],
geometry='geometry')
有没有一种简单快捷的方法来获得这个盒子?或者有没有办法 tp 反转 GeoDataFrame?
解决方案
GeoDataFrame 具有total_bounds
返回所有几何图形的 minx、miny、maxx、maxy 的属性(所有几何图形的最小值/最大值bounds
)。
并且要创建一个多边形,然后您可以将这些值传递给shapely.geometry.box
函数:
>>> denmark.total_bounds
array([ 8.08997684, 54.80001455, 12.69000614, 57.73001659])
>>> from shapely.geometry import box
>>> box(*denmark.total_bounds)
<shapely.geometry.polygon.Polygon at 0x7f06be3e7668>
>>> print(box(*denmark.total_bounds))
POLYGON ((12.6900061377556 54.80001455343792, 12.6900061377556 57.73001658795485, 8.089976840862221 57.73001658795485, 8.089976840862221 54.80001455343792, 12.6900061377556 54.80001455343792))
推荐阅读
- java - NMT 类已提交与 Jstat 压缩类空间
- node.js - 如何使用 Nginx 在 MEAN 堆栈应用程序上正确设置 HTTPS/SSL
- jenkins-pipeline - BuildInfo 中未显示的依赖项数量(使用 uploadSpec)
- ios - UIPrintInteractionController 不显示多个页面
- flutter - Flutter 的高级 SDK 更新?
- python - 在字典列表中查找字典编号 x
- c# - C# 中的登录限制尝试
- java - 当我在循环数组队列中出列时,我的项目无法正确显示
- ios - 故事板未在 Xcode 11 中删除
- pandas - pandas pivot_table aggfunc/values 参数行为异常