如何从通过cartopy的feature
界面导入的数据中提取轮廓线?如果解决方案涉及geoviews.feature
或其他包装,那当然可以。
例如,如何在以下示例中提取绘制为cfeature.COASTLINE
的数据?
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE)
plt.show()
我很感激你可能有的任何暗示!
FWIW,在basemap
,我会这样做:
import mpl_toolkits.basemap as bm
import matplotlib.pyplot as plt
m = bm.Basemap(width=2000e3,height=2000e3,
resolution='l',projection='stere',
lat_ts=70,lat_0=70,lon_0=-60.)
fig,ax=plt.subplots()
coastlines = m.drawcoastlines().get_segments()
您可以直接从要素中获取绘制线的坐标,该要素包含一组shapely.MultiLineString
s。作为概念证明,请查看以下代码:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
fig, (ax1,ax2) = plt.subplots(nrows=2, subplot_kw = dict(projection=ccrs.PlateCarree()))
ax1.add_feature(cfeature.COASTLINE)
for geom in cfeature.COASTLINE.geometries():
for g in geom.geoms:
print(list(g.coords))
ax2.plot(*zip(*list(g.coords)))
plt.show()
给出了这张图片:
换句话说,您可以通过访问其MultiLineString
来迭代该功能的geometries()
s。然后,这些MultiLineString
s中的每一个都包含一个或多个LineString
s,它们具有可以转换为列表的coords
属性。希望这可以帮助。
供将来参考:一段时间之后,我还遇到了这个(更通用的?)方法来访问任何功能:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
shpfilename = shpreader.natural_earth(resolution='110m',
category='physical',
name='coastline')
coastlines = shpreader.Reader(shpfilename).records()
fig, ax = plt.subplots(subplot_kw = dict(projection=ccrs.PlateCarree()))
for c in coastlines:
for g in c.geometry:
ax.plot(*zip(*list(g.coords)))
产生与上面相同的情节。