首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Geopandas:得到一个覆盖地质公园GeoDataFrame区域的盒子,用来倒置地图。

Geopandas:得到一个覆盖地质公园GeoDataFrame区域的盒子,用来倒置地图。
EN

Stack Overflow用户
提问于 2019-04-19 09:37:17
回答 2查看 3.6K关注 0票数 5

我想把地图倒过来。

代码语言:javascript
运行
复制
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手动创建这个框,但这不太好。

代码语言:javascript
运行
复制
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')

有什么简单快捷的方法可以得到这个盒子吗?还是有一种倒置GeoDataFrame的方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-19 09:51:36

GeoDataFrame具有total_bounds属性,它返回所有几何图形的minx、miny、maxx、maxy (所有几何图形的bounds的min/max )。

要创建这个多边形,您可以将这些值传递给shapely.geometry.box函数:

代码语言:javascript
运行
复制
>>> 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))
票数 14
EN

Stack Overflow用户

发布于 2019-04-19 09:48:51

看起来GeoDataFrame有一个属性"total_bounds“

所以这是

代码语言:javascript
运行
复制
denmark.total_bounds

回传

代码语言:javascript
运行
复制
array([ 8.08997684, 54.80001455, 12.69000614, 57.73001659])
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55759733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档