-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrips.py
More file actions
105 lines (65 loc) · 2.27 KB
/
Copy pathtrips.py
File metadata and controls
105 lines (65 loc) · 2.27 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import requests
import json
import math
def intrazonal(area, mode):
distance = math.sqrt(area / math.pi)
if mode == "Walk":
speed = 5
elif mode == "Bicycle":
speed = 15
elif mode == "Drive":
speed = 50
elif mode == "Transit":
speed = 5
else:
return -1,-1
duration = distance / speed
distance = 1000* distance # from km to m
duration = duration * 60 * 60 # from hr to sec
return duration, distance
def osrm_trip(id,x1,y1,x2,y2,mode):
route_url = "http://127.0.0.1:5000/route/v1/" + mode + "/" + str(x1) + "," + str(y1) + ";" + str(x2) + "," + str(y2) + "?steps=false&geometries=geojson&overview=false"
print(route_url)
try:
page = requests.get(route_url, verify=False, timeout=2)
route = json.loads(page.content)
duration = route['routes'][0]['legs'][0]["duration"]
distance = route['routes'][0]['legs'][0]["distance"]
except:
distance = -1
duration = -1
print(distance, duration)
return duration, distance
def osrm_trip_geojson(id,x1,y1,x2,y2,mode):
route_url = "http://127.0.0.1:5000/route/v1/" + mode + "/" + str(x1) + "," + str(y1) + ";" + str(x2) + "," + str(y2) + "?steps=false&geometries=geojson&overview=full"
page = requests.get(route_url)
route = json.loads(page.content)
duration = route['routes'][0]['legs'][0]["duration"]
distance = route['routes'][0]['legs'][0]["distance"]
geometry = route['routes'][0]["geometry"]
geojson = {
"type": "FeatureCollection","features": [
{
"type": "Feature",
"properties": {
"ID": id,
"duration": duration,
"distance": distance
},
"geometry": geometry
}
]
}
return geojson
# osrm_trip(1,-79.24610,43.82540,-79.93925,43.53418,"drive")
# g = osrm_trip_geojson(1,-79.87554,43.24455,-79.34372,43.71603,"bike")
#
# with open("temp/test2_up_elev2.geojson", 'w') as fatty_mcgoo:
# json.dump(g, fatty_mcgoo)
# g = osrm_trip_geojson(1,-79.34372,43.71603,-79.37061,43.65317,"bike")
#
# with open("temp/test1_down_elev2.geojson", 'w') as fatty_mcgoo:
# json.dump(g, fatty_mcgoo)
#
# print(osrm_trip(1,-79.24610,43.82540,-79.93925,43.53418,"drive"))
#