han*_*esk 8 python polygon matplotlib geospatial shapefile
我正在尝试使用python中的matplotlib在世界地图上绘制填充的多边形国家.
我有一个shapefile,每个国家都有国家边界坐标.现在,我想将这些坐标(对于每个国家/地区)转换为带有matplotlib的多边形.不使用Basemap.不幸的是,零件交叉或重叠.是否有工作,可能使用点到点的距离..或重新排序?

han*_*esk 14
哈!我发现了,怎么......我完全忽略了,sf.shapes [i] .parts信息!然后归结为:
# -- import --
import shapefile
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
# -- input --
sf = shapefile.Reader("./shapefiles/world_countries_boundary_file_world_2002")
recs = sf.records()
shapes = sf.shapes()
Nshp = len(shapes)
cns = []
for nshp in xrange(Nshp):
cns.append(recs[nshp][1])
cns = array(cns)
cm = get_cmap('Dark2')
cccol = cm(1.*arange(Nshp)/Nshp)
# -- plot --
fig = plt.figure()
ax = fig.add_subplot(111)
for nshp in xrange(Nshp):
ptchs = []
pts = array(shapes[nshp].points)
prt = shapes[nshp].parts
par = list(prt) + [pts.shape[0]]
for pij in xrange(len(prt)):
ptchs.append(Polygon(pts[par[pij]:par[pij+1]]))
ax.add_collection(PatchCollection(ptchs,facecolor=cccol[nshp,:],edgecolor='k', linewidths=.1))
ax.set_xlim(-180,+180)
ax.set_ylim(-90,90)
fig.savefig('test.png')
Run Code Online (Sandbox Code Playgroud)
那么它看起来像这样:

| 归档时间: |
|
| 查看次数: |
19153 次 |
| 最近记录: |