-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
141 lines (114 loc) · 4.81 KB
/
node.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import csv
import math
import requests
import foursquare
import util
import pickle
import numpy as np
import googlemaps
from datetime import datetime
from scipy import spatial
import learn
class Node:
def __init__(self, location, does_exist):
"""
location: (long, lat)
"""
self.location = location
self.does_exist = does_exist
def calculate_desireability(self):
"""
Calculates the desirability of a node with it's location
Factors:
- Few accidents nearby
- Nearby 4SQ venues
- Away from pedestrian flow
- Near major bike routes
- Nearby modes of transportation
- Large summed distance of nearest N racks
"""
self.feature_nearby_accident = self.get_nearby_accidents()
self.feature_nearby_venues = self.get_nearby_venues()
self.feature_pedestrian_flow = self.get_pedestrian_flow()
self.feature_biking_popularity = self.get_biking_popularity()
self.feature_nearby_transportation = self.get_nearby_transportation()
self.feature_average_rack_distance = self.get_average_rack_distance()
def calculate_desireability_score(self):
"""
Calculates the final desirability score of a node given
the features previously calculated
"""
self.desireability_score = learn.classify(self)
def get_nearby_accidents(self):
"""Gets nearby accidents"""
threshold = 0.000042 # the size of a 2block in manhattan in change of degrees
if (len(util.Util().AccidentResults) == 0):
util.Util().set_Accidents()
accident_points = util.Util().AccidentCords
accident_results = util.Util().AccidentResults
result = 0
point = (float(self.location[0]),float(self.location[1]))
hits = accident_points.query_ball_point(point, 0.000042, 2)
for val in hits:
result+=int(accident_results[val])
return result
def get_nearby_venues(self):
"""Gets nearby venues within a 100 meter radius"""
resp = {}
try:
resp = pickle.load(open("nearby_venues.pkl", "rb"))
except:
client_id = "UDGDHLG2KHK0A3U1KWZQ0WBEWOG0W3X0HTC0OVIGLQNSHNL2"
client_secret = "HWDWNMI0FAW34FIDJL1LDY5VXMMRNRJM5VY15A0310JQI0MH"
client = foursquare.Foursquare(client_id, client_secret)
ll = str(self.location[1]) + "," + str(self.location[0])
rad = 20 # Radius in meters
resp = client.venues.search(params={'radius': str(rad), "ll" : ll, "limit": 50, "intent":"browse"})
pickle.dump(resp, open("nearby_venues.pkl", "wb"))
return len(resp["venues"])
def get_pedestrian_flow(self):
"""Gets nearby pedestrian flow"""
def get_biking_popularity(self):
"""Gets the popularity of a bike route at this location
Location window = 10m?
"""
if len(util.Util().LocationPopularityResults) == 0:
util.Util().set_LocationPopularity()
location_popularity = util.Util().LocationPopularity
location_popularity_results = util.Util().LocationPopularityResults
pt = [(self.location[0]), float(self.location[1])]
neighboring_nodes = location_popularity.query(pt, k=3)
result = 0
for res in neighboring_nodes[1]:
result += location_popularity_results[res]
return result
def get_nearby_transportation(self):
"""Gets the nearby transportation (bus stop, subway, etc.)
Note that i am using 0.0042 intead of 0.000042
"""
if not isinstance(util.Util().Subways, spatial.ckdtree.cKDTree):
util.Util().set_Subways()
if not isinstance(util.Util().Busses, spatial.ckdtree.cKDTree):
util.Util().set_Busses()
pt = [self.location[0], self.location[1]]
subways = util.Util().Subways
transit = len(subways.query_ball_point(pt, 0.0042, 2))
busses = util.Util().Busses
transit += len(busses.query_ball_point(pt, 0.0042, 2))
return transit
def get_average_rack_distance(self):
"""Gets the average distance to closest 4 racks"""
if not isinstance(util.Util().Existing_Nodes, spatial.ckdtree.cKDTree):
util.Util().set_Exisiting_Nodes()
pt = [self.location[0], self.location[1]]
existing_nodes = util.Util().Existing_Nodes
neighboring_nodes = existing_nodes.query(pt, k=4)
return sum(neighboring_nodes[0])
# if __name__ == '__main__':
# n = Node((-73.9808623, 40.7587442), True)
# print n.get_nearby_accidents()
# print n.get_nearby_venues()
# print n.get_pedestrian_flow()
# print n.get_nearby_transportation()
# print n.get_biking_popularity()
# print n.get_average_rack_distance()