load GPS tracks from server, needs a bit of refinement still but the basics are there
[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         public class TrackLoader {
10
11                 private var left:Number=0;
12                 private var right:Number=0;
13                 private var top:Number=0;
14                 private var bottom:Number=0;
15                 private var page:uint=0;
16                 private var _layer:VectorLayer;
17
18                 private var map:Map;
19                 private var apiBaseURL:String;
20
21                 private static const STYLESHEET:String="wireframe.css";
22                 
23                 public function TrackLoader(map:Map, url:String) {
24                         this.map=map;
25                         apiBaseURL=url;
26                 }
27                 
28                 public function load(keep:Boolean=false):void {
29                         if (map.edge_l==left && map.edge_r==right && map.edge_t==top && map.edge_b==bottom) {
30                                 page++;
31                         } else {
32                                 left  =map.edge_l;
33                                 right =map.edge_r;
34                                 top   =map.edge_t;
35                                 bottom=map.edge_b;
36                                 page=0;
37                                 if (!keep) { } // ** TODO: blank the vector layer
38                         }
39
40                         var loader:URLLoader = new URLLoader();
41                         loader.load(new URLRequest(apiBaseURL+"trackpoints?bbox="+left+","+bottom+","+right+","+top+"&page="+page));
42                         loader.addEventListener(Event.COMPLETE, parseGPX);
43                 }
44
45                 public function parseGPX(event:Event):void {
46                         var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
47                         var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
48                         var raw:String = String(event.target.data).replace(xmlnsPattern, "").replace(xsiPattern, "");
49                         var file:XML=new XML(raw);
50
51                         for each (var trk:XML in file.child("trk")) {
52                                 for each (var trkseg:XML in trk.child("trkseg")) {
53                                         var nodestring:Array=[];
54                                         var lat:Number=NaN, lastlat:Number=NaN;
55                                         var lon:Number=NaN, lastlon:Number=NaN;
56                                         for each (var trkpt:XML in trkseg.child("trkpt")) {
57                                                 lat=trkpt.@lat;
58                                                 lon=trkpt.@lon;
59                                                 if (lastlat && nodestring.length>0 && greatCircle(lat,lon,lastlat,lastlon)>30) {
60                                                         layer.createWay({}, nodestring);
61                                                         nodestring=[];
62                                                 }
63                                                 nodestring.push(layer.createNode({}, lat, lon));
64                                                 lastlat=lat; lastlon=lon;
65                                         }
66                                         if (nodestring.length>0) { layer.createWay({}, nodestring); }
67                                 }
68                         }
69                         layer.paint.updateEntityUIs(layer.getObjectsByBbox(left,right,top,bottom), false, false);
70                 }
71
72                 
73                 private function get layer():VectorLayer {
74                         if (!_layer) {
75                                 var n:String='GPS tracks';
76                                 _layer=new VectorLayer(n,map,STYLESHEET);
77                                 map.addVectorLayer(_layer);
78                         }
79                         return _layer;
80                 }
81                 
82                 private function greatCircle(lat1:Number,lon1:Number,lat2:Number,lon2:Number):Number {
83                         var dlat:Number=(lat2-lat1)*Math.PI/180;
84                         var dlon:Number=(lon2-lon1)*Math.PI/180;
85                         var a:Number=Math.pow(Math.sin(dlat / 2),2) + 
86                                      Math.cos(lat1*Math.PI/180) * 
87                                      Math.cos(lat2*Math.PI/180) * 
88                                      Math.pow(Math.sin(dlon / 2),2);
89                         a=Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
90                         return a*3958.75*1609;
91                 }
92                 
93         }
94 }