Skip to content

Commit 2b89d31

Browse files
committed
Initial commit
1 parent e6d480d commit 2b89d31

16 files changed

+596
-0
lines changed

cache.p

32.4 KB
Binary file not shown.

config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
api_keys:
2+
# https://developers.google.com/maps/documentation/static-maps/
3+
google_static_maps: YOURKEYHERE
4+
# https://developers.google.com/maps/documentation/distance-matrix/
5+
google_distance_matrix: YOURKEYHERE
6+
# https://developers.google.com/maps/documentation/directions/
7+
google_directions: YOURKEYHERE
8+
# https://developers.google.com/maps/documentation/geocoding/
9+
google_geocoding: YOURKEYHERE

createCache.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
import requests
4+
import pickle
5+
6+
import maputils
7+
8+
API_KEY = maputils.config['api_keys']['google_distance_matrix']
9+
BASE_URL = 'https://maps.googleapis.com/maps/api/distancematrix'
10+
CAPITALS = maputils.CAPITALS
11+
12+
# cachedDistDur = pickle.load(open('cache.p', 'rb'))
13+
cachedDistDur = {}
14+
15+
def getDistDur(origin, destination):
16+
# print origin, destination
17+
key = tuple(sorted([origin, destination]))
18+
if key in cachedDistDur:
19+
# print origin, destination, 'from cache'
20+
return cachedDistDur[key]
21+
origin = origin.replace(' ','+')
22+
destination = destination.replace(' ','+')
23+
r = requests.get(BASE_URL + '/json?units=imperial&origins=' + origin + '&destinations=' + destination + '&key=' + API_KEY)
24+
if r.json()['status'] == 'OK':
25+
dist = r.json()['rows'][0]['elements'][0]['distance']['value']
26+
time = r.json()['rows'][0]['elements'][0]['duration']['value']
27+
result = (dist, time)
28+
cachedDistDur[key] = result
29+
print key, result
30+
# print origin, destination, result
31+
return result
32+
else:
33+
raise Exception('Over api limit!')
34+
35+
for p1 in CAPITALS[:50]:
36+
print p1
37+
for p2 in CAPITALS[:50]:
38+
if p1 != p2:
39+
getDistDur(p1, p2)
40+
41+
pickle.dump(cachedDistDur, open('cache.p', 'wb'), -1)

createDirectionsCache.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
3+
import requests
4+
import pickle
5+
import polyline
6+
7+
import maputils
8+
9+
API_KEY = maputils.config['api_keys']['google_directions']
10+
BASE_URL = 'https://maps.googleapis.com/maps/api/directions/json?'
11+
CAPITALS = maputils.CAPITALS
12+
13+
# directionsCache = pickle.load(open('directions.p','rb'))
14+
directionsCache = {}
15+
16+
def getDirections(origin, destination):
17+
if (origin, destination) in directionsCache:
18+
directions = directionsCache[(origin, destination)]
19+
# print origin, destination
20+
else:
21+
url = BASE_URL + 'origin=' + origin + '&destination=' + destination + '&key=' + API_KEY
22+
print origin, destination, '\t' + url
23+
r = requests.get(url)
24+
# print url
25+
if r.json()['status'] == 'OK':
26+
poly = r.json()['routes'][0]['overview_polyline']['points']
27+
directions = polyline.decode(poly)
28+
else:
29+
print r.json()
30+
raise Exception('Something went wrong')
31+
directionsCache[(origin, destination)] = directions
32+
directionsCache[(destination, origin)] = list(reversed(directions))
33+
return directions
34+
35+
for p1 in CAPITALS[:50]:
36+
# print p1
37+
for p2 in CAPITALS[:50]:
38+
if p1 != p2:
39+
getDirections(p1, p2)
40+
41+
pickle.dump(directionsCache, open('directions.p','wb'), -1)

