【问题标题】:geopandas map centering with countries以国家为中心的 geopandas 地图
【发布时间】:2021-06-24 06:28:45
【问题描述】:

我在 geopandas 上为前苏联绘制数据,它看起来真的不太好。

我尝试过早在1、2 上发布的这段代码,但没有任何帮助,因此我不需要地图,我需要在上面放数据。而且因为我正在合并“世界”(实际上只包括前苏联国家)和“国家”上的冠状病毒数据,所以我需要具有原始国家和调整多边形的数据框。

我的代码的关键和平:

url = "https://opendata.arcgis.com/datasets/a21fdb46d23e4ef896f31475217cbb08_1.geojson"
world = gpd.read_file(url)
ex_ussr = ['Ukraine', 'Belarus', 'Kyrgyzstan', 'Azerbaijan', 'Tajikistan', 'Armenia', 'Georgia', 'Russia', 'Kazakhstan', 'Lithuania', 'Latvia', 'Estonia', 'Uzbekistan']
world = world[world['CNTRY_NAME'].isin(ex_ussr)]
df_world = pd.merge(df_covid, world, on='Country')
crs = {'init': 'epsg:4326'}
corona_gpd = gpd.GeoDataFrame(df_world, crs=crs, geometry='geometry')
f, ax = plt.subplots(1, 1, figsize=(30,5))
ax = corona_gpd.plot(column='New cases', cmap='rainbow', ax=ax, legend=True, legend_kwds={'label': 'New Cases by Country'})

【问题讨论】:

  • 您尝试过不同的投影吗?你可能需要使用 cartopy
  • 我尝试移动地图,但无法在移动地图上绘图...我很早就发现了这个:youtube.com/watch?v=wEoDhO_Zuyc

标签: matplotlib geospatial geopandas


【解决方案1】:

这是一个具有挑战性的问题,我很乐意尝试。以下是一个可运行的代码,它将创建一个具有良好几何形状的 russia 地理数据框 - 几何形状不会在日期线处散开。

import numpy as np
import matplotlib.pyplot as plt
import geopandas as gpd

#import cartopy.crs as ccrs
#import cartopy

from shapely.geometry import LineString, MultiPolygon, Polygon
from shapely.ops import split
from shapely.affinity import translate
import geopandas

def shift_geom(shift, gdataframe, plotQ=False):
    # this code is adapted from somewhere found in SO
    # *** will give credit here ***
    shift -= 180
    moved_map = []
    splitted_map = []
    border = LineString([(shift,90),(shift,-90)])

    for row in gdataframe["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))

    # got `moved_map` as the moved geometry            
    gdf = geopandas.GeoDataFrame({"geometry": moved_map})
    # can move back to original pos by rerun with -ve shift

    # can change crs here
    if plotQ:
        fig, ax = plt.subplots()
        gdf.plot(ax=ax)
        plt.show()

    return gdf


world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world = world[['name', 'continent', 'geometry', 'pop_est', 'gdp_md_est']]

ex_ussr = ['Ukraine', 'Belarus', 'Kyrgyzstan', 'Azerbaijan', 'Tajikistan', 'Armenia', \
           'Georgia', 'Kazakhstan', 'Lithuania', 'Latvia', 'Estonia', 'Uzbekistan']

# ex_ussr w/o russia
ex_ussr_gdf = world[world['name'].isin(ex_ussr)]
# russia only
russia = world[ world['name']=='Russia' ]

# manipulate russia's geometry
rus_shift_90 = shift_geom(90, russia, False)        # Do not plot
good_geom_rus = shift_geom(-90, rus_shift_90, True) # Plot it

# a plot of new geometry appears

# Create geodataframe with 1 row (Multi-polygon) using this geometry
newrus_gdf = geopandas.GeoDataFrame( { "name": ["Russia"] , "new_geometry": [good_geom_rus.geometry.unary_union]}, \
                             geometry="new_geometry", crs="EPSG:4326")
# Merge `russia` with `newrus_gdf` to get everything in 1 dataframe
russia_final = russia.merge(right=newrus_gdf , on="name")

# Set the `new_geometry` from `newrus_gdf` as the geometry
russia_final.set_geometry("new_geometry", drop=True, inplace=True)

# plot all ex_ussr together = `russia_final` + `ex_ussr_gdf`
rus_ax = russia_final.plot(color='brown')
ex_ussr_gdf.plot(ax=rus_ax, color="green", ec="black", lw=0.3, alpha=0.75)

编辑

要将russia_final 添加到ex_ussr_gdf 并绘制结果,请运行以下代码:-

ex_ussr_gdf = ex_ussr_gdf.append(russia_final, ignore_index=True)
ex_ussr_gdf.plot(color="pink", ec="black", lw=0.3)

【讨论】:

  • 我尝试了相同的逻辑,在一个几何形状偏移的 gdf ​​中创建带有“俄罗斯”的 ex_ussr,但出现了错误。我需要带有“Russia”的 ex_ussr 的新 gdf,因为然后我将它与具有 2 列的“covid”数据框合并 - “Country”,“Total cases”。然后我用这个在covid数据上绘制彩虹图:``` df_exussr = pd.merge(covid_data, ex_ussr_gdf, on='Country') crs = {'init': 'epsg:4326'} corona_gpd = gpd.GeoDataFrame( df_exussr, crs=crs, geometry='geometry') f, ax = plt.subplots(1, 1, figsize=(30,5)) ax = corona_gpd.plot(column='Total cases', cmap='rainbow' , ax=ax, legend=True) ```
  • 我尝试分别将 covid 数据与 russian 和 ex_ussr gdf ​​合并。然后将 ex_ussr 附加到俄罗斯 gdf。我正在尝试绘制彩虹图,但得到错误“'Line2D'对象没有属性'列'”
  • @H.Turd​​iev 请检查我编辑的答案。记住一个问题应该集中在一个主题上。相关问题可以作为新问题提出,以便其他人可以更有效地查看和帮助。
  • 成功了!更重要的是,我明白为什么 :D 谢谢!你是天才!
  • @H.Turd​​iev 然后,考虑通过单击accept 按钮来接受我的回答。作为一个好的提问者,你会得到一些分数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
  • 2021-10-08
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多