Refactor server-based trace loading. Next: remove all the crud
[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.VectorLayer;
12
13     public class Trace extends EventDispatcher {
14         private var _id:Number;
15         private var _description:String;
16         private var tags:Array = []; // N.B. trace tags are NOT k/v pairs
17         private var _isLoaded:Boolean;
18         private var _filename:String;
19         private var _url:String; // migrate so that only XML connection knows this
20         private var _traceData:String;
21         private var map:Map;
22         private var _layer:VectorLayer;
23         private var simplify:Boolean = false;
24
25         private static const STYLESHEET:String="gpx.css";
26
27         public function Trace() {
28             map = Globals.vars.root;
29         }
30
31         /* Create a new trace, from the XML description given by the user/traces call */
32         public function fromXML(xml:XML):Trace {
33             _id = Number(xml.@id);
34             _filename = xml.@name;
35             _description = xml.description;
36             for each(var tag:XML in xml.tag) {
37               tags.push(String(tag));
38             }
39             _url = Connection.getConnectionInstance().apiBase+"gpx/"+_id+"/data.xml";
40             return this;
41         }
42
43         public function get id():Number {
44             return _id;
45         }
46
47         public function get description():String {
48             return _description;
49         }
50
51         public function get filename():String {
52             return _filename;
53         }
54
55         public function get tagsText():String {
56             return tags.join(", ");
57         }
58
59         // I'd rather this wasn't public
60         public function get url():String {
61             return _url;
62         }
63
64         public function get traceData():XML {
65             return XML(_traceData);
66         }
67
68         private function fetchFromServer():void {
69             trace("fetchfromServer");
70             Connection.getConnectionInstance().fetchTrace(id, saveTraceData);
71             dispatchEvent(new Event("loading_data"));
72         }
73
74         private function saveTraceData(event:Event):void {
75             trace("saveTraceData");
76             _traceData = String(URLLoader(event.target).data);
77             dispatchEvent(new Event("loaded_data"));
78         }
79
80         private function get layer():VectorLayer {
81             if (!_layer) {
82                 _layer=new VectorLayer(filename,map,STYLESHEET);
83                 map.addVectorLayer(_layer);
84             }
85             return _layer;
86         }
87
88         public function addToMap():void {
89             if (!_isLoaded) {
90               addEventListener("loaded_data", processEvent);
91               fetchFromServer();
92               return;
93             } else {
94               process();
95             }
96         }
97
98         private function processEvent(e:Event):void {
99             trace("processEvent");
100             process();
101         }
102
103         private function process():void {
104             trace("process");
105             var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
106             var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
107             var raw:String = _traceData.replace(xmlnsPattern, "").replace(xsiPattern, "");
108             var file:XML=new XML(raw);
109
110             for each (var trk:XML in file.child("trk")) {
111                 for each (var trkseg:XML in trk.child("trkseg")) {
112                     trace("trkseg");
113                     var way:Way;
114                     var nodestring:Array=[];
115                     for each (var trkpt:XML in trkseg.child("trkpt")) {
116                         nodestring.push(layer.createNode({}, trkpt.@lat, trkpt.@lon));
117                     }
118                     if (nodestring.length>0) {
119                         way=layer.createWay({}, nodestring);
120                         //if (simplify) { Simplify.simplify(way, paint.map, false); }
121                     }
122                 }
123             }
124             for each (var wpt:XML in file.child("wpt")) {
125                 var tags:Object={};
126                 for each (var tag:XML in wpt.children()) {
127                     tags[tag.name()]=tag.toString();
128                 }
129                 layer.createNode(tags, wpt.@lat, wpt.@lon);
130             }
131             layer.paint.redraw();
132         }
133     }
134 }