如何在一个地图图中绘制多个GeoDataFrame(重叠)?

时间:2019-02-10 08:02:52

标签: python geopandas

我正在尝试在GeoPandas的一张地图上绘制几个不同的图层,但是只显示了最后一个。

现在,我的最终目标是在另一个数字列的基础上绘制一些类似栅格的多边形“像素”,我希望将其覆盖在我感兴趣的地图上。

以下是一些带有大型手动创建的正方形的示例:

# modules you'd need to run this
import geopandas as gpd
from matplotlib import pyplot as plt
from shapely.geometry import polygon
%matplotlib inline

# a few example squares
square1 = {
    'geometry':polygon.Polygon([
        (-4164911.2311, 2834480.454299999),
        (-3514002.14019091, 2834480.454299999),
        (-3514002.14019091, 3394480.454299999),
        (-4164911.2311, 3394480.454299999),
        (-4164911.2311, 2834480.454299999)
    ]),
    'count':95
}
square2 = {
    'geometry':polygon.Polygon([
        (-4164911.2311, 3394480.454299999), 
        (-3514002.14019091, 3394480.454299999), 
        (-3514002.14019091, 3954480.454299999), 
        (-4164911.2311, 3954480.454299999), 
        (-4164911.2311, 3394480.454299999)
    ]),
    'count':65
}
square3 = {
    'geometry': polygon.Polygon([
        (-4164911.2311, 3954480.454299999),
        (-3514002.14019091, 3954480.454299999),
        (-3514002.14019091, 4514480.454299999),
        (-4164911.2311, 4514480.454299999),
        (-4164911.2311, 3954480.454299999)
    ]),
    'count':0
}

# squares put into a GeoDataFrame
squares = gpd.GeoDataFrame([square1,square2,square3],
crs={"proj-string: +proj=aea"})

# world country outlines, included with GeoPandas library
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# selecting only for the western hemisphere
westhem = world[(world['continent'] == 'North America') | 
                (world['continent'] == 'South America')]

# ensuring crs info aligns; squares relate to data written onto 
# Albers Equal Area projection
westhem.crs = {"proj-string: +proj=aea"}

# somewhere I read that establishing your pyplot objects is always a 
# good idea
fig,ax = plt.subplots(1,1,sharex=True,sharey=True,figsize=(11,11))

# this should draw some black outlines for countries
westhem.plot(ax=ax,color='white', edgecolor='black');
# and this should map my squares onto the SAME axis.
squares.plot(ax=ax,cmap='Reds',column='count',legend=True);

但是,我得到的不是代替绘制两个地图,而是:

map of squares with no countries

如何确保两个地图同时出现,以使正方形具有背景?

1 个答案:

答案 0 :(得分:2)

您需要适当的CRS转换,以将两个数据源带入一个公共参考系统。在下面的代码中,naturalearth_lowres底图数据被转换为AlbersEqualArea进行绘图。我使用的AlbersEqualArea的某些参数可能是错误的,但是可以对其进行更正并重新运行以获取所需的输出。

import geopandas as gpd
from matplotlib import pyplot as plt
from shapely.geometry import polygon
from cartopy import crs as ccrs

# proj4str  = ccrs.AlbersEqualArea().proj4_init
# '+ellps=WGS84 +proj=aea +lon_0=0.0 +lat_0=0.0 +x_0=0.0 +y_0=0.0 +lat_1=20.0 +lat_2=50.0 +no_defs'

# a few example squares
square1 = {
    'geometry':polygon.Polygon([
        (-4164911.2311, 2834480.454299999),
        (-3514002.14019091, 2834480.454299999),
        (-3514002.14019091, 3394480.454299999),
        (-4164911.2311, 3394480.454299999),
        (-4164911.2311, 2834480.454299999)
    ]),
    'count':95
}
square2 = {
    'geometry':polygon.Polygon([
        (-4164911.2311, 3394480.454299999), 
        (-3514002.14019091, 3394480.454299999), 
        (-3514002.14019091, 3954480.454299999), 
        (-4164911.2311, 3954480.454299999), 
        (-4164911.2311, 3394480.454299999)
    ]),
    'count':65
}
square3 = {
    'geometry': polygon.Polygon([
        (-4164911.2311, 3954480.454299999),
        (-3514002.14019091, 3954480.454299999),
        (-3514002.14019091, 4514480.454299999),
        (-4164911.2311, 4514480.454299999),
        (-4164911.2311, 3954480.454299999)
    ]),
    'count':0
}

# squares put into a GeoDataFrame
squares = gpd.GeoDataFrame([square1,square2,square3],
                           crs={"proj-string: +proj=aea"})

# world country outlines, included with GeoPandas library
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# selecting only for the western hemisphere
westhem = world[(world['continent'] == 'North America') | 
                (world['continent'] == 'South America')]

# prep AlbersEqualArea projection
# default parameters:
#  central_longitude=0.0  <-- not good for USA
#  standard_parallels=(20.0, 50.0)

# for demo purposes: parameters used here may not match the ..
#  intended CRS you want
crs_aea = ccrs.AlbersEqualArea(central_longitude=0, \
                               standard_parallels=(20, 50))

# crs_aea = ...
# '+ellps=WGS84 +proj=aea +lon_0=-75 +lat_0=0.0 +x_0=0.0 +y_0=0.0 +lat_1=20 +lat_2=50 +no_defs'

# convert the geo dataframe to the projection (crs_aea) just created
westhem_crs_aea = westhem.to_crs(crs_aea.proj4_init)

fig,ax = plt.subplots(1,1,sharex=True,sharey=True,figsize=(11,11))

# this should draw some black outlines for countries
westhem_crs_aea.plot(ax=ax, color='lightgray', edgecolor='black');

# and this should map my squares onto the SAME axis
squares.plot(ax=ax,cmap='Reds',column='count',legend=True);

结果图:

enter image description here

希望这很有用。

相关问题