Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/web/www/routino/router.js
Parent Directory
|
Revision Log
Revision 574 -
(show annotations)
(download)
(as text)
Wed Dec 29 10:11:08 2010 UTC (14 years, 2 months ago) by amb
File MIME type: application/javascript
File size: 36207 byte(s)
Wed Dec 29 10:11:08 2010 UTC (14 years, 2 months ago) by amb
File MIME type: application/javascript
File size: 36207 byte(s)
Added the uncontrolled (not auto-generated) files from routino-1.3.
1 | // |
2 | // Routino router web page Javascript |
3 | // |
4 | // Part of the Routino routing software. |
5 | // |
6 | // This file Copyright 2008,2009,2010 Andrew M. Bishop |
7 | // |
8 | // This program is free software: you can redistribute it and/or modify |
9 | // it under the terms of the GNU Affero General Public License as published by |
10 | // the Free Software Foundation, either version 3 of the License, or |
11 | // (at your option) any later version. |
12 | // |
13 | // This program is distributed in the hope that it will be useful, |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | // GNU Affero General Public License for more details. |
17 | // |
18 | // You should have received a copy of the GNU Affero General Public License |
19 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | // |
21 | |
22 | // |
23 | // Parameters for the router (generated using "--help-profile-js"). |
24 | // |
25 | |
26 | //////////////////////////////////////////////////////////////////////////////// |
27 | |
28 | // Transport types |
29 | var router_transports={foot: 1, horse: 2, wheelchair: 3, bicycle: 4, moped: 5, motorbike: 6, motorcar: 7, goods: 8, hgv: 9, psv: 10}; |
30 | |
31 | // Highway types |
32 | var router_highways={motorway: 1, trunk: 2, primary: 3, secondary: 4, tertiary: 5, unclassified: 6, residential: 7, service: 8, track: 9, cycleway: 10, path: 11, steps: 12}; |
33 | |
34 | // Property types |
35 | var router_properties={paved: 1, multilane: 2, bridge: 3, tunnel: 4}; |
36 | |
37 | // Restriction types |
38 | var router_restrictions={oneway: 1, weight: 2, height: 3, width: 4, length: 5}; |
39 | |
40 | // Allowed highways |
41 | var router_profile_highway={ |
42 | motorway: {foot: 0, horse: 0, wheelchair: 0, bicycle: 0, moped: 0, motorbike: 100, motorcar: 100, goods: 100, hgv: 100, psv: 100}, |
43 | trunk: {foot: 40, horse: 25, wheelchair: 40, bicycle: 30, moped: 90, motorbike: 100, motorcar: 100, goods: 100, hgv: 100, psv: 100}, |
44 | primary: {foot: 50, horse: 50, wheelchair: 50, bicycle: 70, moped: 100, motorbike: 90, motorcar: 90, goods: 90, hgv: 90, psv: 90}, |
45 | secondary: {foot: 60, horse: 50, wheelchair: 60, bicycle: 80, moped: 90, motorbike: 80, motorcar: 80, goods: 80, hgv: 80, psv: 80}, |
46 | tertiary: {foot: 70, horse: 75, wheelchair: 70, bicycle: 90, moped: 80, motorbike: 70, motorcar: 70, goods: 70, hgv: 70, psv: 70}, |
47 | unclassified: {foot: 80, horse: 75, wheelchair: 80, bicycle: 90, moped: 70, motorbike: 60, motorcar: 60, goods: 60, hgv: 60, psv: 60}, |
48 | residential: {foot: 90, horse: 75, wheelchair: 90, bicycle: 90, moped: 60, motorbike: 50, motorcar: 50, goods: 50, hgv: 50, psv: 50}, |
49 | service: {foot: 90, horse: 75, wheelchair: 90, bicycle: 90, moped: 80, motorbike: 80, motorcar: 80, goods: 80, hgv: 80, psv: 80}, |
50 | track: {foot: 95, horse: 100, wheelchair: 95, bicycle: 90, moped: 0, motorbike: 0, motorcar: 0, goods: 0, hgv: 0, psv: 0}, |
51 | cycleway: {foot: 95, horse: 90, wheelchair: 95, bicycle: 100, moped: 0, motorbike: 0, motorcar: 0, goods: 0, hgv: 0, psv: 0}, |
52 | path: {foot: 100, horse: 100, wheelchair: 100, bicycle: 90, moped: 0, motorbike: 0, motorcar: 0, goods: 0, hgv: 0, psv: 0}, |
53 | steps: {foot: 80, horse: 0, wheelchair: 0, bicycle: 0, moped: 0, motorbike: 0, motorcar: 0, goods: 0, hgv: 0, psv: 0} |
54 | }; |
55 | |
56 | // Speed limits |
57 | var router_profile_speed={ |
58 | motorway: {foot: 0, horse: 0, wheelchair: 0, bicycle: 0, moped: 48, motorbike: 112, motorcar: 112, goods: 96, hgv: 89, psv: 89}, |
59 | trunk: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 48, motorbike: 96, motorcar: 96, goods: 96, hgv: 80, psv: 80}, |
60 | primary: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 48, motorbike: 96, motorcar: 96, goods: 96, hgv: 80, psv: 80}, |
61 | secondary: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 48, motorbike: 88, motorcar: 88, goods: 88, hgv: 80, psv: 80}, |
62 | tertiary: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 48, motorbike: 80, motorcar: 80, goods: 80, hgv: 80, psv: 80}, |
63 | unclassified: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 48, motorbike: 64, motorcar: 64, goods: 64, hgv: 64, psv: 64}, |
64 | residential: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 48, motorbike: 48, motorcar: 48, goods: 48, hgv: 48, psv: 48}, |
65 | service: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 32, motorbike: 32, motorcar: 32, goods: 32, hgv: 32, psv: 32}, |
66 | track: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 16, motorbike: 16, motorcar: 16, goods: 16, hgv: 16, psv: 16}, |
67 | cycleway: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 0, motorbike: 0, motorcar: 0, goods: 0, hgv: 0, psv: 0}, |
68 | path: {foot: 4, horse: 8, wheelchair: 4, bicycle: 20, moped: 0, motorbike: 0, motorcar: 0, goods: 0, hgv: 0, psv: 0}, |
69 | steps: {foot: 4, horse: 0, wheelchair: 4, bicycle: 0, moped: 0, motorbike: 0, motorcar: 0, goods: 0, hgv: 0, psv: 0} |
70 | }; |
71 | |
72 | // Highway properties |
73 | var router_profile_property={ |
74 | paved: {foot: 50, horse: 20, wheelchair: 90, bicycle: 50, moped: 100, motorbike: 100, motorcar: 100, goods: 100, hgv: 100, psv: 100}, |
75 | multilane: {foot: 25, horse: 25, wheelchair: 25, bicycle: 25, moped: 25, motorbike: 75, motorcar: 75, goods: 75, hgv: 75, psv: 75}, |
76 | bridge: {foot: 50, horse: 50, wheelchair: 50, bicycle: 50, moped: 50, motorbike: 50, motorcar: 50, goods: 50, hgv: 50, psv: 50}, |
77 | tunnel: {foot: 50, horse: 50, wheelchair: 50, bicycle: 50, moped: 50, motorbike: 50, motorcar: 50, goods: 50, hgv: 50, psv: 50} |
78 | }; |
79 | |
80 | // Restrictions |
81 | var router_profile_restrictions={ |
82 | oneway: {foot: 0, horse: 1, wheelchair: 0, bicycle: 1, moped: 1, motorbike: 1, motorcar: 1, goods: 1, hgv: 1, psv: 1}, |
83 | weight: {foot: 0.0, horse: 0.0, wheelchair: 0.0, bicycle: 0.0, moped: 0.0, motorbike: 0.0, motorcar: 0.0, goods: 5.0, hgv: 10.0, psv: 15.0}, |
84 | height: {foot: 0.0, horse: 0.0, wheelchair: 0.0, bicycle: 0.0, moped: 0.0, motorbike: 0.0, motorcar: 0.0, goods: 2.5, hgv: 3.0, psv: 3.0}, |
85 | width: {foot: 0.0, horse: 0.0, wheelchair: 0.0, bicycle: 0.0, moped: 0.0, motorbike: 0.0, motorcar: 0.0, goods: 2.0, hgv: 2.5, psv: 2.5}, |
86 | length: {foot: 0.0, horse: 0.0, wheelchair: 0.0, bicycle: 0.0, moped: 0.0, motorbike: 0.0, motorcar: 0.0, goods: 5.0, hgv: 6.0, psv: 6.0} |
87 | }; |
88 | |
89 | //////////////////////////////////////////////////////////////////////////////// |
90 | |
91 | // Currently selected transport |
92 | var router_transport=false; |
93 | |
94 | // List of route points |
95 | var routepoints={shortest: {}, quickest: {}}; |
96 | |
97 | |
98 | // |
99 | // Form initialisation - fill in the uninitialised parts |
100 | // |
101 | |
102 | function form_init() |
103 | { |
104 | var key; |
105 | |
106 | for(key in router_transports) |
107 | if(document.forms["form"].elements["transport"][router_transports[key]-1].checked) |
108 | router_transport=key; |
109 | |
110 | if(!router_transport) |
111 | formSetTransport('motorcar'); |
112 | else |
113 | { |
114 | for(key in router_profile_highway) |
115 | { |
116 | if(document.forms["form"].elements["highway-" + key].value=="") |
117 | document.forms["form"].elements["highway-" + key].value=router_profile_highway[key][router_transport]; |
118 | else |
119 | formSetHighway(key); |
120 | } |
121 | |
122 | for(key in router_profile_speed) |
123 | { |
124 | if(document.forms["form"].elements["speed-" + key].value=="") |
125 | document.forms["form"].elements["speed-" + key].value=router_profile_speed[key][router_transport]; |
126 | else |
127 | formSetSpeed(key); |
128 | } |
129 | |
130 | for(key in router_profile_property) |
131 | { |
132 | if(document.forms["form"].elements["property-" + key].value=="") |
133 | document.forms["form"].elements["property-" + key].value=router_profile_property[key][router_transport]; |
134 | else |
135 | formSetProperty(key); |
136 | } |
137 | |
138 | for(key in router_restrictions) |
139 | { |
140 | if(key=="oneway") |
141 | formSetRestriction(key); |
142 | else |
143 | { |
144 | if(document.forms["form"].elements["restrict-" + key].value=="") |
145 | document.forms["form"].elements["restrict-" + key].value=router_profile_restrictions[key][router_transport]; |
146 | else |
147 | formSetRestriction(key); |
148 | } |
149 | } |
150 | } |
151 | |
152 | for(var marker=nmarkers;marker>=1;marker--) |
153 | { |
154 | var lon=document.forms["form"].elements["lon" + marker].value; |
155 | var lat=document.forms["form"].elements["lat" + marker].value; |
156 | |
157 | if(lon != "" && lat != "") |
158 | markerAddMap(marker); |
159 | else |
160 | markerRemove(marker); |
161 | } |
162 | |
163 | updateCustomURL(); |
164 | } |
165 | |
166 | |
167 | // |
168 | // Change of transport in the form |
169 | // |
170 | |
171 | function formSetTransport(type) |
172 | { |
173 | var key; |
174 | |
175 | router_transport=type; |
176 | |
177 | for(key in router_transports) |
178 | document.forms["form"].elements["transport"][router_transports[key]-1].checked=(key==router_transport); |
179 | |
180 | for(key in router_profile_highway) |
181 | document.forms["form"].elements["highway-" + key].value=router_profile_highway[key][router_transport]; |
182 | |
183 | for(key in router_profile_speed) |
184 | document.forms["form"].elements["speed-" + key].value=router_profile_speed[key][router_transport]; |
185 | |
186 | for(key in router_profile_property) |
187 | document.forms["form"].elements["property-" + key].value=router_profile_property[key][router_transport]; |
188 | |
189 | for(key in router_restrictions) |
190 | { |
191 | if(key=="oneway") |
192 | document.forms["form"].elements["restrict-" + key].checked=router_profile_restrictions[key][router_transport]; |
193 | else |
194 | document.forms["form"].elements["restrict-" + key].value=router_profile_restrictions[key][router_transport]; |
195 | } |
196 | |
197 | paramschanged=true; |
198 | |
199 | updateCustomURL(); |
200 | } |
201 | |
202 | |
203 | // |
204 | // Change of highway in the form |
205 | // |
206 | |
207 | function formSetHighway(type) |
208 | { |
209 | router_profile_highway[type][router_transport]=document.forms["form"].elements["highway-" + type].value; |
210 | |
211 | paramschanged=true; |
212 | |
213 | updateCustomURL(); |
214 | } |
215 | |
216 | |
217 | // |
218 | // Change of Speed in the form |
219 | // |
220 | |
221 | function formSetSpeed(type) |
222 | { |
223 | router_profile_speed[type][router_transport]=document.forms["form"].elements["speed-" + type].value; |
224 | |
225 | paramschanged=true; |
226 | |
227 | updateCustomURL(); |
228 | } |
229 | |
230 | |
231 | // |
232 | // Change of Property in the form |
233 | // |
234 | |
235 | function formSetProperty(type) |
236 | { |
237 | router_profile_property[type][router_transport]=document.forms["form"].elements["property-" + type].value; |
238 | |
239 | paramschanged=true; |
240 | |
241 | updateCustomURL(); |
242 | } |
243 | |
244 | |
245 | // |
246 | // Change of oneway rule in the form |
247 | // |
248 | |
249 | function formSetRestriction(type) |
250 | { |
251 | if(type=="oneway") |
252 | router_profile_restrictions[type][router_transport]=document.forms["form"].elements["restrict-" + type].checked; |
253 | else |
254 | router_profile_restrictions[type][router_transport]=document.forms["form"].elements["restrict-" + type].value; |
255 | |
256 | paramschanged=true; |
257 | |
258 | updateCustomURL(); |
259 | } |
260 | |
261 | |
262 | // |
263 | // Update custom URL |
264 | // |
265 | |
266 | function updateCustomURL() |
267 | { |
268 | var custom_url=document.getElementById("custom_url"); |
269 | var visualiser_url=document.getElementById("visualiser_url"); |
270 | |
271 | custom_url.href="customrouter.cgi" + buildURLArguments(1) + ";" + map_args; |
272 | |
273 | visualiser_url.href="customvisualiser.cgi?" + map_args; |
274 | } |
275 | |
276 | |
277 | // |
278 | // Block the use of the return key to submit the form |
279 | // |
280 | |
281 | function block_return_key() |
282 | { |
283 | var form=document.getElementById("form"); |
284 | |
285 | if(form.addEventListener) |
286 | form.addEventListener('keyup', discardReturnKey, false); |
287 | else if(form.attachEvent) |
288 | form.attachEvent('keyup', discardReturnKey); // Internet Explorer |
289 | } |
290 | |
291 | // |
292 | // Function to discard the return key if pressed |
293 | // |
294 | |
295 | function discardReturnKey(ev) |
296 | { |
297 | if(ev.keyCode==13) |
298 | return(false); |
299 | |
300 | return(true); |
301 | } |
302 | |
303 | |
304 | // |
305 | // Map configuration |
306 | // |
307 | |
308 | var map; |
309 | var layerMapOSM, layerVectors, layerGPX; |
310 | var epsg4326, epsg900913; |
311 | var map_args; |
312 | |
313 | var nmarkers, vismarkers, markers, markersmoved, paramschanged; |
314 | var highlights={shortest: null, quickest: null}; |
315 | var popups={shortest: null, quickest: null}; |
316 | var gpx_style; |
317 | |
318 | |
319 | // |
320 | // Initialise the 'map' object |
321 | // |
322 | |
323 | function map_init(lat,lon,zoom) |
324 | { |
325 | // |
326 | // Create the map |
327 | // |
328 | |
329 | epsg4326=new OpenLayers.Projection("EPSG:4326"); |
330 | epsg900913=new OpenLayers.Projection("EPSG:900913"); |
331 | |
332 | // UK coordinate range: West -11.0, South 49.5, East 2.0, North 61.0 |
333 | |
334 | // EDIT THIS to change the visible map boundary. |
335 | var mapbounds=new OpenLayers.Bounds(-11.0,49.5,2.0,61.0).transform(epsg4326,epsg900913); |
336 | |
337 | map = new OpenLayers.Map ("map", |
338 | { |
339 | controls:[ |
340 | new OpenLayers.Control.Navigation(), |
341 | new OpenLayers.Control.PanZoomBar(), |
342 | new OpenLayers.Control.ScaleLine(), |
343 | new OpenLayers.Control.LayerSwitcher() |
344 | ], |
345 | |
346 | projection: epsg900913, |
347 | displayProjection: epsg4326, |
348 | |
349 | // EDIT THIS to set the minimum zoom level |
350 | minZoomLevel: 4, |
351 | |
352 | // EDIT THIS to set the number of zoom levels |
353 | numZoomLevels: 12, // zoom levels 4-15 inclusive |
354 | |
355 | // EDIT THIS if you change the minimum zoom level above |
356 | maxResolution: 156543.0339 / Math.pow(2,4), // Math.pow(2,minZoomLevel) |
357 | |
358 | maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34), |
359 | |
360 | restrictedExtent: mapbounds, |
361 | |
362 | units: "m" |
363 | }); |
364 | |
365 | map.events.register("moveend", map, mapMoved); |
366 | |
367 | // Add a map tile layer (OpenStreetMap tiles, direct access) |
368 | |
369 | layerMapOSM = new OpenLayers.Layer.TMS("Original OSM map", |
370 | "http://tile.openstreetmap.org/", |
371 | { |
372 | emptyUrl: "http://openstreetmap.org/openlayers/img/404.png", |
373 | type: 'png', |
374 | getURL: limitedUrl, |
375 | displayOutsideMaxExtent: true, |
376 | buffer: 1 |
377 | }); |
378 | map.addLayer(layerMapOSM); |
379 | |
380 | // Get a URL for the tile; limited to mapbounds. |
381 | |
382 | function limitedUrl(bounds) |
383 | { |
384 | var z = map.getZoom() + map.minZoomLevel; |
385 | |
386 | if (z>=7 && (bounds.right < mapbounds.left || |
387 | bounds.left > mapbounds.right || |
388 | bounds.top < mapbounds.bottom || |
389 | bounds.bottom > mapbounds.top)) |
390 | return this.emptyUrl; |
391 | |
392 | var res = map.getResolution(); |
393 | var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h)); |
394 | var limit = Math.pow(2, z); |
395 | |
396 | if (y < 0 || y >= limit) |
397 | return this.emptyUrl; |
398 | |
399 | var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w)); |
400 | |
401 | x = ((x % limit) + limit) % limit; |
402 | return this.url + z + "/" + x + "/" + y + "." + this.type; |
403 | } |
404 | |
405 | // Define a GPX layer but don't add it yet |
406 | |
407 | layerGPX={shortest: null, quickest: null}; |
408 | |
409 | gpx_style={shortest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#00FF00"}), |
410 | quickest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#0000FF"})}; |
411 | |
412 | // Add a vectors layer |
413 | |
414 | layerVectors = new OpenLayers.Layer.Vector("Markers"); |
415 | map.addLayer(layerVectors); |
416 | |
417 | // A set of markers |
418 | |
419 | nmarkers=99; |
420 | vismarkers=0; |
421 | markers={}; |
422 | markersmoved=false; |
423 | paramschanged=false; |
424 | |
425 | for(var marker=1;marker<=nmarkers;marker++) |
426 | { |
427 | if(document.forms["form"].elements["lon" + marker] != undefined) |
428 | { |
429 | markers[marker] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{}, |
430 | new OpenLayers.Style({},{externalGraphic: 'icons/marker-' + marker + '-red.png', |
431 | fillColor: "white", |
432 | graphicYOffset: -25, |
433 | graphicWidth: 21, |
434 | graphicHeight: 25, |
435 | display: "none"})); |
436 | |
437 | layerVectors.addFeatures([markers[marker]]); |
438 | } |
439 | else |
440 | { |
441 | nmarkers=marker-1; |
442 | vismarkers=marker-1; |
443 | break; |
444 | } |
445 | } |
446 | |
447 | // A function to drag the markers |
448 | |
449 | var drag = new OpenLayers.Control.DragFeature(layerVectors, |
450 | {onDrag: dragMove, |
451 | onComplete: dragComplete }); |
452 | map.addControl(drag); |
453 | drag.activate(); |
454 | |
455 | // Colours for the route highlights and popups |
456 | |
457 | route_colours={shortest: "#60C060", quickest: "#6060C0"}; |
458 | |
459 | // Markers to highlight a selected point |
460 | |
461 | var highlight; |
462 | |
463 | for(highlight in highlights) |
464 | { |
465 | highlights[highlight] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{}, |
466 | new OpenLayers.Style({},{strokeColor: route_colours[highlight], |
467 | fillColor: "white", |
468 | pointRadius: 10, |
469 | strokeWidth: 4, |
470 | fillOpacity: 0, |
471 | display: "none"})); |
472 | |
473 | layerVectors.addFeatures([highlights[highlight]]); |
474 | } |
475 | |
476 | // A popup for routing results |
477 | |
478 | var popup; |
479 | |
480 | for(popup in popups) |
481 | { |
482 | popups[popup] = new OpenLayers.Popup(popup, |
483 | new OpenLayers.LonLat(mapbounds.left,mapbounds.bottom), |
484 | new OpenLayers.Size(map.getSize().w-100,60), |
485 | popup, |
486 | false); |
487 | |
488 | popups[popup].keepInMap=true; |
489 | popups[popup].panMapIfOutOfView=false; |
490 | popups[popup].setOpacity(0.75); |
491 | popups[popup].setBackgroundColor(route_colours[popup]); |
492 | popups[popup].setBorder("2px"); |
493 | } |
494 | |
495 | // Set the map centre to the limited range specified |
496 | |
497 | map.setCenter(mapbounds.getCenterLonLat(), map.getZoomForExtent(mapbounds,true)); |
498 | map.maxResolution = map.getResolution(); |
499 | |
500 | // Move the map |
501 | |
502 | if(lon != 'lon' && lat != 'lat' && zoom != 'zoom') |
503 | { |
504 | var lonlat = new OpenLayers.LonLat(lon,lat).transform(epsg4326,map.getProjectionObject()); |
505 | |
506 | map.moveTo(lonlat,zoom-map.minZoomLevel); |
507 | } |
508 | } |
509 | |
510 | |
511 | // |
512 | // Map has moved |
513 | // |
514 | |
515 | function mapMoved() |
516 | { |
517 | var centre = map.getCenter().clone(); |
518 | |
519 | var lonlat = centre.transform(map.getProjectionObject(),epsg4326); |
520 | |
521 | var zoom = this.getZoom() + map.minZoomLevel; |
522 | |
523 | map_args="lat=" + lonlat.lat + ";lon=" + lonlat.lon + ";zoom=" + zoom; |
524 | |
525 | updateCustomURL(); |
526 | } |
527 | |
528 | |
529 | // |
530 | // OpenLayers.Control.DragFeature callback for a drag occuring. |
531 | // |
532 | |
533 | function dragMove(feature,pixel) |
534 | { |
535 | for(var marker in markers) |
536 | if(feature==markers[marker]) |
537 | { |
538 | markersmoved=true; |
539 | |
540 | coordsSetForm(marker); |
541 | } |
542 | } |
543 | |
544 | |
545 | // |
546 | // OpenLayers.Control.DragFeature callback for completing a drag. |
547 | // |
548 | |
549 | function dragComplete(feature,pixel) |
550 | { |
551 | for(var marker in markers) |
552 | if(feature==markers[marker]) |
553 | { |
554 | markersmoved=true; |
555 | |
556 | coordsSetForm(marker); |
557 | } |
558 | } |
559 | |
560 | |
561 | // |
562 | // Toggle a marker on the map. |
563 | // |
564 | |
565 | function markerToggleMap(marker) |
566 | { |
567 | var feature=markers[marker]; |
568 | |
569 | if(feature.style.display == "") |
570 | markerRemoveMap(marker); |
571 | else |
572 | markerAddMap(marker); |
573 | } |
574 | |
575 | |
576 | // |
577 | // Show a marker on the map. |
578 | // |
579 | |
580 | function markerAddMap(marker) |
581 | { |
582 | var feature=markers[marker]; |
583 | |
584 | feature.style.display = ""; |
585 | document.images["waypoint" + marker].src="icons/marker-" + marker + "-red.png"; |
586 | |
587 | formSetCoords(marker); |
588 | |
589 | layerVectors.drawFeature(feature); |
590 | |
591 | markersmoved=true; |
592 | } |
593 | |
594 | |
595 | // |
596 | // Remove a marker from the map. |
597 | // |
598 | |
599 | function markerRemoveMap(marker) |
600 | { |
601 | var feature=markers[marker]; |
602 | |
603 | feature.style.display = "none"; |
604 | document.images["waypoint" + marker].src="icons/marker-" + marker + "-grey.png"; |
605 | |
606 | layerVectors.drawFeature(feature); |
607 | |
608 | markersmoved=true; |
609 | } |
610 | |
611 | |
612 | // |
613 | // Clear the current marker. |
614 | // |
615 | |
616 | function markerRemove(marker) |
617 | { |
618 | for(var marker2=marker;marker2<vismarkers;marker2++) |
619 | { |
620 | document.forms["form"].elements["lon" + marker2].value=document.forms["form"].elements["lon" + (marker2+1)].value; |
621 | document.forms["form"].elements["lat" + marker2].value=document.forms["form"].elements["lat" + (marker2+1)].value; |
622 | |
623 | if(markers[marker2+1].style.display=="") |
624 | markerAddMap(marker2); |
625 | else |
626 | markerRemoveMap(marker2); |
627 | } |
628 | |
629 | markerRemoveMap(vismarkers); |
630 | |
631 | var marker_tr=document.getElementById("point" + vismarkers); |
632 | |
633 | marker_tr.style.display="none"; |
634 | |
635 | vismarkers--; |
636 | |
637 | if(vismarkers==1) |
638 | markerAddAfter(1); |
639 | |
640 | updateCustomURL(); |
641 | } |
642 | |
643 | |
644 | // |
645 | // Add a marker before the current one. |
646 | // |
647 | |
648 | function markerAddBefore(marker) |
649 | { |
650 | if(vismarkers==nmarkers || marker==1) |
651 | return false; |
652 | |
653 | vismarkers++; |
654 | |
655 | var marker_tr=document.getElementById("point" + vismarkers); |
656 | |
657 | marker_tr.style.display=""; |
658 | |
659 | for(var marker2=vismarkers;marker2>marker;marker2--) |
660 | { |
661 | document.forms["form"].elements["lon" + marker2].value=document.forms["form"].elements["lon" + (marker2-1)].value; |
662 | document.forms["form"].elements["lat" + marker2].value=document.forms["form"].elements["lat" + (marker2-1)].value; |
663 | |
664 | if(markers[marker2-1].style.display=="") |
665 | markerAddMap(marker2); |
666 | else |
667 | markerRemoveMap(marker2); |
668 | } |
669 | |
670 | document.forms["form"].elements["lon" + marker].value=""; |
671 | document.forms["form"].elements["lat" + marker].value=""; |
672 | markers[marker].style.display="none"; |
673 | |
674 | markerRemoveMap(marker); |
675 | |
676 | updateCustomURL(); |
677 | } |
678 | |
679 | |
680 | // |
681 | // Add a marker after the current one. |
682 | // |
683 | |
684 | function markerAddAfter(marker) |
685 | { |
686 | if(vismarkers==nmarkers) |
687 | return false; |
688 | |
689 | vismarkers++; |
690 | |
691 | var marker_tr=document.getElementById("point" + vismarkers); |
692 | |
693 | marker_tr.style.display=""; |
694 | |
695 | for(var marker2=vismarkers;marker2>(marker+1);marker2--) |
696 | { |
697 | document.forms["form"].elements["lon" + marker2].value=document.forms["form"].elements["lon" + (marker2-1)].value; |
698 | document.forms["form"].elements["lat" + marker2].value=document.forms["form"].elements["lat" + (marker2-1)].value; |
699 | |
700 | if(markers[marker2-1].style.display=="") |
701 | markerAddMap(marker2); |
702 | else |
703 | markerRemoveMap(marker2); |
704 | } |
705 | |
706 | document.forms["form"].elements["lon" + (marker+1)].value=""; |
707 | document.forms["form"].elements["lat" + (marker+1)].value=""; |
708 | markers[marker+1].style.display="none"; |
709 | |
710 | markerRemoveMap(marker+1); |
711 | |
712 | updateCustomURL(); |
713 | } |
714 | |
715 | |
716 | // |
717 | // Move this marker up. |
718 | // |
719 | |
720 | function markerMoveUp(marker) |
721 | { |
722 | if(marker==1) |
723 | return false; |
724 | |
725 | markerSwap(marker,marker-1); |
726 | } |
727 | |
728 | |
729 | // |
730 | // Move this marker down. |
731 | // |
732 | |
733 | function markerMoveDown(marker) |
734 | { |
735 | if(marker==vismarkers) |
736 | return false; |
737 | |
738 | markerSwap(marker,marker+1); |
739 | } |
740 | |
741 | |
742 | // |
743 | // Swap a pair of markers. |
744 | // |
745 | |
746 | function markerSwap(marker1,marker2) |
747 | { |
748 | var lon=document.forms["form"].elements["lon" + marker1].value; |
749 | var lat=document.forms["form"].elements["lat" + marker1].value; |
750 | var display=markers[marker1].style.display; |
751 | |
752 | document.forms["form"].elements["lon" + marker1].value=document.forms["form"].elements["lon" + marker2].value; |
753 | document.forms["form"].elements["lat" + marker1].value=document.forms["form"].elements["lat" + marker2].value; |
754 | if(markers[marker2].style.display=="") |
755 | markerAddMap(marker1); |
756 | else |
757 | markerRemoveMap(marker1); |
758 | |
759 | document.forms["form"].elements["lon" + marker2].value=lon; |
760 | document.forms["form"].elements["lat" + marker2].value=lat; |
761 | if(display=="") |
762 | markerAddMap(marker2); |
763 | else |
764 | markerRemoveMap(marker2); |
765 | |
766 | updateCustomURL(); |
767 | } |
768 | |
769 | |
770 | // |
771 | // Reverse the markers. |
772 | // |
773 | |
774 | function markersReverse() |
775 | { |
776 | for(var marker=1;marker<=vismarkers/2;marker++) |
777 | markerSwap(marker,vismarkers+1-marker); |
778 | |
779 | updateCustomURL(); |
780 | } |
781 | |
782 | |
783 | // |
784 | // Set the feature coordinates in the form. |
785 | // |
786 | |
787 | function coordsSetForm(marker) |
788 | { |
789 | var feature=markers[marker]; |
790 | |
791 | var lonlat = new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y); |
792 | lonlat.transform(map.getProjectionObject(),epsg4326); |
793 | |
794 | document.forms["form"].elements["lon" + marker].value=format5f(lonlat.lon); |
795 | document.forms["form"].elements["lat" + marker].value=format5f(lonlat.lat); |
796 | |
797 | updateCustomURL(); |
798 | } |
799 | |
800 | |
801 | // |
802 | // Set the feature coordinates from the form when the form changes. |
803 | // |
804 | |
805 | function formSetCoords(marker) |
806 | { |
807 | var feature=markers[marker]; |
808 | |
809 | var lonlat=map.getCenter().clone(); |
810 | |
811 | lonlat.transform(map.getProjectionObject(),epsg4326); |
812 | |
813 | var lon=document.forms["form"].elements["lon" + marker].value; |
814 | var lat=document.forms["form"].elements["lat" + marker].value; |
815 | |
816 | if(lon!="") |
817 | { |
818 | if(lon<-180) lon=-180; |
819 | if(lon>+180) lon=+180; |
820 | lonlat.lon=lon; |
821 | } |
822 | |
823 | if(lat!="") |
824 | { |
825 | if(lat<-90 ) lat=-90 ; |
826 | if(lat>+90 ) lat=+90 ; |
827 | lonlat.lat=lat; |
828 | } |
829 | |
830 | var point = lonlat.clone(); |
831 | |
832 | point.transform(epsg4326,map.getProjectionObject()); |
833 | |
834 | feature.move(point); |
835 | |
836 | markersmoved=true; |
837 | |
838 | coordsSetForm(marker); |
839 | } |
840 | |
841 | |
842 | // |
843 | // Format a number in printf("%.5f") format. |
844 | // |
845 | |
846 | function format5f(number) |
847 | { |
848 | var newnumber=Math.floor(number*100000+0.5); |
849 | var delta=0; |
850 | |
851 | if(newnumber>=0 && newnumber<100000) delta= 100000; |
852 | if(newnumber<0 && newnumber>-100000) delta=-100000; |
853 | |
854 | var string=String(newnumber+delta); |
855 | |
856 | var intpart =string.substring(0,string.length-5); |
857 | var fracpart=string.substring(string.length-5,string.length); |
858 | |
859 | if(delta>0) intpart="0"; |
860 | if(delta<0) intpart="-0"; |
861 | |
862 | return(intpart + "." + fracpart); |
863 | } |
864 | |
865 | |
866 | // |
867 | // Zoom to a specific item in the route |
868 | // |
869 | |
870 | function zoomTo(type,line) |
871 | { |
872 | var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat).transform(epsg4326,map.getProjectionObject()); |
873 | |
874 | map.moveTo(lonlat,map.numZoomLevels-2); |
875 | } |
876 | |
877 | |
878 | // |
879 | // Highlight a specific item in the route |
880 | // |
881 | |
882 | function highlight(type,line) |
883 | { |
884 | if(line==-1) |
885 | { |
886 | highlights[type].style.display = "none"; |
887 | |
888 | map.removePopup(popups[type]); |
889 | } |
890 | else |
891 | { |
892 | var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat).transform(epsg4326,map.getProjectionObject()); |
893 | |
894 | var popupstr=""; |
895 | |
896 | if(routepoints[type][line].text=="") |
897 | popupstr += "From: ".bold() + "Start Point".italics(); |
898 | else |
899 | popupstr += "After: ".bold() + routepoints[type][line].text + " [ " + routepoints[type][line].segdist + " , " + routepoints[type][line].segdura + " ] "; |
900 | |
901 | popupstr += "<br>"; |
902 | |
903 | if(routepoints[type][line].bearing==undefined) |
904 | popupstr += "Stop: ".bold(); |
905 | else if(routepoints[type][line].turn==undefined) |
906 | popupstr += "Go: ".bold() + routepoints[type][line].bearing; |
907 | else |
908 | popupstr += "Go: ".bold() + routepoints[type][line].turn + " (" + routepoints[type][line].bearing + ")"; |
909 | |
910 | if(routepoints[type][line].type=="Waypt") |
911 | popupstr += " [Waypoint]"; |
912 | |
913 | popupstr += "<br>"; |
914 | |
915 | if(routepoints[type][line+1]==undefined) |
916 | popupstr += "At: ".bold() + "Finish Point".italics(); |
917 | else |
918 | popupstr += "Onto: ".bold() + routepoints[type][line+1].text; |
919 | |
920 | popupstr += "<br>"; |
921 | |
922 | popupstr += "Total: ".bold() + routepoints[type][line].totdist + " , " + routepoints[type][line].totdura; |
923 | |
924 | popups[type].setContentHTML(popupstr); |
925 | |
926 | highlights[type].move(lonlat); |
927 | |
928 | if(highlights[type].style.display = "none") |
929 | { |
930 | highlights[type].style.display = ""; |
931 | |
932 | map.addPopup(popups[type]); |
933 | |
934 | var lonlat=new OpenLayers.LonLat(map.getCenter().lon,map.getExtent().top); |
935 | var position=map.getPixelFromLonLat(lonlat); |
936 | |
937 | position.x=position.x-popups[type].size.w/2; |
938 | position.y=position.y+50; |
939 | |
940 | popups[type].lonlat=map.getLonLatFromPixel(position); |
941 | popups[type].updatePosition(); |
942 | } |
943 | } |
944 | |
945 | layerVectors.drawFeature(highlights[type]); |
946 | } |
947 | |
948 | |
949 | // |
950 | // Build a set of URL arguments |
951 | // |
952 | |
953 | function buildURLArguments(all) |
954 | { |
955 | var url="?"; |
956 | |
957 | url=url + "transport=" + router_transport; |
958 | |
959 | for(var marker=1;marker<=vismarkers;marker++) |
960 | if(markers[marker].style.display == "" || all) |
961 | { |
962 | url=url + ";lon" + marker + "=" + document.forms["form"].elements["lon" + marker].value; |
963 | url=url + ";lat" + marker + "=" + document.forms["form"].elements["lat" + marker].value; |
964 | } |
965 | |
966 | for(key in router_profile_highway) |
967 | url=url + ";highway-" + key + "=" + document.forms["form"].elements["highway-" + key].value; |
968 | |
969 | for(key in router_profile_speed) |
970 | url=url + ";speed-" + key + "=" + document.forms["form"].elements["speed-" + key].value; |
971 | |
972 | for(key in router_profile_property) |
973 | url=url + ";property-" + key + "=" + document.forms["form"].elements["property-" + key].value; |
974 | |
975 | for(key in router_restrictions) |
976 | { |
977 | if(key=="oneway") |
978 | url=url + ";" + key + "=" + document.forms["form"].elements["restrict-" + key].checked; |
979 | else |
980 | url=url + ";" + key + "=" + document.forms["form"].elements["restrict-" + key].value; |
981 | } |
982 | |
983 | return(url); |
984 | } |
985 | |
986 | |
987 | // |
988 | // Display data statistics |
989 | // |
990 | |
991 | function displayStatistics() |
992 | { |
993 | // Use AJAX to get the statistics |
994 | |
995 | OpenLayers.loadURL("statistics.cgi",null,null,runStatisticsSuccess); |
996 | } |
997 | |
998 | |
999 | // |
1000 | // Success in running router. |
1001 | // |
1002 | |
1003 | function runStatisticsSuccess(response) |
1004 | { |
1005 | var statistics_data=document.getElementById("statistics_data"); |
1006 | var statistics_link=document.getElementById("statistics_link"); |
1007 | |
1008 | statistics_data.innerHTML="<pre>" + response.responseText + "</pre>"; |
1009 | |
1010 | statistics_link.style.display="none"; |
1011 | } |
1012 | |
1013 | |
1014 | // |
1015 | // Submit form - performing the routing |
1016 | // |
1017 | |
1018 | function findRoute(type) |
1019 | { |
1020 | tab_select("results"); |
1021 | |
1022 | hideshow_hide('help_options'); |
1023 | hideshow_hide('shortest'); |
1024 | hideshow_hide('quickest'); |
1025 | |
1026 | var div_status=document.getElementById("result_status"); |
1027 | div_status.innerHTML = "Running..."; |
1028 | |
1029 | var url="router.cgi" + buildURLArguments(0) + ";type=" + type; |
1030 | |
1031 | // Destroy the existing layer(s) |
1032 | |
1033 | if(markersmoved || paramschanged) |
1034 | { |
1035 | if(layerGPX.shortest!=null) |
1036 | removeGPXTrace("shortest"); |
1037 | if(layerGPX.quickest!=null) |
1038 | removeGPXTrace("quickest"); |
1039 | markersmoved=false; |
1040 | paramschanged=false; |
1041 | } |
1042 | else if(layerGPX[type]!=null) |
1043 | removeGPXTrace(type); |
1044 | |
1045 | // Use AJAX to run the router |
1046 | |
1047 | routing_type=type; |
1048 | |
1049 | OpenLayers.loadURL(url,null,null,runRouterSuccess,runRouterFailure); |
1050 | } |
1051 | |
1052 | |
1053 | // |
1054 | // Remove a GPX trace |
1055 | // |
1056 | |
1057 | function removeGPXTrace(type) |
1058 | { |
1059 | map.removeLayer(layerGPX[type]); |
1060 | layerGPX[type].destroy(); |
1061 | layerGPX[type]=null; |
1062 | |
1063 | var span_status=document.getElementById(type + "_status"); |
1064 | span_status.innerHTML = "No Information".italics(); |
1065 | |
1066 | var div_data=document.getElementById(type + "_data"); |
1067 | div_data.innerHTML = ""; |
1068 | |
1069 | var div_text=document.getElementById(type + "_text"); |
1070 | div_text.innerHTML = ""; |
1071 | |
1072 | hideshow_hide(type); |
1073 | } |
1074 | |
1075 | |
1076 | // |
1077 | // Success in running router. |
1078 | // |
1079 | |
1080 | function runRouterSuccess(response) |
1081 | { |
1082 | var lines=response.responseText.split('\n'); |
1083 | |
1084 | var uuid=lines[0]; |
1085 | var cpuinfo=lines[1]; |
1086 | var distinfo=lines[2]; |
1087 | var message=lines[3]; |
1088 | |
1089 | // Update the status message |
1090 | |
1091 | var div_status=document.getElementById("result_status"); |
1092 | |
1093 | if(message!="") |
1094 | { |
1095 | div_status.innerHTML = message.bold() + "<br>" + cpuinfo.small(); |
1096 | hideshow_show('help_route'); |
1097 | return; |
1098 | } |
1099 | else |
1100 | { |
1101 | div_status.innerHTML = "Routing Completed".bold() + "<br>" + cpuinfo.small(); |
1102 | hideshow_hide('help_route'); |
1103 | } |
1104 | |
1105 | // Update the routing result message |
1106 | |
1107 | var span_status=document.getElementById(routing_type + "_status"); |
1108 | span_status.innerHTML = distinfo.bold(); |
1109 | |
1110 | var div_data=document.getElementById(routing_type + "_data"); |
1111 | |
1112 | var result="<table>"; |
1113 | url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track"; |
1114 | result=result + "<tr><td>GPX track file:<td onclick='window.open(\"" + url + "\")'><span>Open Popup</span><td>"; |
1115 | url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-route"; |
1116 | result=result + "<tr><td>GPX route file:<td onclick='window.open(\"" + url + "\")'><span>Open Popup</span><td>"; |
1117 | url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=txt-all"; |
1118 | result=result + "<tr><td>Full text file:<td onclick='window.open(\"" + url + "\")'><span>Open Popup</span><td>"; |
1119 | url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=txt"; |
1120 | result=result + "<tr><td>Text file:<td onclick='window.open(\"" + url + "\")'><span>Open Popup</span>"; |
1121 | result=result + "</table>"; |
1122 | |
1123 | div_data.innerHTML=result; |
1124 | |
1125 | // Add a GPX layer |
1126 | |
1127 | var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track"; |
1128 | |
1129 | layerGPX[routing_type] = new OpenLayers.Layer.GML("GPX (" + routing_type + ")", url, |
1130 | { |
1131 | format: OpenLayers.Format.GPX, |
1132 | style: gpx_style[routing_type], |
1133 | projection: map.displayProjection |
1134 | }); |
1135 | |
1136 | map.addLayer(layerGPX[routing_type]); |
1137 | |
1138 | hideshow_show(routing_type); |
1139 | |
1140 | displayResult(routing_type,uuid); |
1141 | } |
1142 | |
1143 | |
1144 | // |
1145 | // Failure in running router. |
1146 | // |
1147 | |
1148 | function runRouterFailure(response) |
1149 | { |
1150 | var div_status=document.getElementById("result_status"); |
1151 | div_status.innerHTML = String("Failed to run router!").bold(); |
1152 | } |
1153 | |
1154 | |
1155 | // |
1156 | // Display the route |
1157 | // |
1158 | |
1159 | function displayResult(type,uuid) |
1160 | { |
1161 | routing_type = type; |
1162 | |
1163 | // Add the route |
1164 | |
1165 | var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=txt"; |
1166 | |
1167 | // Use AJAX to get the route |
1168 | |
1169 | OpenLayers.loadURL(url,null,null,getRouteSuccess,getRouteFailure); |
1170 | } |
1171 | |
1172 | |
1173 | // |
1174 | // Success in getting route. |
1175 | // |
1176 | |
1177 | var turn_instruct={"-4": "Very sharp left", "-3": "Sharp left", "-2": "Left", "-1": "Slight left", |
1178 | "0": "Straight on", |
1179 | "4": "Very sharp right", "3": "Sharp right", "2": "Right", "1": "Slight right"}; |
1180 | |
1181 | var bearing_instruct={"-4": "South", "-3": "South-West", "-2": "West", "-1": "North-West", |
1182 | "0": "North", |
1183 | "4": "South", "3": "South-East", "2": "East", "1": "North-East"}; |
1184 | |
1185 | function getRouteSuccess(response) |
1186 | { |
1187 | var lines=response.responseText.split('\n'); |
1188 | var div_text=document.getElementById(routing_type + "_text"); |
1189 | |
1190 | routepoints[routing_type]={}; |
1191 | |
1192 | var result="<hr><table onmouseout='highlight(\"" + routing_type + "\",-1)'>"; |
1193 | |
1194 | var lat,lon,segdist,segdura,totdist,totdura,turn,text; |
1195 | |
1196 | for(line in lines) |
1197 | { |
1198 | var words=lines[line].split('\t'); |
1199 | |
1200 | // fprintf(textfile,"#Latitude\tLongitude\tSection \tSection \tTotal \tTotal \tPoint\tTurn\tBearing\tHighway\n"); |
1201 | // fprintf(textfile,"# \t \tDistance\tDuration\tDistance\tDuration\tType \t \t \t \n"); |
1202 | |
1203 | if(lines[line].match(/^#/)) |
1204 | ; |
1205 | else if(words[1]==undefined) |
1206 | ; |
1207 | else |
1208 | { |
1209 | routepoints[routing_type][line]={lat: Number(words[0]), lon: Number(words[1]), |
1210 | segdist: words[2], segdura: words[3], |
1211 | totdist: words[4], totdura: words[5], |
1212 | type: words[6], text: words[9]}; |
1213 | |
1214 | if(words[7] != "") |
1215 | routepoints[routing_type][line].turn=turn_instruct[Number(words[7])]; |
1216 | |
1217 | if(words[8] != "") |
1218 | routepoints[routing_type][line].bearing=bearing_instruct[Number(words[8])]; |
1219 | |
1220 | if(words[9]=="") |
1221 | words[9]="Start Point".italics(); |
1222 | |
1223 | result=result + "<tr onclick='zoomTo(\"" + routing_type + "\"," + line + ")'" + |
1224 | " onmouseover='highlight(\"" + routing_type + "\"," + line + ")'>" + |
1225 | "<td class='right'>" + words[4] + |
1226 | "<td class='right'>" + words[5] + |
1227 | "<td class='left'>" + " " + words[9]; |
1228 | } |
1229 | } |
1230 | |
1231 | result=result + "</table>"; |
1232 | |
1233 | div_text.innerHTML=result; |
1234 | } |
1235 | |
1236 | |
1237 | // |
1238 | // Failure in getting route. |
1239 | // |
1240 | |
1241 | function getRouteFailure(response) |
1242 | { |
1243 | var div_text=document.getElementById(routing_type + "_text"); |
1244 | div_text.innerHTML = "Failed to get route information!".bold(); |
1245 | } |