Issue
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:
- I assume that
lon[1],lon[1]
is one point of the boarder. Obviously there are a lot more. - How do I find out what the file says. What is
shapeType
? What are the numbers inside ofrecords
? - The coordinates seem not to be in "standard" WGS84? What is it?
- Is there a quick way to paint the polygon?
Thanks a lot!!!
Solution
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. https://datadependence.com/2016/06/creating-map-visualisations-in-python/
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.
Answered By - Jamal Moir
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.