如何在 python 的地图上显示 shapefile?
How to show shapefile on map in python?
我目前正在使用 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()
这是使用 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")
我目前正在使用 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()
这是使用 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")