-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.py
66 lines (60 loc) · 2.33 KB
/
grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
import os
import pickle
import requests
#Defines the BASE_POINT as the Mustek Metro station.
BASE_POINT = Point(14.4243832,50.083593)
def get_walking_time(p2, p1=BASE_POINT):
URL = 'http://dev.virtualearth.net/REST/V1/Routes/Transit?timeType=Departure&dateTime=1:00:00PM&output=json&key=<APIKEYGOESHERE>'
params = {'wp.0':str(p1.y)+','+str(p1.x), 'wp.1':str(p2.y)+','+str(p2.x)}
try:
resp = requests.get(URL, params = params)
resp = resp.json()
print(params)
transits = resp['resourceSets'][0]['resources'][0]['routeLegs'][0]['itineraryItems']
walkingTime = 0
walkingDist = 0
for t in transits:
if t['iconType'] == 'Walk':
walkingTime += float(t['travelDuration'])
walkingDist+= float(t['travelDistance'])
walkingTime/=60
return (walkingTime, walkingDist)
except:
return None
def create_grid(bbox,polygon ,num_intervals=50):
#Y, X = Lat, Long
lowY = float(bbox[0])
highY = float(bbox[1])
lowX =float( bbox[2])
highX =float( bbox[3])
gridPoints = []
intervalX = (highX - lowX)/num_intervals
intervalY = (highY - lowY)/num_intervals
print('Interval X :', intervalX)
print('Interval Y :', intervalY)
for i in range(num_intervals+1):
for j in range(num_intervals+1):
point = Point(lowX + (i*intervalX), lowY+ (j*intervalY))
if point.within(polygon):
gridPoints.append(point)
print(len(gridPoints), ' points added')
return gridPoints
def get_grid(num_intervals=50):
f = open('prague_bounding_box.json')
if(os.path.exists('./points.pkl')):
pklf = open('./points.pkl', 'rb')
return pickle.load(pklf)
pragueJson = json.load(f)[0]
praguePoints = pragueJson['geojson']['coordinates'][0]
#praguePoly = Polygon(praguePoints, pragueJson['boundingbox'])
points = [Point((p[0], p[1])) for p in praguePoints]
points_tuples = [(p[0], p[1]) for p in praguePoints]
polygon = Polygon(points_tuples)
grid = create_grid(pragueJson['boundingbox'], polygon, num_intervals = num_intervals)
pklf = open('./points.pkl', 'wb')
print('saving points to points.pkl')
pickle.dump(grid, pklf)
return grid