是否可以在x轴(经度)上移动地理 pandas 世界地图? [英] Is it possible to shift a geopandas world map on the x axis (longitude)?

查看:48
本文介绍了是否可以在x轴(经度)上移动地理 pandas 世界地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以将地理熊猫世界地图的中心定在特定的经度上.基本上,只是想将其偏移约5-10度左右.

几个月前发布了一个先前的问题,但没有得到答案.想知道是否有人知道解决方案.(有关stackoverflow的原始问题的链接是

地图移动了120°

I'm wondering if there is a way to center a geopandas world map on a particular point of longitude. Basically, just wanting to shift it by about ~5-10 degrees or so.

A previous question was posted several months ago, but didn't receive an answer. Wondering if anyone knows the solution. (Link to the original question on stackoverflow is here)

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

Would really appreciate it if someone could help.

Thanks in advance

解决方案

I found out how to 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()
   

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:

A map shifted by 120°

这篇关于是否可以在x轴(经度)上移动地理 pandas 世界地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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