我已经编辑了我在另一个问题中回答的代码,以解决您的一些问题(并非所有人都可能)。它是可运行的(使用某些 shapefile)并生成附加图。我希望该代码对您进一步调查有用。
import cartopy.crs as ccrs
import geopandas
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, extent=[-60,120, -50,40]):
# shift: expect positive values for good results
# crs: crs="EPSG:4326" (not anything else); this handles world geometries
# 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
# must specify CRS here
moved_geom_gdf = gpd.GeoDataFrame({"geometry": moved_geom}, crs="EPSG:4326")
# can change crs here
if plotQ:
#fig1, ax1 = plt.subplots(figsize=[8,6])
fig = plt.figure( figsize=(8,8) )
ax1 = fig.add_subplot( projection=ccrs.PlateCarree() )
moved_geom_gdf.plot( ax=ax1, figsize=(8,5), scheme='quantiles', cmap='tab20')
ax1.set_extent(extent, crs=ccrs.PlateCarree())
# ax1.gridlines( draw_labels=True ) # careful, misleading labels
plt.show()
return moved_geom_gdf
wideseas = geopandas.GeoDataFrame.from_file('./data/Longhurst_world_v4_2010.shp')
wideseas180 = shift_geom(180, wideseas, True, extent=[-60,120, -50,70])
输出图:
