Best way to get a map of a city using Basemap?

fosho picture fosho · Jul 6, 2015 · Viewed 7.5k times · Source

I am trying to use Basemap to display a map of a city, for example San Francisco, in python. I have tried the following:

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
# llcrnrlat,llcrnrlon,urcrnrlat,urcrnrlon
# are the lat/lon values of the lower left and upper right corners
# of the map.
# lat_ts is the latitude of true scale.
# resolution = 'c' means use crude resolution coastlines.
m = Basemap(projection='merc',llcrnrlat=37.79,urcrnrlat=37.81,\
        llcrnrlon=-122.42,urcrnrlon=-122.4,lat_ts=20,resolution='c')
m.drawcoastlines()
m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,91.,30.))
m.drawmeridians(np.arange(-180.,181.,60.))
m.drawmapboundary(fill_color='aqua')
plt.title("Mercator Projection")
plt.show()

However this does not work and just shows blue where the map is meant to be. So how can I get a map of San Francisco using python?

Answer

rth picture rth · Jul 6, 2015

Your coordinates must be wrong: it shows blue because you are zooming on the ocean somewhere.

Besides, this code will only draw the coastline as explained in the documentation. To get the map of a city, you actually need to load the corresponding data using one of the available back-ends. For instance, you could query the data from a API service such as ArcGIS, etc, with the corresponding method.