From b35f002e1a453a12d104bbba6cd6ca354d18fcb6 Mon Sep 17 00:00:00 2001 From: jullinator Date: Thu, 30 Aug 2018 19:21:53 +0200 Subject: leaflet updated --- algorithms.py | 11 +++++++- server.py | 1 - templates/index.css | 6 ++-- templates/index.html | 78 +++++++++++++++++++++++++--------------------------- templates/index.js | 55 ++++++++---------------------------- test_dijkstra.py | 18 ++++++------ 6 files changed, 71 insertions(+), 98 deletions(-) diff --git a/algorithms.py b/algorithms.py index f39910c..1a62118 100644 --- a/algorithms.py +++ b/algorithms.py @@ -11,4 +11,13 @@ def length_haversine(p1, p2): a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlng / 2) ** 2 c = 2 * math.asin(math.sqrt(a)) - return 6372797.560856 * c # return the distance in meters \ No newline at end of file + return 6372797.560856 * c # return the distance in meters + +def get_closest_node_id(nodes, source_node): + """ Search through all nodes and return the id of the node + that is closest to 'source_node'. """ + pass + +def find_shortest_path(nodes, source_id, target_id): + """ Return the shortest path using Dijkstra's algortihm. """ + return [] \ No newline at end of file diff --git a/server.py b/server.py index dcf0987..e69de29 100644 --- a/server.py +++ b/server.py @@ -1 +0,0 @@ -## Work yo magic xDDDD \ No newline at end of file diff --git a/templates/index.css b/templates/index.css index f8bab2c..0d9fcbc 100644 --- a/templates/index.css +++ b/templates/index.css @@ -1,6 +1,6 @@ -html { height: 100% } -body { height: 80%; margin: 0; padding: 0 } -#map_canvas { height: 100%; width: 100% } +#map { + height:80%; +} .row { display:flex; } diff --git a/templates/index.html b/templates/index.html index 717adc7..d193447 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,54 +1,50 @@ - - + + {{ templates/index.css }} - - - -
- +
-
-
- Place marker for: -
- - -
-
- - -
-
-
-
- - -
-
- - +
+
+ Place marker for: +
+ + +
+
+ + +
-
-
-
- - +
+
+ + +
+
+ + +
-
- - +
+
+ + +
+
+ + +
+ +
- -
- - - + + {{ templates/index.js }} - + \ No newline at end of file diff --git a/templates/index.js b/templates/index.js index bc05ed7..b47ab72 100644 --- a/templates/index.js +++ b/templates/index.js @@ -1,22 +1,12 @@ -var map; +const map = L.map('map').setView([58.40384776751319, 15.578484535217285], 15); const form = document.querySelector('#path-form') const markerIsStart = document.querySelector("#marker-point-start") const markerIsEnd = document.querySelector("#marker-point-end") - -google.maps.event.addDomListener(window, 'load', initialize); form.addEventListener('submit', postShortestPath, false) - -function initialize() { - var mapOptions = { - center: new google.maps.LatLng(58.40384776751319, 15.578484535217285), - zoom: 15, - mapTypeId: google.maps.MapTypeId.ROADMAP - }; - var mapCanvas = document.getElementById("map_canvas") - map = new google.maps.Map(mapCanvas, mapOptions); - google.maps.event.addListener(map, 'click', handleMapClick) -} +L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { + attribution: '© OpenStreetMap contributors' +}).addTo(map); async function postShortestPath(event){ event.preventDefault() @@ -38,42 +28,19 @@ async function postShortestPath(event){ }) res = await res.json() - var points = new google.maps.MVCArray() console.log(res.path) - res.path.map(coord=>{ - var [lat, lng] = coord - var point = new google.maps.LatLng(lat, lng) - points.push(point) - }) - - createPolyline(points) -} + var poly = L.polyline(res.path).addTo(map) -function createPolyline(points){ - var polyline = new google.maps.Polyline({ - path: points, - map: map - }); } -function handleMapClick ({latLng}){ - createMarker(latLng, `Click: ${latLng}`) - console.log(latLng) - var {lat, lng} = latLng +function handleMapClick ({latlng}){ + var {lat, lng} = latlng + var marker = L.marker([lat, lng]).addTo(map) var posNumber = markerIsStart.checked ? '1' : '2' - document.querySelector('#lat' + posNumber).value = lat() - document.querySelector('#lng' + posNumber).value = lng() + document.querySelector('#lat' + posNumber).value = lat + document.querySelector('#lng' + posNumber).value = lng if(markerIsStart.checked) markerIsEnd.checked = true - - } - -function createMarker (position, title){ - return new google.maps.Marker({ - map, - position, - title - }) -} \ No newline at end of file +map.on('click', handleMapClick) diff --git a/test_dijkstra.py b/test_dijkstra.py index 4f54b4e..a0f315c 100644 --- a/test_dijkstra.py +++ b/test_dijkstra.py @@ -1,14 +1,15 @@ -import Graph +import notebooks.Graph as Graph from collections import defaultdict from heapq import heappush, heappop import math -def dijkstra(adjacency_list, source, target): - """ To be implemented by students. `adjacency_list` is a dictionary with values: (vertice, weight). - Function should return (distance, path). Path should be a list of the vertices in the shortest path - **including** the source and target. If no shortest path is found, it should return (infinity, []) """ - - return math.inf, [source] +def dijkstra(adjacency_list, source_id, target_id): + """ To be implemented by students. `adjacency_list` is a dictionary with structure: + {node_id: [...(neighbor_id, weight)]}. + Function should return (distance, path). Path should be a list of the nodes in the shortest path + **including** the source_id and target_id. If no shortest path is found, it should return (infinity, []) """ + + return math.inf, [source_id] if __name__ == '__main__': @@ -16,4 +17,5 @@ if __name__ == '__main__': Graph.test_dijkstra(dijkstra) print('Passed ALL tests!') except AssertionError: - print('Failed one or more tests') \ No newline at end of file + print('Failed one or more tests') + -- cgit v1.2.1