disable caching when using Flash Debug Player
[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                         var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
51                         var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
52                         var raw:String = String(event.target.data).replace(xmlnsPattern, "").replace(xsiPattern, "");
53                         var file:XML=new XML(raw);
54
55                         for each (var trk:XML in file.child("trk")) {
56                                 for each (var trkseg:XML in trk.child("trkseg")) {
57                                         var nodestring:Array=[];
58                                         var lat:Number=NaN, lastlat:Number=NaN;
59                                         var lon:Number=NaN, lastlon:Number=NaN;
60                                         for each (var trkpt:XML in trkseg.child("trkpt")) {
61                                                 lat=trkpt.@lat;
62                                                 lon=trkpt.@lon;
63                                                 if (lastlat && nodestring.length>0 && greatCircle(lat,lon,lastlat,lastlon)>30) {
64                                                         layer.createWay({}, nodestring);
65                                                         nodestring=[];
66                                                 }
67                                                 nodestring.push(layer.createNode({}, lat, lon));
68                                                 lastlat=lat; lastlon=lon;
69                                         }
70                                         if (nodestring.length>0) { layer.createWay({}, nodestring); }
71                                 }
72                         }
73                         layer.paint.updateEntityUIs(layer.getObjectsByBbox(left,right,top,bottom), false, false);
74                 }
75
76                 
77                 private function get layer():VectorLayer {
78                         if (!_layer) {
79                                 var n:String='GPS tracks';
80                                 _layer=new VectorLayer(n,map,STYLESHEET);
81                                 map.addVectorLayer(_layer);
82                         }
83                         return _layer;
84                 }
85                 
86                 private function greatCircle(lat1:Number,lon1:Number,lat2:Number,lon2:Number):Number {
87                         var dlat:Number=(lat2-lat1)*Math.PI/180;
88                         var dlon:Number=(lon2-lon1)*Math.PI/180;
89                         var a:Number=Math.pow(Math.sin(dlat / 2),2) + 
90                                      Math.cos(lat1*Math.PI/180) * 
91                                      Math.cos(lat2*Math.PI/180) * 
92                                      Math.pow(Math.sin(dlon / 2),2);
93                         a=Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
94                         return a*3958.75*1609;
95                 }
96                 
97         }
98 }