You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cartopy绘制河流时报URL错误,无法显示加尔维斯顿航道问题咨询

问题解决方案

一、URL报错的原因与修复方法

你遇到的socket.gaierror: [Errno 11001] getaddrinfo failed报错,是因为当前使用的Cartopy版本内置的NaturalEarth数据集下载地址naciscdn.org已经失效,域名无法解析导致自动下载失败。
可任选以下一种方式修复:

  • 将Cartopy升级到最新正式版,新版已将数据源地址切换为NaturalEarth官方域名,可正常完成自动下载
  • 手动下载10m精度的ne_10m_rivers_lake_centerlines.zip数据包,放入本地Cartopy缓存目录:C:\Users\你的用户名\.local\share\cartopy\shapefiles\natural_earth\physical\,无需解压即可被Cartopy直接识别调用

二、rivers功能无法满足需求的原因

你当前调用的rivers_lake_centerlines是天然河流、湖泊的中心线线要素,仅能通过edgecolor设置线条颜色,你配置的facecolor参数是无效的;且加尔维斯顿航道属于人工航运通道,并不在该天然水系数据集的覆盖范围内,就算成功下载数据也无法显示你需要的航道水域。

三、更适合的高分辨率地图绘制方案

最简单的实现方式是直接调用高分辨率地图瓦片作为底图,瓦片已经预置了航道、岸线、地物标注等所有你需要的要素,无需自行寻找和加载对应shapefile,修改后的参考代码如下:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
from metpy.plots import USCOUNTIES

ship_lon, ship_lat = -94.80234, 29.31221

# 选择高分辨率瓦片源,也可替换为OpenStreetMap等其他瓦片
request = cimgt.Stamen('terrain-background')
# 缩放级别10-15之间,数值越高分辨率越高,对应下载时间越长
zoom_level = 12

ax = plt.axes(projection=request.crs)
ax.set_extent([-95.5, -94.1, 28.8, 29.8], crs=ccrs.PlateCarree())
# 添加瓦片底图
ax.add_image(request, zoom_level)
# 叠加县界图层
ax.add_feature(USCOUNTIES.with_scale('500k'),facecolor='none', edgecolor='gray',linewidth=0.5)

ax.set_title('Hunter-T事故发生时大致位置',loc='left',fontsize=10,fontweight='bold')
# 标记船只位置,需指定坐标参考系为PlateCarree
plt.scatter(ship_lon, ship_lat, marker='*', color='black',s=12, zorder=10, transform=ccrs.PlateCarree())
plt.text(ship_lon, ship_lat+.03, '船舶位置', fontsize=5, fontweight='bold',horizontalalignment='center', transform=ccrs.PlateCarree())

plt.savefig(fname='vessel_location.png',bbox_inches='tight', dpi=600)
plt.close()

如果你需要纯矢量的航道要素,可以自行下载美国国家海洋和大气管理局(NOAA)发布的美国沿海航运通道shapefile,通过Cartopy的Reader接口加载到图中即可。

内容的提问来源于stack exchange,提问作者bayouwxman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 17:39:03