作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 here 获得了邮政编码周长的 shapefile并想将它们绘制在 Cartopy map 上,就像我在 this example 中所做的那样.
根据消息来源,该数据采用 EPSG 4326 坐标系。当我尝试绘制数据时
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
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 = (mapsize,mapsize))
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, 8, zorder = 0)
# Add city borders
shape_feature = ShapelyFeature(Reader(shapefile).geometries(), ccrs.epsg(4326),
linewidth = 2, facecolor = (1, 1, 1, 0),
edgecolor = (0.3, 0.3, 0.3, 1))
ax.add_feature(shape_feature, zorder = 1)
我看到以下错误:
ValueError: EPSG code does not define a projection
可能相关 - 当我查看 ccrs.epsg()
函数时,它表示不支持此 EPSG 代码
help(ccrs.epsg)
Help on function epsg in module cartopy.crs:
epsg(code)
Return the projection which corresponds to the given EPSG code.
The EPSG code must correspond to a "projected coordinate system",
so EPSG codes such as 4326 (WGS-84) which define a "geodetic coordinate
system" will not work.
Note
----
The conversion is performed by querying https://epsg.io/ so a
live internet connection is required.
鉴于这个结果,我还尝试使用ccrs.Geodetic()
:
# Add city borders
shape_feature = ShapelyFeature(Reader(shapefile).geometries(), ccrs.Geodetic(),
linewidth = 2, facecolor = (1, 1, 1, 0),
edgecolor = (0.3, 0.3, 0.3, 1))
ax.add_feature(shape_feature, zorder = 1)
但这也无法显示邮政编码周界,并显示此警告消息:
UserWarning: Approximating coordinate system <cartopy._crs.Geodetic object at 0x1a2d2375c8> with the PlateCarree projection.
'PlateCarree projection.'.format(crs))
我也尝试过ccrs.PlateCarree()
,但没有运气。请帮忙!
最佳答案
要将不同来源的数据绘制在一起,必须为每个数据集声明正确的坐标引用系
。对于 shapefile,您可以在其随附的 xxx.prj
文件中找到它。
这是工作代码:
import cartopy.io.img_tiles as cimgt
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
shapefile_name= "./data/ZIPCODE.shp"
mapwidth, mapheight = 8, 8
pad = 0.25
stamen_terrain = cimgt.Stamen('terrain-background')
stm_crs = stamen_terrain.crs
fig = plt.figure(figsize = (mapwidth, mapheight))
ax = fig.add_subplot(1, 1, 1, projection=stm_crs) #world mercator
# Set extent of map
ax.set_extent([-123.3-pad, -121.5+pad, 37.05-pad, 38.75+pad], crs=ccrs.Geodetic())
# Plot base map
ax.add_image(stamen_terrain, 8, zorder=0)
# Add polygons from shapefile
# Note: the use of `ccrs.epsg(26910)`
shape_feature = ShapelyFeature(Reader(shapefile_name).geometries(), ccrs.epsg(26910))
# You can choose one of the 2 possible methods to plot
# ... the geometries from shapefile
# Styling is done here.
method = 1
if method==1:
# iteration is hidden
ax.add_feature(shape_feature, facecolor='b', edgecolor='red', alpha=0.4, zorder = 15)
if method==2:
# iterate and use `.add_geometries()`
# more flexible to manipulate particular items
for geom in shape_feature.geometries():
ax.add_geometries([geom], crs=shape_feature.crs, facecolor='b', edgecolor='red', alpha=0.4)
plt.show()
输出图:
关于python - 使用 ccrs.epsg() 使用 EPSG 4326 坐标系绘制邮政编码周长形状文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57030906/
我从 here 获得了邮政编码周长的 shapefile并想将它们绘制在 Cartopy map 上,就像我在 this example 中所做的那样. 根据消息来源,该数据采用 EPSG 4326
我是一名优秀的程序员,十分优秀!