3

I have the following code:

from mpl_toolkits.basemap import Basemap
map = Basemap(projection='merc', lat_0=50, lon_0=4,
    resolution = 'l', area_thresh = 0.1,
    llcrnrlon=4, llcrnrlat=50,
    urcrnrlon=40, urcrnrlat=60)

map.drawcoastlines(linewidth=0.15)
map.drawcountries(linewidth=0.15)
map.fillcontinents(color='brown',lake_color='white')
map.drawmapboundary(fill_color='white')

enter image description here

And on top of this map I want to display a shapefile that consists of only one polygon. The polygon defines a closed area. I've found different tutorials on how to manually add polygons or plot multiple polygons from a shapefile, but I am not able to do it for my case. The shapefile attribute table is composed of only two fields: 'c' and 'area'.

For now I have arrived to this

import shapefile

s = shapefile.Reader(filepath,'c',drawbounds=False)
shapes = s.shapes()
records = s.records()
for record, shape in zip(records,shapes):
    lons,lats = zip(*shape.points)
    data = np.array(map(lons, lats)).T
x, y =map(lons,lats) 
1

1 Answer 1

1

Had the same issue, but it's so simple that you never thought about doing it with the many tutorials and modules and kind of similar issues on the net:

map.readshapefile('luthuania', 'any_name_you_like', drawbounds=True)

so for your example:

from mpl_toolkits.basemap import Basemap
map = Basemap(projection='merc', lat_0=50, lon_0=4,
    resolution = 'l', area_thresh = 0.1,
    llcrnrlon=4, llcrnrlat=50,
    urcrnrlon=40, urcrnrlat=60)

map.readshapefile('luthuania', 'any_name_you_like', drawbounds=True, linewidth=2, color='b')

map.drawcoastlines(linewidth=0.15)
map.drawcountries(linewidth=0.15)
map.fillcontinents(color='brown',lake_color='white')
map.drawmapboundary(fill_color='white')

Which gives

lithuania

The module shapefile, by the way is used by Basemap under the hood: see C:\Python33\Lib\site-packages\mpl_toolkits\basemap\shapefile.py

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.