跨越 lon=180 的缓冲线导致带状 geopandas matplotlib

问题描述

当地图的 central_longitude = 180 时,我试图在 180 经度上绘制多边形几何图形;但是会出现人工条带。

有没有办法阻止 matplotlib 出现红色圆圈中显示的条带?

enter image description here

import matplotlib.pyplot as plt
import geopandas as gpd
import cartopy.crs as ccrs

lats = np.linspace(-75,75,100)
lons = np.linspace(150,190,100)
gdf = gpd.GeoDataFrame(
    crs=4326,geometry=gpd.points_from_xy(lons,lats)
)
gdf = gdf.to_crs({'init': 'epsg:3174'})
gdf['geometry'] = gdf['geometry'].buffer(90000)
gdf['route'] = 0
gdf = gdf.to_crs({'init': 'epsg:4326'})

ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_geometries(gdf['geometry'],crs=ccrs.PlateCarree())
ax.set_extent([120,250,-75,75],crs=ccrs.PlateCarree())
ax.gridlines(draw_labels=True,xlocs=[180])
ax.coastlines()

解决方法

epsg:3174 重新投影到 EPSG:4326 会使穿越日期变更线的几何图形变得混乱。您可以直接使用 epsg:3174 绘制地理数据框,如以下代码所示:

import matplotlib.pyplot as plt
import geopandas as gpd
import cartopy.crs as ccrs
import numpy as np

lats = np.linspace(-75,75,100)
lons = np.linspace(150,190,100)
gdf = gpd.GeoDataFrame(
    crs=4326,geometry=gpd.points_from_xy(lons,lats)
)
# gdf2 = gdf.to_crs({'init': 'epsg:3174'})
gdf2 = gdf.to_crs(epsg=3174)
gdf2['geometry'] = gdf2['geometry'].buffer(90000)
gdf2['route'] = 0

ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_geometries(gdf2['geometry'],crs=ccrs.epsg(3174))
ax.set_extent([120,250,-75,75],crs=ccrs.PlateCarree())
ax.gridlines(draw_labels=True,xlocs=[180])
ax.coastlines()

plt.show()

route_xing_dateline

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...