Skip to content

Commit

Permalink
Merge pull request #13 from MurrayGroves/streetlights
Browse files Browse the repository at this point in the history
Streetlights
  • Loading branch information
MurrayGroves authored Feb 26, 2024
2 parents ad7f4de + 313e4b8 commit 1d1af4a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
16 changes: 13 additions & 3 deletions saferoute/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function App() {
setNewRoute(undefined);
return;
}
fetch(`http://localhost:8080/route?start=${start}&end=${end}&weight=${weight ? 'safety': 'travel_time'}`).then(
fetch(`http://localhost:8080/route?start=${start}&end=${end}&weight=${weight ? 'combined_index': 'travel_time'}`).then(
(response) => {
response.json().then((data) => {
let coordsList = data["route"];
Expand Down Expand Up @@ -100,7 +100,12 @@ out;
];
setStart(loc);
},
options: { types: [] },
options: { types: [], bounds: {
north: 51.5,
south: 51.4,
west: -2.66834,
east: -2.45634,
}},
});

const fromRef = refs.ref;
Expand All @@ -118,7 +123,12 @@ out;
];
setEnd(loc);
},
options: { types: [] },
options: { types: [], bounds: {
north: 51.5,
south: 51.4,
west: -2.66834,
east: -2.45634,
} },
});

const toRef = refs.ref;
Expand Down
26 changes: 25 additions & 1 deletion safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def add_combined_index(G, precision=None):
if precision is None:
precision = 1
edges = utils_graph.graph_to_gdfs(G, nodes=False, fill_edge_geometry=False)
edges["combined_index"] = edges["travel_time"] + edges["safety"]
edges["combined_index"] = np.clip(a=(edges["travel_time"] + edges["safety"] - edges["streetlight"]), a_min=0, a_max=None)
nx.set_edge_attributes(G, values=edges["combined_index"], name="combined_index")
return G

Expand All @@ -26,6 +26,12 @@ def add_edge_safety_index(G, precision=None):
nx.set_edge_attributes(G, values=edges["safety"], name="safety")
return G

def add_streetlight_index(G, precision=None):
edges = utils_graph.graph_to_gdfs(G, nodes=False, fill_edge_geometry=False)
edges["streetlight"] = 1
nx.set_edge_attributes(G, values=edges["streetlight"], name="streetlight")
return G

def crime_score(crime, score):
if crime == "Anti-social behaviour":
score += 2
Expand Down Expand Up @@ -54,6 +60,7 @@ def crime_score(crime, score):
nx.set_edge_attributes(G, values=4, name="speed_kph")
G = ox.add_edge_travel_times(G)
G = add_edge_safety_index(G)
G = add_streetlight_index(G)

def add_node(G, connectingNodes, x, y, direction):
newId = f"{len(G.nodes) + 1}-custom"
Expand Down Expand Up @@ -98,6 +105,23 @@ def get_reference_points(G):
safety = G[edges[index][0]][edges[index][1]][0]["safety"]
G[edges[index][0]][edges[index][1]][0]["safety"] = crime_score(row[2], safety)

with open("streetlights.csv", newline='') as f:
reader = csv.reader(f, delimiter=',')
linecount = 1
for row in reader:
if linecount == 1:
linecount += 1
continue
longitude = float(row[0])
latitude = float(row[1])
if longitude < -2.642410 or longitude > -2.463330 or latitude < 51.401792 or latitude > 51.523830:
continue
_, index = kd_tree.query((longitude, latitude))
streetlight = G[edges[index][0]][edges[index][1]][0]["streetlight"]
G[edges[index][0]][edges[index][1]][0]["streetlight"] = streetlight + 1



G = add_combined_index(G)


Expand Down

0 comments on commit 1d1af4a

Please sign in to comment.