[英]Extracting data from cartopy.feature
如何从通过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
的要素中获取绘制线的坐标。 作为概念证明,请查看以下代码:
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()
给出这张照片:
换句话说,您可以通过访问其geometries()
来遍历特征的MultiLineString
。 然后,每个MultiLineString
包含一个或多个LineString
,它们具有可以转换为列表的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)))
产生与上述相同的情节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.