Get Street View Photos¶
M. Fawcett - 10/12/2021
Download Google Street View static photos from a list of street addresses.
It assumes that a csv file containing addresses has been sourced from the National Address Database, and the csv file in in the current working directory.
It also assumes you have a Google API key to make the requests to the Street View Service.
There is a charge of $0.007 (7/10ths of a cent) per request.
In [35]:
# Load libraries
import pandas as pd
import numpy as np
import os
import urllib.parse
import urllib.request
In [2]:
# Set some settings
pd.set_option('display.max_columns', None)
In [38]:
# Sort address list by street name then house number.
df = pd.read_csv("NAD_r7_NewYork_13903.csv")
# the .sort_values method returns a new dataframe, so assign this to a new variable.
sorted_addr = df.sort_values(by=["Post_Comm", "StreetName", "Add_Number"])
In [49]:
# Define a function to get images and save them to a sub-folder of current working directory.
def GetStreetImage(Addr, SaveLoc, key):
base = "https://maps.googleapis.com/maps/api/streetview?size=640x640&location="
MyUrl = base + urllib.parse.quote_plus(Addr) + key #added url encoding
# print(MyUrl)
fi = Addr + ".jpg"
urllib.request.urlretrieve(MyUrl, os.path.join(SaveLoc, fi))
In [50]:
j = 0 # a loop counter
maxj = 10000 # limit the number of addresses being searched
previousaddress = ""
for index, row in sorted_addr.iterrows():
try:
# print(row["StN_PreDir"], str(row['Add_Number']), row['StreetName'], row['StN_PosTyp'], row["Post_Comm"], row["State"], str(row["Zip_Code"]))
address = (str(row["StN_PreDir"]) + " " + str(row['Add_Number']) + " " + row['StreetName'] + " " + row['StN_PosTyp']
+ " " + row["Post_Comm"] + " " + row["State"] + " " + str(row["Zip_Code"]))
if previousaddress != address: # For whatever reason, the same address can repeat multiple times
# in the NAD.
previousaddress = address
if j % 100 == 0:
print(j)
address_clean = address.replace("nan", "")
# print(address_clean)
GetStreetImage(address_clean, DESTINATION_FOLDER, MY_API_KEY)
j += 1
if j > maxj:
break
except Exception as e:
print(e)
#replace(np.nan, 0)
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000 3100 3200 3300 3400 3500 3600 3700 3800 3900 4000 4100 4200 4300 4400 4500 4600 4700 4800 4900 5000 5100 5200 5300 5400 5500 5600 5700 5800 5900 6000 6100 6200 6300 6400 6500 6600 6700 6800 6900