我正在尝试向Cartopy底图中添加一些形状文件。事实证明,某些形状文件有效,而其他形状文件无效。我整天用Google搜索,没有任何运气。你能帮忙吗? 下面是我正在使用的代码。可以在此处下载两个形状文件:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io.img_tiles import OSM
from cartopy.io import shapereader
#%% ======== map paramters =================
map_extent_eastings=(378000,390000)
map_extent_northings=(6427500,6443000)
#%%
plt.subplots(1,1,figsize=(10,10))
ax = plt.axes(projection=ccrs.epsg(28350))
ax.set_extent([map_extent_eastings[0],map_extent_eastings[1],map_extent_northings[0],map_extent_northings[1]],crs=ccrs.epsg(28350))
tiler = OSM()
cur_CRS = tiler.crs
zoom = 8 #12 for the final
ax.add_image(tiler, zoom, interpolation='bilinear' )
#============= Figure out why this deosn't work ===================
### this one doesn't work
ax.add_geometries(shapereader.Reader('data/AdditionalPumping_south_v2_test.shp').geometries(),
ccrs.epsg(28350),facecolor='none',edgecolor='r',linewidth=1.5,zorder=10)
# This one works
ax.add_geometries(shapereader.Reader('data/MAR_locations_v2.shp').geometries(),
ccrs.epsg(28350),facecolor='none',edgecolor='r',linewidth=1.5,zorder=10)
plt.show()
=============================
答案 0 :(得分:0)
我检查了您已上传到保管箱的shapefile。原来shapefile没有被正确读取。
当我打印出 MAR_locations_v2.shp 的记录时,这就是我得到的
记录(MULTIPOLYGON((((386523.5418971451 6432084.711430285,386523.5418971451 6432184.711430287,386573.541897146 6432184.711430287,386573.541897146 6432084.711430285,386523.5418971451 6432084.711430285))),{'Name':''},)
记录(MULTIPOLYGON((((386405.7078714892 6438043.356735789,386405.7078714892 6438143.356735789,386505.7078714911 6438143.356735789,386505.7078714911 6438043.356735789,386405.7078714892 6438043.356735789))),{'Name':''},)
当我打印 AdditionalPumping_south_v2_test.shp 的记录时,我得到了
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':1,'row':465.0,'column_':94.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':2,'row':469.0,'column_':97.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':3,'row':470.0,'column_':94.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':4,'row':473.0,'column_':106.0,'Shape_Leng':80.0,'Shape_Area':-400.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':5,'row':475.0,'column_':93.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':6,'row':475.0,'column_':97.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':7,'row':480.0,'column_':93.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':8,'row':482.0,'column_':97.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':9,'row':482.0,'column_':110.0,'Shape_Leng':80.0,'Shape_Area':-400.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':10,'row':485.0,'column_':92.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':11,'row':489.0,'column_':97.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':12,'row':490.0,'column_':92.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':13,'row':491.0,'column_':114.0,'Shape_Leng':80.0,'Shape_Area':-400.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':14,'row':495.0,'column_':91.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':15,'row':496.0,'column_':97.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':16,'row':500.0,'column_':91.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':17,'row':500.0,'column_':118.0,'Shape_Leng':80.0,'Shape_Area':-400.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':18,'row':503.0,'column_':97.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':19,'row':509.0,'column_':120.0,'Shape_Leng':80.0,'Shape_Area':-400.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':20,'row':510.0,'column_':96.0,'Shape_Leng':240.0,'Shape_Area':-2000.0},)
Record(GEOMETRYCOLLECTION EMPTY,{'OBJECTID':21,'row':518.0,'column_':119.0,'Shape_Leng':80.0,'Shape_Area':-400.0},)
这不是对问题的完整答案,但是它回答了为什么您不了解情节的部分。您可能希望以其他方式保存shapefile以便进行绘制。
答案 1 :(得分:0)
Github上的Cartopy社区已找到解决方案。对于对此感兴趣的任何人,请参见https://github.com/SciTools/cartopy/issues/1217