summaryrefslogtreecommitdiffstats
path: root/server.py
blob: 8055e63232c30b7fc4c60738f22e68feda6f12a1 (plain) (blame)
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
import json

import algorithms
import store
from lib import run_server, get, post, read_html

grids = None
nodes = None
unconnected_nodes = None

@get('/')
def index():
    global nodes
    global grids
    global unconnected_nodes

    nodes, grids, unconnected_nodes = store.extract_osm_nodes("linkoping.osm")

    return read_html('templates/index.html')


@get('/show-area')
def show_area():
    rect = dict()
    # FIXME: Don't hardcode bikes maybe? Maybe just remove this altogether
    for (k, node) in enumerate(
            store.select_nodes_in_rectangle(nodes['bike'], 58.3984, 58.3990,
                                            15.5733, 15.576)):
        rect[node.id] = node.coord_tuple()
    return json.dumps(rect)


@get('/favicon.ico')
def favicon():
    with open("data/favicon.ico", "rb") as image:
        read_image = image.read()
        image_bytes = bytearray(read_image)

    return image_bytes


@get('/show-unconnected-nodes')
def show_unconnected_nodes():
    print(f"Showing {len(unconnected_nodes)} unconnected nodes")
    return json.dumps({
        node.id: node.coord_tuple() for node in unconnected_nodes['bike']
    })


@post('/shortest-path')
def shortest_path(body):
    body = json.loads(body)
    transport_mode = body['transport_mode']

    relevant_grid = grids[transport_mode]

    source_id = algorithms.grid_search(relevant_grid,
                                       store.Node(-1, body['lat1'],
                                                      body['lng1']))
    target_id = algorithms.grid_search(relevant_grid,
                                       store.Node(-1, body['lat2'],
                                                      body['lng2']))

    relevant_nodes = nodes[transport_mode]

    path = algorithms.find_shortest_path(nodes[transport_mode], source_id, target_id)
    response = {"path": [(relevant_nodes[node].lat, relevant_nodes[node].lng) for node in path]}

    return json.dumps(response)


run_server()