簡體   English   中英

在Cartopy地圖上繪制shapefile城市邊界

[英]Plot shapefile city borders on top of cartopy map

我正在嘗試使用在此處獲得的shapefile並在此示例之后,在Cartopy地形圖的頂部繪制灣區城市/城鎮邊界的輪廓。 由於某些原因,即使我通過zorder指定邊框位於頂部,邊框也不會顯示。 我想念什么嗎?

# import functions
import matplotlib.pyplot as plt
import cartopy.io.img_tiles as cimgt
import cartopy.crs as ccrs
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature

# Create a Stamen terrain background instance
stamen_terrain = cimgt.Stamen('terrain-background')
fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(1, 1, 1, projection=stamen_terrain.crs)

# Set range of map, stipulate zoom level
ax.set_extent([-122.7, -121.5, 37.15, 38.15], crs=ccrs.Geodetic())
ax.add_image(stamen_terrain, 12, zorder = 0)

# Add city borders - not working
filename = r'./shapefile/ba_cities.shp' # from https://earthworks.stanford.edu/catalog/stanford-vj593xs7263
shape_feature = ShapelyFeature(Reader(filename).geometries(), ccrs.PlateCarree(), edgecolor='black')
ax.add_feature(shape_feature, zorder = 1)
plt.show()

沒有shapefile邊框!為什么?

正如@ImportanceOfBeingErnest和@swatchai所建議的那樣, ShapelyFeature cartopy.feature.ShapelyFeature()中的CRS(坐標參考系統)參數不正確。

可以在shapefile隨附的.xml文件之一中找到正確的EPSG(歐洲石油調查組織?)代碼:

   <gco:CharacterString>26910</gco:CharacterString>
</code>
<codeSpace>
   <gco:CharacterString>EPSG</gco:CharacterString>

並將其作為ShapelyFeature()的第二個參數傳遞,即可獲取shapefile正確繪制城市邊界:

# Add city borders
filename = r'./shapefile/ba_cities.shp'
shape_feature = ShapelyFeature(Reader(filename).geometries(), ccrs.epsg(26910), 
                               linewidth = 1, facecolor = (1, 1, 1, 0), 
                               edgecolor = (0.5, 0.5, 0.5, 1))
ax.add_feature(shape_feature)
plt.show()

現在繪制城市邊界

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM