Using Python to grab Google Street View imagery

I am at it again with using Google data. For a few projects I was interested in downloading street view imagery data. It has been used in criminal justice applications as a free source for second hand systematic social observation by having people code aspects of disorder from the imagery (instead of going in person) (Quinn et al., 2014), as estimates of the ambient walking around population (Yin et al., 2015), and examining criminogenic aspects of the built environment (Vandeviver, 2014).

I think it is just a cool source of data though to be honest. See for example Phil Cohen’s Family Inequality post in which he shows examples of auctioned houses in Detroit over time.

Using the Google Street View image API you can submit either a set of coordinates or an address and have the latest street view image returned locally. This ends up being abit simpler than my prior examples (such as the street distance API or the places API) because it just returns the image blob, no need to parse JSON.

Below is a simple example in python, using a set of addresses in Detroit that are part of a land bank. This function takes an address and a location to download the file, then saves the resulting jpeg to your folder of choice. I defaulted for the image to be 1200×800 pixels.

import urllib, os

myloc = r"C:\Users\andrew.wheeler\Dropbox\Public\ExampleStreetView" #replace with your own location
key = "&key=" + "" #got banned after ~100 requests with no key

def GetStreet(Add,SaveLoc):
  base = "https://maps.googleapis.com/maps/api/streetview?size=1200x800&location="
  MyUrl = base + urllib.quote_plus(Add) + key #added url encoding
  fi = Add + ".jpg"
  urllib.urlretrieve(MyUrl, os.path.join(SaveLoc,fi))

Tests = ["457 West Robinwood Street, Detroit, Michigan 48203",
         "1520 West Philadelphia, Detroit, Michigan 48206",
         "2292 Grand, Detroit, Michigan 48238",
         "15414 Wabash Street, Detroit, Michigan 48238",
         "15867 Log Cabin, Detroit, Michigan 48238",
         "3317 Cody Street, Detroit, Michigan 48212",
         "14214 Arlington Street, Detroit, Michigan 48212"]

for i in Tests:
  GetStreet(Add=i,SaveLoc=myloc)

Dropbox has a nice mosaic view for a folder of pictures, you can view all seven photos here. Here is the 457 West Robinwood Street picture:

In my tests my IP got banned after around 100 images, but you can get a verified google account which allows 25,000 image downloads per day. Unfortunately the automatic API only returns the most recent image – there is no way to return older imagery nor know the date-stamp of the current image. (You technically could download the historical data if you know the pano id for the image. I don’t see any way though to know the available pano id’s though.) Update — as of 2018 there is now a Date associated with the image, specifically a Year-Month, but no more specific than that. Not being able to figure out historical pano id’s is still a problem as far as I can tell as well.

But this is definitely easier for social scientists wishing to code images as opposed to going into the online maps. Hopefully the API gets extended to have dates and a second API to return info. on what image dates are available. I’m not sure if Mike Bader’s software app is actually in the works, but for computer scientists there is a potential overlap with social scientists to do feature extraction of various social characteristics, in addition to manual coding of the images.

 

Advertisements
Leave a comment

16 Comments

  1. Joe Devlin

     /  March 20, 2018

    Thanks for this, really useful! I initially got – AttributeError: module ‘urllib’ has no attribute ‘urlretrieve’ – so amended the code slightly to import urllib.request and also added request to – urllib.request.urlretrieve(MyUrl, os.path.join(SaveLoc,fi)) – but am now getting – HTTP Error 400: Bad Request – any ideas why that might be?

    Reply
    • Sorry, not sure. Code works as is for me still (just ran it) — I am using V2.7 – are you using V3? Might be a good question for stackoverflow.

      Reply
    • I see the ping-back someone answered your question — it needs to be encoded. I have updated the code snippet, as this is a good idea when passing arbitrary strings into the api, as other characters could cause problems as well.

      Reply
      • Joe Devlin

         /  March 23, 2018

        Thanks Andrew! I was thinking these images would be good to use for machine learning, maybe to try and identify certain stores/brands/signs or something like that. What do you think? Have you heard of anything like that being done? Also, do you know how I can remove the link to my facebook profile from these posts?

    • IANAL, so view the terms of service for your own application to see if you think you are OK. I cited a few applications that are similar to machine vision applications in this post already (Yin paper and work of Mike Bader), another I am familiar with is this Streetscore, http://ieeexplore.ieee.org/document/6910072/?tp=&arnumber=6910072&url=http:%2F%2Fieeexplore.ieee.org%2Fxpls%2Fabs_all.jsp%3Farnumber%3D6910072.

      I just manually took out the link to your facebook profile from your comment.

      Reply
  2. Andrew Campbell

     /  March 20, 2018

    This is great! I’m aware that Google Street View has a bunch of cameras that form the panoramas. I’m interested in getting the images for selected locations taken from the camera facing in the frontward direction. I’m relatively new to python so i’m wondering if you have any suggestions on whether it’s possible?

    Thanks!

    Reply
  3. Andrew Campbell

     /  March 21, 2018

    The images that download to the specified destination from the code are taken from a view looking at houses along a street. Is there a way in which the images can be oriented to face the road? I’m interested in looking at roadside assets in google street view imagery

    Reply
    • Ok, to do that it takes alittle bit of GIS know-how. First you need to find the orientation of the street, then you can identify which direction to point the camera. If you submit a lat-lon (and orientation) you can get an image for anywhere in the panorama.

      I did this for another project in which I needed to take running images of whole streets. What I did was generate a fine sample of points along the street (like every meter), in a GIS. From that data you can calculate the local orientation of the street. Then I sampled google street view images about every 40 meters (so there was little to no overlap between shots). But I requested images at 90 degrees to the street orientation (so I got the sidewalk), whereas here you would want to just look straight ahead (or backwards?).

      Reply
      • Andrew Campbell

         /  March 27, 2018

        Thanks for your response Andrew.

        This is actually for a GIS project so my GIS know-how is solid. You’re correct in that I do want to look straight ahead (90 degrees, give or take accounting for optimal viewing) of the street. My question would be, where do i need to define the lat longs and orientation for each individual image location in the code?

      • Just using a street-centerline file I created sample points regularly along the segment. Is that your question? You can email and I can share code if you want (I just made the regularly sampled point in ArcGIS).

  4. Simon Ghislain

     /  May 12, 2018

    Hello,

    i just installed python 3.5 and pyCharm, pasted the code, and got relatively same error at run as Joe Devlin:
    AttributeError: module ‘urllib’ has no attribute ‘quote_plus’

    Btw thank you very much for getting me on tracks…and sharing your code.
    I’m trying to
    1) define an itinerary from point A to point B
    2) generate, like in your reply to Andrew Campbell, a set of points every 40m or so
    3) use those points to calculate local direction of road
    4) generate google street screenshots with your code

    (I would like to make an animated GIF of that itinerary)

    But i don’t know where to start…i would like NOT to use ArcGIS (i used to use it but i don’t have it anymore). Also i’m new to Python too, altough i have some background in programmation and coding.
    Is it possible to use the Google itinerary tool to extract the set of points? (instead of a street-centerline file, whiwh I suppose is generated with ArcGIS?)
    Could you please help me in telling me the main steps I should follow in order to achieve that?
    I would be so thankful…

    Thank you very much in advance for any help, and already for sharing your knowledge

    Reply
  5. Simon Ghislain

     /  May 12, 2018

    Ok, the library urllib has been split into urllib.request, urllib.parse ans urllib.error from Python 2.x to Python 3.x

    So only change is to write “urllib.parse.quote_plus” instead of “urllib.quote_plus”
    and write
    “urllib.request.urlretrieve” instead of “urllib.urlretrieve”

    But then i got another error:
    line 258, in urlretrieve
    tfp = open(filename, ‘wb’)
    FileNotFoundError: [Errno 2] No such file or directory: ‘C:\\tests\\ExampleStreetView\\457 West Robinwood Street, Detroit, Michigan 48203.jpg’

    So looks like urlretrieve, instead of copying url content to a new file, tries to access the file beeing created?

    Reply
  6. Simon Ghislain

     /  May 12, 2018

    Yeah it works!
    Simply the destination directory didn’t exist yet…
    Wow so magical…I tried with my own adress, get the .jpg in less than one second…

    Now the last thing I would like to do is instead of a list of addresses, generate a set of coordinates and determine direction alongside road from a Google Map itinerary…with Direction aAPI? Is is possible?

    Reply
  1. Python, Google street view API – HTTP Error 400: Bad Request – program faq
  2. Drawing Google Streetview images down an entire street using python | Andrew Wheeler

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: