Remove unnecessary public call, and add notes about future plans
[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 _traceData:String;
20         private var map:Map;
21         private var _layer:VectorLayer;
22         private var simplify:Boolean = false;
23
24         private static const STYLESHEET:String="gpx.css";
25
26         public function Trace() {
27             map = Globals.vars.root;
28         }
29
30         /* Create a new trace, from the XML description given by the user/traces call */
31         public function fromXML(xml:XML):Trace {
32             _id = Number(xml.@id);
33             _filename = xml.@name;
34             _description = xml.description;
35             for each(var tag:XML in xml.tag) {
36               tags.push(String(tag));
37             }
38             return this;
39         }
40
41         public function get id():Number {
42             return _id;
43         }
44
45         public function get description():String {
46             return _description;
47         }
48
49         public function get filename():String {
50             return _filename;
51         }
52
53         public function get tagsText():String {
54             return tags.join(", ");
55         }
56
57         private function fetchFromServer():void {
58             // todo - needs proper error handling
59             Connection.getConnectionInstance().fetchTrace(id, saveTraceData);
60             dispatchEvent(new Event("loading_data"));
61         }
62
63         private function saveTraceData(event:Event):void {
64             _traceData = String(URLLoader(event.target).data);
65             dispatchEvent(new Event("loaded_data"));
66         }
67
68         private function get layer():VectorLayer {
69             if (!_layer) {
70                 _layer=new VectorLayer(filename,map,STYLESHEET);
71                 map.addVectorLayer(_layer);
72             }
73             return _layer;
74         }
75
76         public function addToMap():void {
77             // this allows adding and removing traces from the map, without re-downloading
78             // the data from the server repeatedly.
79             if (!_isLoaded) {
80               addEventListener("loaded_data", processEvent);
81               fetchFromServer();
82               return;
83             } else {
84               process();
85             }
86         }
87
88         public function removeFromMap():void {
89             //todo
90         }
91
92         private function processEvent(e:Event):void {
93             process();
94         }
95
96         private function process():void {
97             var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
98             var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
99             var raw:String = _traceData.replace(xmlnsPattern, "").replace(xsiPattern, "");
100             var file:XML=new XML(raw);
101
102             for each (var trk:XML in file.child("trk")) {
103                 for each (var trkseg:XML in trk.child("trkseg")) {
104                     trace("trkseg");
105                     var way:Way;
106                     var nodestring:Array=[];
107                     for each (var trkpt:XML in trkseg.child("trkpt")) {
108                         nodestring.push(layer.createNode({}, trkpt.@lat, trkpt.@lon));
109                     }
110                     if (nodestring.length>0) {
111                         way=layer.createWay({}, nodestring);
112                         //if (simplify) { Simplify.simplify(way, paint.map, false); }
113                     }
114                 }
115             }
116             for each (var wpt:XML in file.child("wpt")) {
117                 var tags:Object={};
118                 for each (var tag:XML in wpt.children()) {
119                     tags[tag.name()]=tag.toString();
120                 }
121                 layer.createNode(tags, wpt.@lat, wpt.@lon);
122             }
123             layer.paint.redraw();
124         }
125     }
126 }