![](/img/trans.png)
[英]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.