设置Geopandas地图的中心 [英] Set centre of geopandas map

查看:149
本文介绍了设置Geopandas地图的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过以下方式绘制带有大熊猫的世界地图:

I can plot a world map with geopandas with:

world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
fig, ax = plt.subplots()
world.plot(ax=ax, color=(0.8,0.5,0.5))

,它工作正常,但我想将地图的中心定在与本初子午线不同的经度上.我该怎么做?

and it works fine, but I would like to center the map on a different longitude than the Prime Meridian. How do I do this?

推荐答案

这是您可以执行的操作:

This is how you can do it:

from shapely.geometry import LineString
from shapely.ops import split
from shapely.affinity import translate
import geopandas

world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

def shift_map(shift):
    shift -= 180
    moved_map = []
    splitted_map = []
    border = LineString([(shift,90),(shift,-90)])
    for row in world["geometry"]:
        splitted_map.append(split(row, border))
    for element in splitted_map:
        items = list(element)
        for item in items:
            minx, miny, maxx, maxy = item.bounds
            if minx >= shift:
                moved_map.append(translate(item, xoff=-180-shift))
            else:
                moved_map.append(translate(item, xoff=180-shift))
    gdf = geopandas.GeoDataFrame({"geometry":moved_map})
    fig, ax = plt.subplots()
    gdf.plot(ax=ax)
    plt.show()
   

第一步,创建您的世界并将其分割在您的预定义边界上. 然后,您将获得所有元素的边界,并检查边界是否与所需的平移相匹配.然后,将比边界大的每个元素平移到地图的左侧,然后将所有其他元素移到右侧,以使它们以+ 180°倾斜.

In the first step, you create your world and split it on a pre defined border of yours. Then you get the bounds of all elements and check if the bounds match your desired shift. Afterwards you translate every element bigger than your border to the left side of the map and move all other elements to the right side, so that they aling with +180°.

例如,这为您提供:

This gives you for example:

地图移动了120°

这篇关于设置Geopandas地图的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