我正在尝试在 Python 的 shapefile 中绘制某些区域的地图。我的基本方法是这样的:
shp = fiona.open("C:/Users/nils/Documents/Maps/my_shapefile.shp")
bds = shp.bounds
ll = (bds[0], bds[1])
ur = (bds[2], bds[3])
coords = list(ll + ur)
w, h = coords[2] - coords[0], coords[3] - coords[1]
# Make figure instance, add Basemap and CCG boundaries from shapefile
fig, ax = plt.subplots(figsize=(12,10))
m = Basemap(projection="tmerc", lon_0 = -2., lat_0 = 49., ellps="WGS84",
llcrnrlon = coords[0], llcrnrlat = coords[1],
urcrnrlon = coords[2], urcrnrlat = coords[3],
lat_ts = 0, resolution="i", suppress_ticks=True)
m.readshapefile("C:/Users/nils/Documents/Maps/my_shapefile.shp", "Regions")
# Extract polygon coordinates of and names of regions to plot from shapefile
to_plot = ["region_A", "region_B", "region_C"]
poly = []; name = []
for coordinates, region in zip(m.Regions, m.Regions_info):
if any(substr in region["name"] for substr in to_plot):
poly.append(Polygon(coordinates))
name.append(region["name"])
# Turn polygons into patches using descartes
patches = []
for i in poly:
patches.append(PolygonPatch(i, facecolor='#006400', edgecolor='#787878', lw=0.25, alpha=0.5))
# Add PatchCollection to basemap
ax.add_collection(PatchCollection(patches, match_original=True))
现在我的问题是 shapefile 覆盖了更大的地理区域,但我只想绘制该区域的一个子集(例如,我有一个英国 shapefile,但想绘制威尔士所有地区的地图)。现在我可以识别正确的区域并只添加上面示例中的那些补丁,但是 matplotlib 仍然会绘制 shapefile 中所有区域的边界,并且由 fiona 的bounds
方法识别的边界显然与我的补丁子集无关选择。
我有两个与此相关的问题:
如何让 matplotlib 仅绘制 shapefile 中定义的补丁子集的边界?
如何获得补丁子集的边界,类似于 fiona 的
bound
方法对整个 shapefile 所做的那样?