【问题标题】:How to show shapefile on map in python?如何在 python 中的地图上显示 shapefile?
【发布时间】:2020-06-16 07:30:25
【问题描述】:

我目前正在使用 python 并使用底图绘制越南的地图,如下所示: Vietnam's Map

我的形状文件:enter image description here

我想使用我已经拥有的 shapefile 显示所有岛屿组或小岛屿,但我不知道要阅读 shapefile。我在 Internet 上尝试了一些代码,但没有成功。我希望你们能帮助我。我真的很感激。非常感谢。 这是我的代码:

import cartopy.crs as ccrs
from cartopy.feature import ShapelyFeature
from cartopy.io.shapereader import Reader
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
plt.figure(figsize=(12,7))
m = Basemap(projection='mill',
       llcrnrlat = 7,
       urcrnrlat = 25,
       llcrnrlon = 90,
       urcrnrlon = 120,
       resolution = 'l')
m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.fillcontinents(color='None',lake_color='#FFFFFF')
m.drawmapboundary(color='k', linewidth=1.0, fill_color=None, zorder=None, ax=None)
parallels = np.arange(0.,81,10.)
# labels = [left,right,top,bottom]
m.drawparallels(parallels,labels=[False,True,True,False])
meridians = np.arange(10.,351.,10.)
m.drawmeridians(meridians,labels=[True,False,False,True])

fig, ax = plt.subplots(subplot_kw=dict(projection=ccrs.PlateCarree()))
ax.set_extent([90, 120, 25, 7])
# Set map extent
In_st_shp = 'E:\koppen-master\haidao\haidao.shp'
# provide path of shapefile
state_feature = ShapelyFeature(Reader(In_st_shp).geometries(),
                               crs=ccrs.PlateCarree(), edgecolor='k')
ax.add_feature(state_feature, facecolor="none")

plt.show()

haidao.shp

【问题讨论】:

  • 我经常使用 Cartopy 来做这件事,我可以帮助你。让我知道你是否可以安装 cartopy。
  • 顺便说一句,我找到了这个底图教程。 basemaptutorial.readthedocs.io/en/latest/shapefile.html
  • 谢谢,但我无法从 cmd 下载 cartopy。它有一些问题。

标签: python shapefile


【解决方案1】:

这是使用 cartopy 的工作代码。 请检查地图范围并在代码中设置shapefile的路径。

import shapefile
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.feature import ShapelyFeature
from cartopy.io.shapereader import Reader
import matplotlib.pyplot as plt
fig, ax = plt.subplots(subplot_kw=dict(projection=ccrs.PlateCarree()))
ax.set_extent([102, 111, 7, 23])
ax.coastlines(color='black', linewidth=1, resolution='50m')
ax.add_feature(cfeature.BORDERS.with_scale('50m'),
               linestyle='-', alpha=.5)
# Set map extent
In_st_shp = 'Path_to_shape.shp'
# provide path of shapefile
state_feature = ShapelyFeature(Reader(In_st_shp).geometries(),
                               crs=ccrs.PlateCarree(), edgecolor='r')
ax.add_feature(state_feature, facecolor="r")

【讨论】:

  • 我有一些错误:文件“E:\koppen-master\CodeDev\123.py”,第 5 行,在 中导入 cartopy.crs 作为 ccrs 文件“C:\Users\Admin \AppData\Local\Programs\Python\Python38-32\lib\site-packages\cartopy_init_.py”,第 96 行,在 中导入 cartopy.crs 文件“C:\Users\ Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cartopy\crs.py",第 2418 行,在
  • 好像 cartopy 没有正确安装。导入 cartopy 时出错。
  • 哦,我修复了它,它有这个错误:文件“E:\koppen-master\CodeDev\123.py”,第 29 行,在 图中,ax = plt.subplots(figsize =rois[rois]['figsize'], NameError: name 'rois' is not defined
  • 感谢您指出这一点。那是我系统特有的东西。我已经编辑了代码。请使用新的。
  • 这是可行的,但结果似乎很奇怪。顺便说一句,非常感谢您的帮助
猜你喜欢
  • 2021-07-10
  • 2013-08-15
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
相关资源
最近更新 更多