summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjullinator <justus.karlsson@hotmail.se>2018-08-30 19:21:53 +0200
committerjullinator <justus.karlsson@hotmail.se>2018-08-30 19:21:53 +0200
commitb35f002e1a453a12d104bbba6cd6ca354d18fcb6 (patch)
tree0bb717527fc3f0f35c535dc1fcc65d84dec0c145
parente3b6cb5c1cf67286409ae5f9789f203e6beddff1 (diff)
downloadtdde25-b35f002e1a453a12d104bbba6cd6ca354d18fcb6.tar.gz
leaflet updated
-rw-r--r--algorithms.py11
-rw-r--r--server.py1
-rw-r--r--templates/index.css6
-rw-r--r--templates/index.html78
-rw-r--r--templates/index.js55
-rw-r--r--test_dijkstra.py18
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 @@
<html>
<head>
- <meta name="viewport"content="initial-scale=1.0, user-scalable=no"/>
-
+ <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" />
+ <script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js"></script>
{{ templates/index.css }}
-
</head>
-
<body>
-
- <div id="map_canvas"></div>
-
+ <div id="map"></div>
<form id="path-form">
- <div class="row" >
- <div class="col">
- Place marker for:
- <div>
- <input type="radio" name="marker-point" id="marker-point-start" checked="true" />
- <label for="marker-point-start">Start</label>
- </div>
- <div>
- <input type="radio" name="marker-point" id="marker-point-end" />
- <label for="marker-point-end">Destination</label>
- </div>
- </div>
- <div class="col">
- <div>
- <label for="lat1">Start lat:</label>
- <input name="lat1" id="lat1" />
- </div>
- <div>
- <label for="lng1">Start lng:</label>
- <input name="lng1" id="lng1" />
+ <div class="row" >
+ <div class="col">
+ Place marker for:
+ <div>
+ <input type="radio" name="marker-point" id="marker-point-start" checked="true" />
+ <label for="marker-point-start">Start</label>
+ </div>
+ <div>
+ <input type="radio" name="marker-point" id="marker-point-end" />
+ <label for="marker-point-end">Destination</label>
+ </div>
</div>
- </div>
- <div class="col">
- <div>
- <label for="lat2">End latitude:</label>
- <input name="lat2" id="lat2" />
+ <div class="col">
+ <div>
+ <label for="lat1">Start lat:</label>
+ <input name="lat1" id="lat1" />
+ </div>
+ <div>
+ <label for="lng1">Start lng:</label>
+ <input name="lng1" id="lng1" />
+ </div>
</div>
- <div>
- <label for="lng2">End longitude:</label>
- <input name="lng2" id="lng2" />
+ <div class="col">
+ <div>
+ <label for="lat2">End latitude:</label>
+ <input name="lat2" id="lat2" />
+ </div>
+ <div>
+ <label for="lng2">End longitude:</label>
+ <input name="lng2" id="lng2" />
+ </div>
</div>
+ <input type="submit" value="Find path" />
+
</div>
- <input type="submit" />
- </div>
- </form>
- <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=REPLACE_WITH_YOUR_API_KEY&sensor=false"></script>
-
+
+ </form>
{{ templates/index.js }}
</body>
-</html>
+</html> \ 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: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> 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')
+