Skip to main content
 首页 » 编程设计

python之从 Natural Earth 和 OpenStreetMap for Cartopy 下载数据

2025年05月04日37tintown

我正在尝试使用 cartopy 绘制多张 map ,我想离线使用它们。 Cartopy有一个数据目录,

import cartopy.config 
cartopy.config 
{'data_dir': '/home/user/.local/share/cartopy', 
'downloaders': {('shapefiles', 
'gshhs'): <cartopy.io.shapereader.GSHHSShpDownloader at 0x7f3ee33ee7d0>, 
('shapefiles', 
 'natural_earth'): <cartopy.io.shapereader.NEShpDownloader at 0x7f3ee33ee710>}, 
'pre_existing_data_dir': '', 
'repo_data_dir': '/home/user/bin/virtualenvs/mobi/local/lib/python2.7/site-packages/cartopy/data'} 

所以我相信我可以从 Natural Earth 下载 map 地点。我怎样才能在这个目录中构建这些数据,这样 cartopy 就不会使用互联网来绘图?我如何才能对 OpenStreetMap 数据执行相同的操作?

请您参考如下方法:

(仅部分回答)

在 Natural Earth 网站 http://www.naturalearthdata.com/downloads/,您可以找到所有可下载的文件。 例如,此链接提供低分辨率数据:http://www.naturalearthdata.com/downloads/110m-physical-vectors/

该页面上的其中一个数据文件具有以下链接地址: http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/physical/ne_110m_coaSTLine.zip

这段代码将下载该文件(如果它在计算机上不可用):-

import cartopy 
fname = cartopy.io.shapereader.natural_earth( \ 
  resolution='110m', \ 
  category='physical', \ 
  name='110m_coastline') 

fname 是下载文件的完整路径名。

cartopy无需自行安排下载位置。它已经有默认位置,您可以通过以下方式找到:

cartopy.config['data_dir']   # usually 'C:\\Users\\username\\.local\\share\\cartopy' 

您可以查看下载的文件并查看它们在该位置的结构。

下次当您使用 cartopy 函数 cartopy.io.shapereader.natural_earth(使用默认配置)时,它将使用可用的本地文件。