I want to create a map with several given points in Python. For this I want to use Basemap from matplotlib. It works well, but I don't know how to get a proper background map.
How can I import an OSM map? Or should I use a different mapping package? I just want to create a raster map and save it as png.
This not my solution; I have pasted it from the question because the asker doesn't have enough reputation to answer his own question.
I found a solution:
Using
imshow
within Basemap includes an png into the plot as background image. To obtain the right background image, I used the export feature of OSM with boundaries taken from the Basemap constructor:m = Basemap(llcrnrlon=7.4319, urcrnrlat=52.0632, urcrnrlon=7.848, llcrnrlat=51.8495, resolution='h', projection='merc') im = plt.imread('background.png') m.imshow(im, interpolation='lanczos', origin='upper')
I found some accessible basemap imagery from NASA GIBS tileserver. You might be able to use the same method for other tileservers.
http://earthdata.nasa.gov/wiki/main/index.php/GIBS_Supported_Clients#Script-level_access_to_imagery
Thi Uses GDAL's gdal_translate in a python subshell:
import subprocess
import matplotlib.pyplot
import mpl_toolkits.basemap
l,u,r,d=(7.4319,52.0632,7.848,51.8495)
subprocess.call ('gdal_translate -of GTiff -outsize 400 400 -projwin {l} {u} {r} {d} TERRA.xml Background.tif'.format(l=l,u=u,r=r,d=d),shell=True )
im=matplotlib.pyplot.imread('Background.tif')
m = mpl_toolkits.basemap.Basemap(llcrnrlon=l, urcrnrlat=u, urcrnrlon=r, llcrnrlat=d,
resolution='h', projection='merc')
m.imshow(im, interpolation='lanczos', origin='upper')
matplotlib.pyplot.show()
This needs the TERRA.xml file from the above link, though you can inline the XML as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With