我正在学习如何在基础 Python 映射库中使用地理空间数据,这是我第一次使用 Shapely/Polygons/Basemap/etc。
我有一组多边形来描述给定区域的社区。如果我只想简单地绘制它们,下面的函数可以解决问题(mymap 只是一个 Basemap 对象,hood_map 是邻域及其相关多边形的集合):
def drawNeighborhoods(mymap,hood_map):
for hood in hood_map.neighborhoods:
lon,lat = hood.polygon.exterior.coords.xy
x,y = mymap(lon,lat)
mymap.plot(x,y,'-k',color='lightgrey')
这很好用,我得到了地图上每个社区的轮廓。但是,我现在想根据每个社区的外部数据对社区进行着色(例如,如果没有比萨店,则为白色,如果有 100 多家比萨店,则为红色,等等)。为此,我创建了一个颜色图、颜色条等。
cmap = cm.get_cmap('Reds')
norm = Normalize(vmin=0, vmax=max(number_of_pizza_joints))
cb = ColorbarBase(ax, cmap=cmap, norm=norm)
然后我这样做(基于此示例https://gist.github.com/urschrei/6436526):
def drawNeighborhoods(mymap,hood_map):
patches = []
for hood in hood_map.neighborhoods:
color = cmap(norm(hood.number_of_pizza_joints))
lon,lat = hood.polygon.exterior.coords.xy
x,y = mymap(lon,lat)
poly = Polygon(zip(x,y))
patches.append(PolygonPatch(poly,fc=color, ec='#555555', alpha=0.5, zorder=4))
ax.add_collection(PatchCollection(patches, match_original=True))
在这里,我收到一个错误,即:
Traceback (most recent call last):
File "colorHoodsByPizza.py", line 103, in <module>
drawNeighborhoods(mymap,hood_map)
File "colorHoodsByPizza.py", line 52, in drawNeighborhoods
patches.append(PolygonPatch(poly,fc='#cc00cc', ec='#555555', alpha=0.5, zorder=4))
File "/Users/zach/anaconda2/lib/python2.7/site-packages/descartes/patch.py", line 87, in PolygonPatch
return PathPatch(PolygonPath(polygon), **kwargs)
File "/Users/zach/anaconda2/lib/python2.7/site-packages/descartes/patch.py", line 53, in PolygonPath
ptype = polygon["type"]
TypeError: 'Polygon' object has no attribute '__getitem__'
我的猜测是getitem错误很可能是因为polygon["type"]不存在,应该是polygon.type;然而,这是在预制库“笛卡尔”中,所以我很困惑为什么会出现这个错误。我试过四处寻找笛卡尔中发生的这个错误,但找不到任何线索;所以我假设我在做一些愚蠢的事情。有什么见解吗?