Skip to main content
1 of 2
james
  • 103
  • 1
  • 3

How to import a background image for overlay with geopandas

I recently downloaded the shapefiles from San Francisco from Mapzen. Now I am able to display the streets, but I would like to have a real map as a background image, such that it is easier to make the link between the real roads from San Francisco and the ones shown by Geopandas.

Here is the code to show the streets with Geopandas:

  1. First Download the shaepfile from Mapzen: https://s3.amazonaws.com/metro-extracts.mapzen.com/san-francisco_california.imposm-shapefiles.zip

  2. Open a Python notebook and add those lines:

    #Useful starting lines %matplotlib inline import numpy as np import matplotlib.pyplot as plt %load_ext autoreload %autoreload 2

  3. Load the street map:

    shapefile_dir = 'the path to your directory where you store the shapefile'

    shapefile_name ='san-francisco_california_osm_roads_gen0.shp'

    shapefile_roads = os.path.join(shapefile_dir, shapefile_name)

  4. Import as a geopandas dataframe:

    import os

    import geopandas as gpd

    df = gpd.GeoDataFrame.from_file(shapefile_roads)

  5. Simply plot the street network:

    df.plot();

The output should be:

enter image description here

Now I would like to have a real map (Color map) as a background. How can I do it ?

james
  • 103
  • 1
  • 3