GeoPandas 地图:以国家/地区为中心(并在 180 度经度处加入其分割几何图形)
GeoPandas map: Centering the country on the plot (and joining its split geometries at 180 degrees longitude)
我正在尝试使用一些自定义 shapefile 绘制俄罗斯等值线图,它目前看起来真的很尴尬。有没有什么办法可以让地图以国家为中心,这样它就不会分成两部分(也许可以放大一点)?
可以根据需要操纵某个国家的几何形状并获得更好的情节。这是一个可运行的代码及其输出图。
import matplotlib.pyplot as plt
import geopandas as gpd
from shapely.geometry import LineString
from shapely.ops import split
from shapely.affinity import translate
def shift_geom(shift, gdataframe, plotQ=False):
# this code is adapted from answer found in SO
# will be credited here: ???
shift -= 180
moved_geom = []
splitted_geom = []
border = LineString([(shift,90),(shift,-90)])
for row in gdataframe["geometry"]:
splitted_geom.append(split(row, border))
for element in splitted_geom:
items = list(element)
for item in items:
minx, miny, maxx, maxy = item.bounds
if minx >= shift:
moved_geom.append(translate(item, xoff=-180-shift))
else:
moved_geom.append(translate(item, xoff=180-shift))
# got `moved_geom` as the moved geometry
moved_geom_gdf = gpd.GeoDataFrame({"geometry": moved_geom})
# can change crs here
if plotQ:
fig1, ax1 = plt.subplots(figsize=[8,6])
moved_geom_gdf.plot(ax=ax1)
plt.show()
return moved_geom_gdf
# take the `lowres` data for use
# you can use your own geodataframe here
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# select Russia only
russia = world[world['iso_a3']=="RUS"]
# shift geometry of Russia
new_rus = shift_geom(90, russia, False)
# restore the geometry to original geo-location
# ... geometry now in 1 piece
# ... option True --> make a plot
_ = shift_geom(-90, new_rus, True)
输出图:
我正在尝试使用一些自定义 shapefile 绘制俄罗斯等值线图,它目前看起来真的很尴尬。有没有什么办法可以让地图以国家为中心,这样它就不会分成两部分(也许可以放大一点)?
可以根据需要操纵某个国家的几何形状并获得更好的情节。这是一个可运行的代码及其输出图。
import matplotlib.pyplot as plt
import geopandas as gpd
from shapely.geometry import LineString
from shapely.ops import split
from shapely.affinity import translate
def shift_geom(shift, gdataframe, plotQ=False):
# this code is adapted from answer found in SO
# will be credited here: ???
shift -= 180
moved_geom = []
splitted_geom = []
border = LineString([(shift,90),(shift,-90)])
for row in gdataframe["geometry"]:
splitted_geom.append(split(row, border))
for element in splitted_geom:
items = list(element)
for item in items:
minx, miny, maxx, maxy = item.bounds
if minx >= shift:
moved_geom.append(translate(item, xoff=-180-shift))
else:
moved_geom.append(translate(item, xoff=180-shift))
# got `moved_geom` as the moved geometry
moved_geom_gdf = gpd.GeoDataFrame({"geometry": moved_geom})
# can change crs here
if plotQ:
fig1, ax1 = plt.subplots(figsize=[8,6])
moved_geom_gdf.plot(ax=ax1)
plt.show()
return moved_geom_gdf
# take the `lowres` data for use
# you can use your own geodataframe here
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# select Russia only
russia = world[world['iso_a3']=="RUS"]
# shift geometry of Russia
new_rus = shift_geom(90, russia, False)
# restore the geometry to original geo-location
# ... geometry now in 1 piece
# ... option True --> make a plot
_ = shift_geom(-90, new_rus, True)
输出图: