03cb67d0aab829f82072f03d56916339b2e9f84c
[potlatch2.git] / net / systemeD / potlatch2 / utils / TrackLoader.as
1 package net.systemeD.potlatch2.utils {
2
3         import net.systemeD.halcyon.connection.*;
4         import net.systemeD.halcyon.Map;
5         import net.systemeD.halcyon.VectorLayer;
6         import flash.net.*;
7         import flash.events.*;
8
9         /* still to do:
10         - empty layer on reload
11         - cope with tracks with timestamps */
12
13         public class TrackLoader {
14
15                 private var left:Number=0;
16                 private var right:Number=0;
17                 private var top:Number=0;
18                 private var bottom:Number=0;
19                 private var page:uint=0;
20                 private var _layer:VectorLayer;
21
22                 private var map:Map;
23                 private var apiBaseURL:String;
24
25                 private static const STYLESHEET:String="gpx.css";
26                 
27                 public function TrackLoader(map:Map, url:String) {
28                         this.map=map;
29                         apiBaseURL=url;
30                 }
31                 
32                 public function load(keep:Boolean=false):void {
33                         if (map.edge_l==left && map.edge_r==right && map.edge_t==top && map.edge_b==bottom) {
34                                 page++;
35                         } else {
36                                 left  =map.edge_l;
37                                 right =map.edge_r;
38                                 top   =map.edge_t;
39                                 bottom=map.edge_b;
40                                 page=0;
41                                 if (!keep) { } // ** TODO: blank the vector layer
42                         }
43
44                         var loader:URLLoader = new URLLoader();
45                         loader.load(new URLRequest(apiBaseURL+"trackpoints?bbox="+left+","+bottom+","+right+","+top+"&page="+page));
46                         loader.addEventListener(Event.COMPLETE, parseGPX);
47                 }
48
49                 public function parseGPX(event:Event):void {
50             default xml namespace = new Namespace("http://www.topografix.com/GPX/1/0");
51                         var file:XML = new XML(event.target.data);
52
53                         for each (var trkseg:XML in file..trkseg) {
54                                 var nodestring:Array = [];
55                                 var lat:Number = NaN, lastlat:Number = NaN;
56                                 var lon:Number = NaN, lastlon:Number = NaN;
57                 for each (var trkpt:XML in trkseg.trkpt) {
58                                         lat = trkpt.@lat;
59                     lon = trkpt.@lon;
60                     if (lastlat && nodestring.length > 0 && greatCircle(lat, lon, lastlat, lastlon) > 30) {
61                         layer.createWay({}, nodestring);
62                         nodestring = [];
63                     }
64                     nodestring.push(layer.createNode({}, lat, lon));
65                     lastlat = lat; lastlon = lon;
66                                 }
67                 if (nodestring.length > 0) { layer.createWay({}, nodestring); }
68                         }
69             
70                         layer.paint.updateEntityUIs(layer.getObjectsByBbox(left,right,top,bottom), false, false);
71                 }
72
73                 
74                 private function get layer():VectorLayer {
75                         if (!_layer) {
76                                 var n:String='GPS tracks';
77                                 _layer=new VectorLayer(n,map,STYLESHEET);
78                                 map.addVectorLayer(_layer);
79                         }
80                         return _layer;
81                 }
82                 
83                 private function greatCircle(lat1:Number,lon1:Number,lat2:Number,lon2:Number):Number {
84                         var dlat:Number=(lat2-lat1)*Math.PI/180;
85                         var dlon:Number=(lon2-lon1)*Math.PI/180;
86                         var a:Number=Math.pow(Math.sin(dlat / 2),2) + 
87                                      Math.cos(lat1*Math.PI/180) * 
88                                      Math.cos(lat2*Math.PI/180) * 
89                                      Math.pow(Math.sin(dlon / 2),2);
90                         a=Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
91                         return a*3958.75*1609;
92                 }
93                 
94         }
95 }