c408f9a878c513a2df545af201bb6d0141084d11
[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         public function get traceData():XML {
58             return XML(_traceData);
59         }
60
61         private function fetchFromServer():void {
62             Connection.getConnectionInstance().fetchTrace(id, saveTraceData);
63             dispatchEvent(new Event("loading_data"));
64         }
65
66         private function saveTraceData(event:Event):void {
67             _traceData = String(URLLoader(event.target).data);
68             dispatchEvent(new Event("loaded_data"));
69         }
70
71         private function get layer():VectorLayer {
72             if (!_layer) {
73                 _layer=new VectorLayer(filename,map,STYLESHEET);
74                 map.addVectorLayer(_layer);
75             }
76             return _layer;
77         }
78
79         public function addToMap():void {
80             if (!_isLoaded) {
81               addEventListener("loaded_data", processEvent);
82               fetchFromServer();
83               return;
84             } else {
85               process();
86             }
87         }
88
89         private function processEvent(e:Event):void {
90             process();
91         }
92
93         private function process():void {
94             var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
95             var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
96             var raw:String = _traceData.replace(xmlnsPattern, "").replace(xsiPattern, "");
97             var file:XML=new XML(raw);
98
99             for each (var trk:XML in file.child("trk")) {
100                 for each (var trkseg:XML in trk.child("trkseg")) {
101                     trace("trkseg");
102                     var way:Way;
103                     var nodestring:Array=[];
104                     for each (var trkpt:XML in trkseg.child("trkpt")) {
105                         nodestring.push(layer.createNode({}, trkpt.@lat, trkpt.@lon));
106                     }
107                     if (nodestring.length>0) {
108                         way=layer.createWay({}, nodestring);
109                         //if (simplify) { Simplify.simplify(way, paint.map, false); }
110                     }
111                 }
112             }
113             for each (var wpt:XML in file.child("wpt")) {
114                 var tags:Object={};
115                 for each (var tag:XML in wpt.children()) {
116                     tags[tag.name()]=tag.toString();
117                 }
118                 layer.createNode(tags, wpt.@lat, wpt.@lon);
119             }
120             layer.paint.redraw();
121         }
122     }
123 }