底图Shapefile可视化


问题内容

用Basemap创建一些地图后,我变得很热情。我想集成shapefile信息,比如说一个多边形,但是存在一些问题。我在这里下载了巴伐利亚村庄的寄宿生:

https://www.arcgis.com/home/item.html?id=b752861d1a08489b9a40337668d4367e

现在,我想为雷根斯堡集成一个多边形。我可以使用此代码获取信息,但是我有一些问题

#!/usr/bin/env python

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import shapefile

map = Basemap(projection='merc',
              resolution='l',
              area_thresh=0.01,
              llcrnrlon=9.497681, llcrnrlat=47.827908,
              urcrnrlon=12.683716, urcrnrlat=50.408517)

map.drawcountries(color="gray")
map.fillcontinents(color='#c8dfb0', lake_color='#53BEFD')
map.drawmapboundary(color='black', linewidth=0.5, fill_color='#53BEFD')

sf = shapefile.Reader("BY_Gemeinden/BY_Gemeinden_WM.shp")
shapes = sf.shapes()
records = sf.records()
for record, shape in zip(records, shapes):
    if record[3] == "Regensburg":
        print(shape.shapeType)
        lons, lates = zip(*shape.points)
        print(record)
        print(lons)
        print(lates)

plt.savefig("foo.eps")

输出如下:

5
[797, 'BY', '6001', 'Regensburg', 'Regensburg', 'Freistaat
Bayern','Oberpfalz', '09362000', '6.42920556908e+004',
'8.05927936478e+007']
1350746.04018
6287601.12826

我的问题:

  1. 我认为这lon[1],lon[1]是寄宿生的一分。显然还有更多。
  2. 我如何找出文件中的内容。什么shapeType啊 里面的数字是records多少?
  3. 坐标似乎不在“标准” WGS84中?它是什么?
  4. 有没有一种快速的方法可以绘制多边形?

非常感谢!!!


问题答案:

我最近写了一篇有关使用Basemap制作地图的博客文章,其中我使用shapefile在英格兰和威尔士的邮政编码区域进行绘制和着色。这可能会有帮助。http://www.jamalmoir.com/2016/06/creating-
map-visualisations-in-python.html

基本上,您可以使用shapefile创建一个PatchCollection,然后为PatchCollection着色。然后将其添加到地图中,鲍勃是您的叔叔。

m.readshapefile('data/uk_postcode_bounds/Areas', 'areas')

df_poly = pd.DataFrame({
        'shapes': [Polygon(np.array(shape), True) for shape in m.areas],
        'area': [area['name'] for area in m.areas_info]
    })
df_poly = df_poly.merge(new_areas, on='area', how='left')

cmap = plt.get_cmap('Oranges')   
pc = PatchCollection(df_poly.shapes, zorder=2)
norm = Normalize()

pc.set_facecolor(cmap(norm(df_poly['count'].fillna(0).values)))
ax.add_collection(pc)

在此示例中,我使用新房子的数量为每个区域着色,但是您可以做自己喜欢的事情。