-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
105 lines (85 loc) · 2.74 KB
/
server.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
from typing import List
from fastapi import FastAPI, Response
from geojson_pydantic.features import Feature, FeatureCollection
import osmnx as ox
from shapely.geometry import box, Point
import find_route_functions
from typing import List
from models import LatLng
capetown_G = ox.graph_from_address(
"Cape Town, South Africa", dist=5000, network_type="drive"
)
nodes, edges = ox.graph_to_gdfs(capetown_G)
minx, miny, maxx, maxy = nodes.total_bounds
graph_bounds = box(minx, miny, maxx, maxy)
print(f"Using bounds {graph_bounds}")
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/graph_bounds")
def read_graph():
return graph_bounds.__geo_interface__
@app.get("/shortest_route")
def read_shortest_route(
origin_lat: float,
origin_lng: float,
destination_lat: float,
destination_lng: float,
):
if not (
graph_bounds.contains(Point(origin_lng, origin_lat))
and graph_bounds.contains(Point(destination_lng, destination_lat))
):
return Response(
status_code=400, content="Origin or destination outside of Cape Town"
)
shortest_path = find_route_functions.shortest_path(
orig_lat=origin_lat,
orig_lng=origin_lng,
dest_lat=destination_lat,
dest_lng=destination_lng,
graph=capetown_G,
)
return linestring_to_feature(shortest_path)
@app.get("/k_shortest_paths")
def read_k_shortest_paths(
origin_lat: float,
origin_lng: float,
destination_lat: float,
destination_lng: float,
k: int,
):
if not (
graph_bounds.contains(Point(origin_lng, origin_lat))
and graph_bounds.contains(Point(destination_lng, destination_lat))
):
return Response(
status_code=400, content="Origin or destination outside of Cape Town"
)
k_shortest_paths = find_route_functions.k_shortest_paths(
orig_lat=origin_lat,
orig_lng=origin_lng,
dest_lat=destination_lat,
dest_lng=destination_lng,
graph=capetown_G,
k=k,
)
return linestrings_to_feature_collection(k_shortest_paths)
def linestring_to_feature(linestring: List[LatLng]) -> Feature:
coordinates = [[c.lon, c.lat] for c in linestring]
linestring_feature = {
"type": "Feature",
"properties": {},
"id": f"{str(linestring[0])} {str(linestring[-1])}",
"geometry": {
"type": "LineString",
"coordinates": coordinates,
},
}
return Feature(**linestring_feature)
def linestrings_to_feature_collection(
linestrings: List[List[LatLng]],
) -> FeatureCollection:
features = [linestring_to_feature(linestring) for linestring in linestrings]
return FeatureCollection(features=features)