- 积分
- 66436
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-7-23
- 最后登录
- 1970-1-1
|
发表于 2021-8-25 16:09:46
|
显示全部楼层
本帖最后由 edwardli 于 2021-8-25 16:12 编辑
我打开了Province.shp,内容和你的一样。
def shp2clip(originfig,ax,shpfile,region, clabel = None, vcplot = None):
sf = shapefile.Reader(shpfile,encoding='ANSI')
vertices = []
codes = []
for shape_rec in sf.shapeRecords():
####这里需要找到和region匹配的唯一标识符,record[]中必有一项是对应的。
if shape_rec.record[1] == region: #####在country1.shp上,对中国以外的其他国家或地区进行maskout
# if shape_rec.record[7] in region: #####在bou2_4p.shp上,对中国的某几个省份或地区之外的部分进行maskout
pts = shape_rec.shape.points
prt = list(shape_rec.shape.parts) + [len(pts)]
for i in range(len(prt) - 1):
for j in range(prt, prt[i+1]):
vertices.append((pts[j][0], pts[j][1]))
codes += [Path.MOVETO]
codes += [Path.LINETO] * (prt[i+1] - prt -2)
codes += [Path.CLOSEPOLY]
clip = Path(vertices, codes)
plate_carree_transform = ccrs.PlateCarree()._as_mpl_transform(ax)
clip = PathPatch(clip, transform=plate_carree_transform)
if vcplot:
if isinstance(originfig,Iterable):
for ivec in originfig:
ivec.set_clip_path(clip)
else:
originfig.set_clip_path(clip)
else:
for contour in originfig.collections:
# for contour in originfig:
contour.set_clip_path(clip)
print(0)
if clabel:
clip_map_shapely = ShapelyPolygon(vertices)
for text_object in clabel:
if not clip_map_shapely.contains(ShapelyPoint(text_object.get_position())):
text_object.set_visible(False)
return clip
clip = shp2clip(cs, ax, 'D:/pycode/shp/Province.shp','河北省')
|
|