Basemap Shapefile visualizing

浪子不回头ぞ 提交于 2019-12-01 12:03:35

问题


After creating some maps with Basemap I got enthusiastic. I want to integrate shapefile information, let's say a polygon, but there are some problems. I downloaded the boarders of bavarian villages here:

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

Now I want to integrate a polygon for, let's say, Regensburg. I am able to fetch the information with this code, but I have a few problems

#!/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")

The output looks like this:

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

My Questions:

  1. I assume that lon[1],lon[1] is one point of the boarder. Obviously there are a lot more.
  2. How do I find out what the file says. What is shapeType? What are the numbers inside of records?
  3. The coordinates seem not to be in "standard" WGS84? What is it?
  4. Is there a quick way to paint the polygon?

Thanks a lot!!!


回答1:


I recently wrote a blog post on making maps with Basemap and in it I use a shapefile to draw and colour in postcode areas in England and Wales. It might be of some help. http://www.jamalmoir.com/2016/06/creating-map-visualisations-in-python.html

Basically you can create a PatchCollection using your shapefile and then colour the PatchCollection in. Then you add that to your map and Bob's your uncle.

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)

In this example I use the count of new houses to colour each area but you can do what you like.



来源:https://stackoverflow.com/questions/37859994/basemap-shapefile-visualizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!