createGeoCache.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
import requests
4+
import pickle
5+
import yaml
6+
import maputils
7+
8+
CAPITALS = maputils.CAPITALS
9+
API_KEY = maputils.config['api_keys']['google_geocoding']
10+
BASE_URL = 'https://maps.googleapis.com/maps/api/geocode'
11+
12+
def getCoords(place):
13+
place = place.replace(' ','+')
14+
url = BASE_URL + '/json?address=' + place + '&key=' + API_KEY
15+
r = requests.get(url)
16+
lat = r.json()['results'][0]['geometry']['location']['lat']
17+
lon = r.json()['results'][0]['geometry']['location']['lng']
18+
return (lat, lon)
19+
20+
geoCache = {}
21+
for place in CAPITALS:
22+
geoCache[place] = getCoords(place)
23+
pickle.dump(geoCache, open('geoCache.p','wb'), -1)

createMap.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import matplotlib.pyplot as plt
2+
from mpl_toolkits.basemap import Basemap
3+
import pickle
4+
5+
# create the figure and axes instances.
6+
fig = plt.figure()
7+
ax = fig.add_axes([0.1,0.1,0.8,0.8])
8+
# setup of basemap ('lcc' = lambert conformal conic).
9+
# use major and minor sphere radii from WGS84 ellipsoid.
10+
m = Basemap(llcrnrlon=-145.5,llcrnrlat=1.,urcrnrlon=-2.566,urcrnrlat=46.352,\
11+
rsphere=(6378137.00,6356752.3142),\
12+
resolution='l',area_thresh=1000.,projection='lcc',\
13+
lat_1=50.,lon_0=-107.,ax=ax)
14+
15+
# draw coastlines and political boundaries.
16+
m.drawcoastlines()
17+
m.drawcountries()
18+
m.drawstates()
19+
20+
pickle.dump(m,open('map.p','wb'),-1)

directions.p

7.06 MB
Binary file not shown.

geoCache.p

1.75 KB
Binary file not shown.

gmap-plot.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import polyline
2+
import requests
3+
import csv
4+
import pickle
5+
import shutil
6+
from subprocess import call
7+
8+
import maputils
9+
10+
csvfile = open('stats.tsv', 'rb')
11+
stats = csv.reader(csvfile, delimiter='\t')
12+
route = list(eval(list(stats)[-1][1]))
13+
14+
poly = maputils.getPoly(route, 0.2)
15+
url = maputils.mapUrl(poly)
16+
17+
filename = 'goog.png'
18+
r = requests.get(url, stream=True)
19+
if r.status_code == 200:
20+
with open(filename, 'wb') as f:
21+
r.raw.decode_content = True
22+
shutil.copyfileobj(r.raw, f)
23+
call(["open", filename])
24+
else:
25+
print r.status_code, 'Error'

gmap-stats.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import polyline
2+
import requests
3+
import pickle
4+
import shutil
5+
import csv
6+
import math
7+
8+
import maputils
9+
10+
csvfile = open('stats.tsv', 'rb')
11+
stats = csv.reader(csvfile, delimiter='\t')
12+
routes = [ list(eval(row[1])) for row in stats ]
13+
14+
for i in xrange(len(routes)):
15+
# for i in xrange(len(routes)-1,len(routes)):
16+
17+
filename = 'pics/' + '{:03}'.format(i) + '.png'
18+
# route = routes[i]
19+
route = routes[i]
20+
21+
poly = maputils.getPoly(route, 0.15)
22+
url = maputils.mapUrl(poly)
23+
24+
r = requests.get(url, stream=True)
25+
if r.status_code == 200:
26+
with open(filename, 'wb') as f:
27+
r.raw.decode_content = True
28+
shutil.copyfileobj(r.raw, f)
29+
print len(url), len(url) - len(poly), len(poly), len(poly)/float(len(polyline.decode(poly))), filename #, routes[i]
30+
else:
31+
print len(url), len(url) - len(poly), len(poly), len(poly)/float(len(polyline.decode(poly))), r.status_code, 'ERROR'
32+
break
33+

0 commit comments

Comments
 (0)