Created
April 24, 2018 13:47
-
-
Save manashmandal/a2b510c656002eade5c731b3d493e8d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from __future__ import division,absolute_import,print_function\n", | |
"import requests\n", | |
"import shutil\n", | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"from scipy.interpolate import interp1d \n", | |
"import os\n", | |
"save_dir = 'F:\\streetview'\n", | |
"road = 'TajmahalRoad'\n", | |
"district = 'Dhaka'\n", | |
"start = [23.764260, 90.365447]\n", | |
"end = [23.7625406,90.3592206]\n", | |
"interpolation = 50" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Use [HERE api](https://developer.here.com/documentation/routing/topics/what-is.html) to get coordinates along path" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"app_id = \"DemoAppId01082013GAL\"\n", | |
"app_code = \"AJKnXv84fjrb0KIHawS0Tg\"\n", | |
"request = \"https://route.cit.api.here.com/routing/7.2/calculateroute.json?waypoint0=\"+str(start[0])+\"%2C\"+str(start[1])+\"&waypoint1=\"+str(end[0])+\"%2C\"+str(end[1])+\"&mode=fastest%3Bcar%3Btraffic%3Adisabled&routeattributes=shape&app_id=\"+app_id+\"&app_code=\"+app_code+\"&departure=now\"\n", | |
"response = requests.get(request)\n", | |
"if response.status_code == 200:\n", | |
" links = response.json()['response']['route'][0]['shape']" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Plot path GPS coordinates of response" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"links_ = [np.asarray(x.split(',')).astype(float) for x in links]\n", | |
"links_ = np.reshape(np.hstack(links_),(-1,2))\n", | |
"plt.scatter(links_[:,0],links_[:,1])\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Interpolate along the coordinates and plot" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"f2 = interp1d(links_[:,0],links_[:,1], kind='linear')\n", | |
"xnew = np.linspace(links_[0,0],links_[-1,0], num=interpolation)\n", | |
"plt.scatter(xnew,f2(xnew))\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Turn coordinates to String" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"links_ = zip(xnew,f2(xnew))\n", | |
"links_ = [str(x[0])+\",\"+str(x[1]) for x in links_]\n", | |
"print(links_[0])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Get unique PanoID and fit coordinates for the predicted path" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"panoID = []\n", | |
"latitude = []\n", | |
"longitude = []\n", | |
"for coordinates in links_:\n", | |
" request = \"https://cbks0.google.com/cbk?cb_client=apiv3&authuser=0&hl=en&output=polygon&it=1%3A1&rank=closest&ll=\"+coordinates+\"&radius=10\"\n", | |
" response = requests.get(request)\n", | |
" if response.status_code == 200:\n", | |
" try:\n", | |
" panoID.append(response.json()['result'][0]['id'])\n", | |
" latitude.append(response.json()['result'][0]['latitude'])\n", | |
" longitude.append(response.json()['result'][0]['longitude'])\n", | |
" except KeyError:\n", | |
" print(response.content) \n", | |
"df = pd.DataFrame(panoID,columns={\"panoID\"})\n", | |
"df['latitude'] = latitude\n", | |
"df['longitude'] = longitude\n", | |
"df = df.drop_duplicates(subset=\"panoID\")\n", | |
"df['road'] = [road]*df['panoID'].count()\n", | |
"df['district'] = [district]*df['panoID'].count() \n", | |
"df.head()\n", | |
"panoID = list(set(panoID)) # uniques for google api" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"csv_file = os.path.join(save_dir,\"metadata_{},{}.csv\".format(road,district))\n", | |
"df.to_csv(csv_file,index=False)\n", | |
"df['panoID'].to_csv(os.path.join(save_dir,'downloadlist.txt'),index=False)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Use Google Street View Api to download segments of panos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"api_key = \"AIzaSyDA1bRkrS0i8d1C4QaDz3WGK7B8sMuIHsA\"\n", | |
"fov = 90\n", | |
"size = '640x640'\n", | |
"pitch = '10'\n", | |
"headings = list(range(0,360,45))\n", | |
"headings.remove(90)\n", | |
"headings.remove(270)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for pano in panoID:\n", | |
" for heading in headings:\n", | |
" request = \"https://maps.googleapis.com/maps/api/streetview?size=\"+size+\"&pano=\"+pano+\"&fov=\"+str(fov)+\"&heading=\"+str(heading)+\"&pitch=\"+pitch+\"&key=\"+api_key\n", | |
" response = requests.get(request,stream=True)\n", | |
" if response.status_code == 200:\n", | |
" try:\n", | |
" if response.headers['Content-Type'] == 'image/jpeg':\n", | |
" image_name = \"img_pano{}_heading{}_pitch{}_fov{}.jpeg\".format(pano,heading,pitch,fov)\n", | |
" with open(os.path.join(save_dir,image_name), 'wb') as out_file:\n", | |
" shutil.copyfileobj(response.raw, out_file)\n", | |
" except KeyError:\n", | |
" print(response.content)\n", | |
" del response" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"panoID[27:32]" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment