[英]How do I plot coordinates in csv on the map from from 'natural_earth" using cartopy and matplotlib?
[英]Download data from Natural Earth and OpenStreetMap for Cartopy
我正在尝试使用Cartopy绘制几张地图,但我想离线使用它们。 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网站下载地图。 我该如何在此目录中构建这些数据,以便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
: 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
(具有默认配置)时,它将使用本地文件(如果可用)。
我曾经遇到过类似的问题,其中使用cartopy时,plt.gca()。coastlines()触发了从外部服务器下载zip文件的操作,但是由于缺少Internet连接,下载失败。
/home/apps/CARTOPY/0.16.0/lib64/python2.7/site-packages/Cartopy-0.16.0-py2.7-linux-x86_64.egg/cartopy/io/__init__.py:260: DownloadWarning: Downloading: http://naciscdn.org/naturalearth/110m/physical/ne_110m_coastline.zip
warnings.warn('Downloading: {}'.format(url), DownloadWarning)
我手动下载了zip文件,然后将其解压缩到-〜/ .local / share / cartopy / shapefiles / natural_earth / physical。
~/.local/share/cartopy/shapefiles/natural_earth/physical> ls
ne_110m_coastline.README.html ne_110m_coastline.cpg ne_110m_coastline.prj ne_110m_coastline.shx
ne_110m_coastline.VERSION.txt ne_110m_coastline.dbf ne_110m_coastline.shp
然后从某些文件中重命名/删除“ ne_”前缀后,我能够解决此问题。
~/PLOT_TEST> ls ~/.local/share/cartopy/shapefiles/natural_earth/physical/
110m_coastline.cpg 110m_coastline.dbf 110m_coastline.prj 110m_coastline.shp 110m_coastline.shx ne_110m_coastline.README.html ne_110m_coastline.VERSION.txt
我准备了一个代码,您可以在其中从自然地球下载shapefile,然后将其转换为数据框。 请注意,自然地球上的国家/地区坐标为多边形和多多边形格式。 如果要处理的是线串的River,则需要修改代码。
您可能需要使用所需的文件名(例如“ coastlines”)来操纵“名称”。 在以下链接中找到更多信息:
https://www.naturalearthdata.com/downloads/
import cartopy.io.shapereader as shpreader
ne_earth_countries = shpreader.natural_earth(resolution = '10m',
category = 'cultural',
name='admin_0_countries')
countries = shpreader.Reader(ne_earth_countries).records()
def extract_geom_meta(country):
coords = np.empty(shape=[0,2])
for geom in country.geometry:
coords = np.append(coords, geom.exterior.coords, axis=0)
country_name = country.attributes["ADMIN"]
return [country_name, coords]
WorldDF = pd.DataFrame([extract_geom_meta(country) for country in countries],
columns=['countries','coordinates'])
CountryDF = pd.concat([pd.DataFrame(WorldDF['coordinates'][country_idx])
for country_idx in range(len(WorldDF))]).reset_index()
CountryDF['Label'] = CountryDF.apply(lambda row: 1 if row['index'] == 0
else 0,axis=1).cumsum()-1
CountryDF['Country'] = CountryDF['Label'].apply(lambda row: WorldDF.countries[row])
CountryDF.rename(columns={0:'Longitude', 1:'Latitude'},inplace=True)
print(CountryDF.head())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.