Fetching Google Street View Images of any location with Python and OSMNx

4 minute read

Published:

In this post, i will describe how to collect Google Street view images of any location with OSMNx. For the tutorial, you will need OSMNx and access to Google Street View. If you have a working installation of OSMNx and have accessed the Google Street View api before, skip to Implementation or to the GitHub repo for the code.

OSMNx

OSMNx is a library for reading and manipulating OpenStreetMap street networks developed for Python by Geoff Boeing. You can read more about it and how to install it here. It is pretty straightforward.

As at the time of writing, i tried to install on another computer and ran into some dependency issues. Hence, i recommend conda install -c conda-forge osmnx for the installation. Also, ensure that your conda channel settings prioritizes conda-forge using conda config --add channels conda-forge, you can also go ahead to delete the defaults channel in the .condarc file.

Assuming that you have successfully installed the package, you can go ahead to download the street network for the location you are interested in. We will be using that of Dublin City, Ireland as follows:


import geopandas as gpd
import osmnx as ox

G_dublin = ox.graph_from_place('Dublin, County Dublin, Leinster, Ireland', network_type='drive') #drivable roads
ox.save_graph_shapefile(G_dublin, filename='dublin_network') #save to disk

We read only the drivable roads for dublin (see the OSMNx documentation for other options) and save it to disk for use later on.

Google Street View

Google street view is a feature of GM that holds a panoramic views of streets around the world. The current coverage of GSV varies around the world (see here). We will access GSV via the API, but first you need a key, you can get that here. Also ensure that you enable GSV api access under dashboard > Enable APIS AND SERVICES. As long as you are not exceeding 25k requests a day, the api key should be enough for access. Otherwise, you will need the key with a digital signature (ref).

Since GSV keeps a panoramic view of the streets, it is important to bear some concepts in mind before making your queries. These concepts are field of view (fov), heading, image size, pitch. You can read about them here.

If you just want to dive in. You can play around with the values for each parameter. They are pretty intuitive.

To access the API with python, you need to install the library from this link.

Implementation

With all the packages and tools collected, we move forward to the actual implementation. First we import the necessary libraries:

import google_streetview.api
import urllib, os
import urllib.parse

We will use geopandas to read the street network written to disk above.

As OSMNx is dependent on geopandas, you should have it installed already. If for some reason you don’t. install it with conda or pip.


dublin = gpd.read_file('data/dublin_network/edges/edges.shp')

Next, we take the centroids of each street edge components and write to a column streetCentroid in the geodataframe: dublin. This will be used as a reference point for all the streets.

def getStreetCentroid(row):
    obj = row['geometry']
    centroid = obj.centroid
    return centroid

dublin['streetCentroid']  = dublin.apply(getStreetCentroid,axis=1)

Now, the actual image collection. We consider just three headings 0,125 and 250 degrees here. Notice the values for size, pitch, fov. You can change as you wish in skeleton. In the actual implementation, 3 images are collected for each street corresponding to the headings specified.

orientation = ['0','125','250']

def GetStreet(Add,SaveLoc,orientation):
    skeleton = 'https://maps.googleapis.com/maps/api/streetview?size=640x640&fov=120&pitch=-0.76&heading='+orientation+'&location='
    ThisUrl = skeleton + urllib.parse.quote(Add) + "&key="+"YOUR-API_KEY"  
    filePath = Add +'_facing='+orientation+'.jpg'
    urllib.request.urlretrieve(ThisUrl, os.path.join(SaveLoc,filePath))

try:
	for index, row in dublin.iterrows(): 
	    points = (dublin.loc[index,'streetCentroid'])
	    pointList = list(points.coords)
	    locationCoordinate = str(pointList[0][1])+','+str(pointList[0][0])
	    for j in orientation:
	    	# change the value in SaveLoc to the folder you want to save the images
	        GetStreet(Add=locationCoordinate,SaveLoc = 'dublinImages',orientation = j)
	print('Finished downloading images for dublin')
except:
	print('error')

Conclusion

If this helps you or have some other queries, please send an email or leave a comment below.

Leave a Comment