Bugfix - clear out tags before appending, to handle refreshing the trace data from...
[potlatch2.git] / net / systemeD / halcyon / connection / Trace.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.EventDispatcher;
4     import flash.utils.Dictionary;
5     import flash.events.*;
6     import flash.net.URLLoader;
7
8     import net.systemeD.halcyon.connection.*;
9     import net.systemeD.halcyon.Map;
10     import net.systemeD.halcyon.Globals;
11     import net.systemeD.halcyon.MapPaint;
12
13     /**
14     * Implements trace objects loaded from the OSM API.
15     * See also potlatch2's utils GpxImporter.as and Importer.as classes, which can handle
16     * loading GPX files (and other formats) from arbitrary urls.
17     */
18     public class Trace extends EventDispatcher {
19         private var _id:Number; // id of the trace, as reported by the server
20         private var _description:String; // description, as reported by the server
21         private var tags:Array = []; // N.B. trace tags are NOT k/v pairs
22         private var _isLoaded:Boolean; // Flag for when the data has been downloaded and parsed
23         private var _filename:String; // The original name of the file, as reported by the server
24         private var _traceData:String; // the trace data, saved as a string
25         private var map:Map;
26         private var _layer:MapPaint;
27         private var masterConnection:Connection; // The authenticated connection
28         private var _connection:Connection; // The one we store our fake nodes/ways in.
29         private var simplify:Boolean = false;
30
31         private static const STYLESHEET:String="stylesheets/gpx.css";
32
33         /** Create a new trace.
34         * @param masterConnection The authenticated connection to communicate with the server
35         */
36         public function Trace(masterConnection:Connection, id:int=0) {
37             this.masterConnection = masterConnection;
38             map = Globals.vars.root; // REFACTOR this prevents traces being added to arbitrary maps
39                         if (id!=0) _id=id;
40         }
41
42         /** Create a new trace, from the XML description given by the user/traces call.
43         * This only creates the object itself, the actual trace contents (trkseg etc) are
44         * lazily downloaded later. */
45         public function fromXML(xml:XML):Trace {
46             _id = Number(xml.@id);
47             _filename = xml.@name;
48             _description = xml.description;
49             tags = [];
50             for each(var tag:XML in xml.tag) {
51               tags.push(String(tag));
52             }
53             return this;
54         }
55
56         public function get id():Number {
57             return _id;
58         }
59
60         public function get description():String {
61             return _description;
62         }
63
64         public function get filename():String {
65             return _filename;
66         }
67
68         public function get tagsText():String {
69             return tags.join(", ");
70         }
71
72         private function fetchFromServer():void {
73             // todo - needs proper error handling
74             masterConnection.fetchTrace(id, saveTraceData);
75             dispatchEvent(new Event("loading_data"));
76         }
77
78         private function saveTraceData(event:Event):void {
79             _traceData = String(URLLoader(event.target).data);
80             dispatchEvent(new Event("loaded_data"));
81         }
82
83         private function get connection():Connection {
84             if (!_connection) {
85                 // create a new connection so that the ids don't impact the main layer.
86                 _connection = new Connection(filename, null, null, null);
87             }
88             return _connection
89         }
90
91         private function get layer():MapPaint {
92             if (!_layer) {
93                 // create a new layer for every trace, so they can be turned on/off individually
94                 _layer = map.addLayer(connection, STYLESHEET);
95             }
96             return _layer;
97         }
98
99         public function addToMap():void {
100             // this allows adding and removing traces from the map, without re-downloading
101             // the data from the server repeatedly.
102             if (!_isLoaded) {
103               addEventListener("loaded_data", processEvent);
104               fetchFromServer();
105               return;
106             } else {
107               process();
108             }
109         }
110
111         public function removeFromMap():void {
112             //todo
113         }
114
115         private function processEvent(e:Event):void {
116             removeEventListener("loaded_data", processEvent);
117             _isLoaded=true;
118             process();
119         }
120
121         private function process():void {
122             var file:XML = new XML(_traceData);
123             var action:CompositeUndoableAction = new CompositeUndoableAction("add trace objects");
124                         for each (var ns:Namespace in file.namespaceDeclarations()) {
125                                 if (ns.uri.match(/^http:\/\/www\.topografix\.com\/GPX\/1\/[01]$/)) {
126                                         default xml namespace = ns;
127                                 }
128                         }
129
130                         Trace.parseTrkSegs(file,connection,action,false);
131                         
132             for each (var wpt:XML in file.wpt) {
133                 var tags:Object = {};
134                 for each (var tag:XML in wpt.children()) {
135                     tags[tag.name().localName]=tag.toString().substr(0,255);
136                 }
137                 var node:Node = connection.createNode(tags, wpt.@lat, wpt.@lon, action.push);
138                                 connection.registerPOI(node);
139             }
140
141             action.doAction(); /* just do it, don't add to undo stack */
142                         default xml namespace = new Namespace("");
143             layer.updateEntityUIs(true, false);
144         }
145
146                 /* Draw ways from <trkseg>s, with elementary filter to remove points within 3 metres of each other. 
147                    Optionally split way if more than 50m from previous point.
148                    FIXME: do auto-joining of dupes as per Importer. */
149
150                 public static function parseTrkSegs(file:XML, connection:Connection, action:CompositeUndoableAction, smartSplitting:Boolean=false):void {
151                         for each (var ns:Namespace in file.namespaceDeclarations()) {
152                                 if (ns.uri.match(/^http:\/\/www\.topografix\.com\/GPX\/1\/[01]$/)) { default xml namespace = ns; }
153                         }
154                         for each (var trkseg:XML in file..trkseg) {
155                                 var nodestring:Array = [];
156                                 var lat:Number = NaN, lastlat:Number = NaN;
157                                 var lon:Number = NaN, lastlon:Number = NaN;
158                                 var dist:Number=0;
159                                 for each (var trkpt:XML in trkseg.trkpt) {
160                                         lat = trkpt.@lat;
161                                         lon = trkpt.@lon;
162                                         if (isNaN(lastlat)) { lastlat = lat; lastlon = lon; }
163                                         dist=Trace.greatCircle(lat, lon, lastlat, lastlon);
164                                         if (dist>3) {
165                                                 if ((dist>50 && smartSplitting) || nodestring.length>500) {
166                                                         if (dist<=50 || !smartSplitting) nodestring.push(connection.createNode({}, lat, lon, action.push));
167                                                         if (nodestring.length>1) connection.createWay({}, nodestring, action.push);
168                                                         nodestring=[];
169                                                 }
170                                                 nodestring.push(connection.createNode({}, lat, lon, action.push));
171                                                 lastlat=lat; lastlon=lon;
172                                         }
173                                 }
174                                 if (nodestring.length > 1) { connection.createWay({}, nodestring, action.push); }
175                         }
176                 }
177                 
178                 public static function greatCircle(lat1:Number,lon1:Number,lat2:Number,lon2:Number):Number {
179                         var dlat:Number=(lat2-lat1)*Math.PI/180;
180                         var dlon:Number=(lon2-lon1)*Math.PI/180;
181                         var a:Number=Math.pow(Math.sin(dlat / 2),2) + 
182                                      Math.cos(lat1*Math.PI/180) * 
183                                      Math.cos(lat2*Math.PI/180) * 
184                                      Math.pow(Math.sin(dlon / 2),2);
185                         a=Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
186                         return a*3958.75*1609;
187                 }
188                 
189     }
190 }