Skip to content

Instantly share code, notes, and snippets.

@paulopperman
Last active January 4, 2018 16:11
Show Gist options
  • Save paulopperman/cf55a5a7afc3afa9147847729bafbb53 to your computer and use it in GitHub Desktop.
Save paulopperman/cf55a5a7afc3afa9147847729bafbb53 to your computer and use it in GitHub Desktop.
get streetview images of points in a geojson file
import requests
from PIL import Image
from io import BytesIO
import os
import pandas as pd
# get crosswalk dataset
newport_crossings_url = 'https://raw.githubusercontent.com/NewportDataPortal/sidewalk-map/development/npt-sidewalk-street-intersections.geojson'
crossing_response = requests.get(newport_crossings_url)
crossing_json = crossing_response.json()
# extract list of lat/lon
crossing_points = []
for c in crossing_json['features']:
crossing_points.extend([c['geometry']['coordinates']])
url = 'https://maps.googleapis.com/maps/api/streetview'
key = os.environ['STREETVIEW_API_KEY']
# dataset naming parameters
image_folder = 'crossing_images/' # this must be created first TODO: check if exists and create if not
dataset_name = 'npt_crosswalk'
dataset_df = pd.DataFrame()
tots = len(crossing_points)
counter = 1
for p in crossing_points:
print(str(counter) + ' of ' + str(tots))
params = {'location': str(p[1])+','+str(p[0]), 'size': "600x600", 'fov':'120', 'key':key}
meta_resp = requests.get(url + '/metadata', params=params)
meta_df = pd.io.json.json_normalize(meta_resp.json())
meta_df['cross_lon'] = p[0]
meta_df['cross_lat'] = p[1]
if meta_df.status.values == 'OK':
fileid = dataset_name + '_' + meta_df.date.values[0] + '_' + str(p[1]) + '_' + str(p[0])
if not os.path.isfile(image_folder + fileid + '.jpg'): # check to see if the file is already in the folder, skip is not
print('requesting ' + fileid +' ...')
image_resp = requests.get(url, params=params, stream=True)
img = Image.open(BytesIO(image_resp.content))
img.save(image_folder + fileid + '.jpg', 'jpeg')
meta_df['img'] = fileid + '.jpg'
print("writing " + fileid + ".jpg")
else:
print(fileid + " already in folder, skipping...")
dataset_df = pd.concat([dataset_df, meta_df])
else:
meta_df['img'] = 'invalid'
print('invalid')
counter += 1
dataset_df.to_json(image_folder + dataset_name + '_metadata.json', orient='records')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment