【问题标题】:Geopandas: Get a box that coveres area of a geopandas GeoDataFrame to use it to invert a mapGeopandas:获取一个覆盖 geopandas GeoDataFrame 区域的框,以使用它来反转地图
【发布时间】:2019-04-19 17:51:36
【问题描述】:

我正在尝试反转地图。

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?

【问题讨论】:

    标签: geopandas


    【解决方案1】:

    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))
    

    【讨论】:

      【解决方案2】:

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

      原来如此

      denmark.total_bounds
      

      返回

      array([ 8.08997684, 54.80001455, 12.69000614, 57.73001659])
      

      【讨论】:

        猜你喜欢
        • 2019-10-11
        • 1970-01-01
        • 2022-09-25
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 2018-10-06
        • 2021-06-22
        • 2020-10-08
        相关资源
        最近更新 更多