卡通片:绘制删除了国家边界的海岸线

2024-05-15 19:32:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用一种颜色勾勒出中国的轮廓,同时用另一种颜色显示全球海岸线。我的第一次尝试是:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as feature
import cartopy.io.shapereader as shapereader


countries = shapereader.natural_earth(resolution='110m',
                                      category='cultural',
                                      name='admin_0_countries')

# Find the China boundary polygon.
for country in shapereader.Reader(countries).records():
    if country.attributes['su_a3'] == 'CHN':
        china = country.geometry
        break
else:
    raise ValueError('Unable to find the CHN boundary.')


plt.figure(figsize=(8, 4))
ax = plt.axes(projection=ccrs.PlateCarree())

ax.set_extent([50, 164, 5, 60], ccrs.PlateCarree())

ax.add_feature(feature.LAND)
ax.add_feature(feature.OCEAN)
ax.add_feature(feature.COASTLINE, linewidth=4)

ax.add_geometries([china], ccrs.Geodetic(), edgecolor='red',
                  facecolor='none')

plt.show()

The result

我把海岸线加厚了,这样你就可以看到它们与国家边界重叠的事实。在

我的问题是:有没有办法去掉国家轮廓旁边的海岸线,这样我就不会让这两条线在视觉上相互影响了?在

Note: This question was asked to me directly via email, and I chose to post my response here so that others may learn/benefit from a solution.


Tags: toimportadd颜色aspltaxcountry
1条回答
网友
1楼 · 发布于 2024-05-15 19:32:03

在自然地球集合中没有一个叫做“没有中国边界的海岸线”的数据集,所以我们只能自己制作。为此,我们需要使用shapely操作,尤其是它的difference方法。在

差分法如下所示(取自Shapely's docs)。下面突出显示了两个示例圆(ab)的区别:

difference method

那么,我们的目标是达到写作的目的,并将这个结果视为我们的海岸线。

有很多方法可以做到这一点。GeopDaas和菲奥娜是两种能够给出非常可读结果的技术。在这种情况下,让我们使用cartopy提供的工具:

首先,我们掌握了中国边界(另见:cartopy shapereader docs)。在

import cartopy.io.shapereader as shapereader


countries = shapereader.natural_earth(resolution='110m',
                                      category='cultural',
                                      name='admin_0_countries')

# Find the China boundary polygon.
for country in shapereader.Reader(countries).records():
    if country.attributes['su_a3'] == 'CHN':
        china = country.geometry
        break
else:
    raise ValueError('Unable to find the CHN boundary.')

接下来,我们要了解海岸线的几何形状:

^{pr2}$

现在,让中国走出海岸线:

coastlines_m_china = [geom.difference(china)
                      for geom in coastlines]

当我们将其形象化时,我们会发现两者之间的差异并不完美:

Imperfect difference

我们不想要黑线的原因是自然地球海岸线数据集与国家数据集的派生方式不同,因此它们不是完全重合的坐标。在

为了绕开这一事实,可以在中国边界上应用一个小“黑客”来扩大边界,以达到这个交集的目的。buffer方法非常适合于此目的。在

# Buffer the Chinese border by a tiny amount to help the coordinate
# alignment with the coastlines.
coastlines_m_china = [geom.difference(china.buffer(0.001))
                      for geom in coastlines]

有了这个“黑客”后,我得到了以下结果(完整性包括完整的完整代码):

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as feature
import cartopy.io.shapereader as shapereader


coast = shapereader.natural_earth(resolution='110m',
                                  category='physical',
                                  name='coastline')

countries = shapereader.natural_earth(resolution='110m',
                                      category='cultural',
                                      name='admin_0_countries')

# Find the China boundary polygon.
for country in shapereader.Reader(countries).records():
    if country.attributes['su_a3'] == 'CHN':
        china = country.geometry
        break
else:
    raise ValueError('Unable to find the CHN boundary.')

coastlines = shapereader.Reader(coast).geometries() 

# Hack to get the border to intersect cleanly with the coastline.
coastlines_m_china = [geom.difference(china.buffer(0.001))
                      for geom in coastlines]

ax = plt.axes(projection=ccrs.PlateCarree())

ax.set_extent([50, 164, 5, 60], ccrs.PlateCarree())
ax.add_feature(feature.LAND)
ax.add_feature(feature.OCEAN)

ax.add_geometries(coastlines_m_china, ccrs.Geodetic(), edgecolor='black', facecolor='none', lw=4)
ax.add_geometries([china], ccrs.Geodetic(), edgecolor='red', facecolor='none')

plt.show()

The result

相关问题 更多 >