-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.html
221 lines (177 loc) · 6.14 KB
/
editor.html
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>Code Editor Demo</title>
<link rel="stylesheet" href="static/leaflet.css" />
<script src="static/leaflet.js"></script>
<style type="text/css" media="screen">
#editor {
position: absolute;
right:0;
bottom:0;
top: 50;
left: 0;
height:600px;
width:800px;
}
#map {
position: absolute;
top: 50px;
left:800px;
height:640px;
}
</style>
<script src="static/jquery.min.js"></script>
</head>
<body>
<button id="submitButton" onclick="submitAlgorithm()">Run Code!</button>
<div id="map" style="height: 640px"></div>
<div id="editor">
def routingAlgorithm():
# define helper methods
def nodeWithShortestDistance(frontier):
distance = float('inf')
shortestId = None
for key, val in frontier.iteritems():
if val['distance'] < distance:
distance = val['distance']
shortestId = key
return gn.nodeCache[shortestId]
def getNextNodes(node):
for key in gn.wayCache:
for inode in gn.wayCache[key]:
if gn.nodeCache[inode]['id'] == node['id']:
retArr = []
for key2 in gn.wayCache[key]:
retArr.append(key2)
return retArr
return []
def costBetweenTwoNodes(node1, node2):
radlat1 = math.pi * node1['lat']/180
radlat2 = math.pi * node2['lat']/180
radlon1 = math.pi * node1['lon']/180
radlon2 = math.pi * node2['lon']/180
theta = node1['lon'] - node2['lon']
radtheta = math.pi * theta/180
dist = math.sin(radlat1) * math.sin(radlat2) + math.cos(radlat1) * math.cos(radlat2) * math.cos(radtheta)
dist = math.acos(dist)
dist = dist * 180/math.pi
dist = dist * 60 * 1.1515
dist = dist * 1.609344 * 1000
return dist
def costFromStart(node):
return costBetweenTwoNodes(startNode, node)
def costTillGoal(node):
return costBetweenTwoNodes(node, endNode)
# start the computation!
maxSteps = 1000
node = startNode
node['prevCost'] = 0
pq = PQueue()
alreadyExplored = {}
# cost function = costTillNow + costTillGoal
fstart = 0 + costTillGoal(node)
pq.put(node, fstart)
print("Starting computation.")
while not pq.isEmpty() and maxSteps > 0:
curNode = pq.get()
if curNode['id'] in alreadyExplored:
maxSteps = maxSteps - 1
continue
alreadyExplored[curNode['id']] = True
# if goal found
if curNode['id'] == endNode['id']:
print("Found a path.")
latLng = []
while 'previous' in curNode:
latLng.append({'nodeLat': curNode['lat'], 'nodeLong': curNode['lon']})
curNode = curNode['previous']
gn.drawLine({'pTime': 10000 - maxSteps*10, 'pathLine': latLng})
break
# for each neighbor node
nextNodes = getNextNodes(curNode)
for i in range(0, len(nextNodes)):
nextNode = gn.nodeCache[nextNodes[i]]
if nextNode['id'] in alreadyExplored:
continue
nextNode['previous'] = curNode
nextNode['prevCost'] = curNode['prevCost'] + costBetweenTwoNodes(curNode, nextNode)
# cost function = costTillNow + costTillGoal
fn = nextNode['prevCost'] + costTillGoal(nextNode)
pq.put(nextNode, fn)
gn.drawCircle({'circleLat': curNode['lat'], 'circleLong': curNode['lon'], 'circleTime': 10000 - maxSteps*10})
maxSteps = maxSteps - 1
</div>
<script src="static/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
//////////////////////////
// Setup Code Editor
//////////////////////////
var editor = ace.edit("editor")
editor.setTheme("ace/theme/textmate")
editor.getSession().setMode("ace/mode/python")
//////////////////////////
// Setup Map
//////////////////////////
// Start Target - AM1, Lübeck
var lat1 = 43.7341179;
var lon1 = 7.4180124;
// Goal Target
var lat2 = 43.7343154;
var lon2 = 7.4179286;
var range = 0.02;
var map = L.map('map');
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
map.setView([lat1, lon1], 15);
/* on running the code, submit to server */
function submitAlgorithm() {
var code = editor.getValue()
L.polygon([ // Show loaded range
[lat1 + range, lon1 - range],
[lat1 + range, lon1 + range],
[lat1 - range, lon1 + range],
[lat1 - range, lon1 - range]
], { fillOpacity: 0.2 }).addTo(map)
L.marker([lat1, lon1]).addTo(map)
L.marker([lat2, lon2]).addTo(map)
$.ajax({
url: 'http://localhost:9494/api/runcode',
method: 'POST',
data: {
code: code.toString()
},
success: function(data) {
Visualize(JSON.parse(data))
}
})
}
function Visualize(data) {
console.log('no of circles: ' + data.circles.length)
var circles = [];
for(var i = 0 ; i < data.circles.length ; i++) {
circles.push([data.circles[i]['circleLat'], data.circles[i]['circleLong']]);
}
for(var i = 0 ; i < data.circles.length ; i++) {
(function(i) {
setTimeout(function() {
L.circle(circles[i], 2).addTo(map);
}, data.circles[i]['circleTime']);
})(i)
}
if (!data.path.pathLine) {
console.log("Couldn't find path.");
return;
}
var pathLine = [];
for(var i = 0 ; i < data.path.pathLine.length ; i++) {
pathLine.push([data.path.pathLine[i]['nodeLat'], data.path.pathLine[i]['nodeLong']]);
}
setTimeout(function() {
L.polyline(pathLine, { color: 'red' }).addTo(map);
}, data.path.pTime);
}
</script>
</body>
</html>