2c0c1a5fb618cf8b0e6c47ed666f7188a023aa89
[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     /**
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;
20         private var _description:String;
21         private var tags:Array = []; // N.B. trace tags are NOT k/v pairs
22         private var _isLoaded:Boolean;
23         private var _filename:String;
24         private var _traceData:String;
25         private var map:Map;
26         private var _connection:Connection;
27         private var _layer:VectorLayer;
28         private var simplify:Boolean = false;
29
30         private static const STYLESHEET:String="stylesheets/gpx.css";
31
32         public function Trace(connection:Connection) {
33                         _connection = connection;
34             map = Globals.vars.root;
35         }
36
37         /* Create a new trace, from the XML description given by the user/traces call */
38         public function fromXML(xml:XML):Trace {
39             _id = Number(xml.@id);
40             _filename = xml.@name;
41             _description = xml.description;
42             for each(var tag:XML in xml.tag) {
43               tags.push(String(tag));
44             }
45             return this;
46         }
47
48         public function get id():Number {
49             return _id;
50         }
51
52         public function get description():String {
53             return _description;
54         }
55
56         public function get filename():String {
57             return _filename;
58         }
59
60         public function get tagsText():String {
61             return tags.join(", ");
62         }
63
64         private function fetchFromServer():void {
65             // todo - needs proper error handling
66             _connection.fetchTrace(id, saveTraceData);
67             dispatchEvent(new Event("loading_data"));
68         }
69
70         private function saveTraceData(event:Event):void {
71             _traceData = String(URLLoader(event.target).data);
72             dispatchEvent(new Event("loaded_data"));
73         }
74
75         private function get layer():VectorLayer {
76             if (!_layer) {
77                                 // >>>> REFACTOR: VectorLayer commented out
78                 // _layer=new VectorLayer(filename,map,STYLESHEET);
79                 // map.addVectorLayer(_layer);
80             }
81             return _layer;
82         }
83
84         public function addToMap():void {
85             // this allows adding and removing traces from the map, without re-downloading
86             // the data from the server repeatedly.
87             if (!_isLoaded) {
88               addEventListener("loaded_data", processEvent);
89               fetchFromServer();
90               return;
91             } else {
92               process();
93             }
94         }
95
96         public function removeFromMap():void {
97             //todo
98         }
99
100         private function processEvent(e:Event):void {
101             removeEventListener("loaded_data", processEvent);
102             process();
103         }
104
105         private function process():void {
106             var file:XML = new XML(_traceData);
107                         for each (var ns:Namespace in file.namespaceDeclarations()) {
108                                 if (ns.uri.match(/^http:\/\/www\.topografix\.com\/GPX\/1\/[01]$/)) {
109                                         default xml namespace = ns;
110                                 }
111                         }
112
113             for each (var trkseg:XML in file..trkseg) {
114                 var way:Way;
115                 var nodestring:Array = [];
116                 for each (var trkpt:XML in trkseg.trkpt) {
117                     nodestring.push(layer.createNode({}, trkpt.@lat, trkpt.@lon));
118                 }
119                 if (nodestring.length > 0) {
120                     way = layer.createWay({}, nodestring);
121                     //if (simplify) { Simplify.simplify(way, paint.map, false); }
122                 }
123             }
124
125             for each (var wpt:XML in file.wpt) {
126                 var tags:Object = {};
127                 for each (var tag:XML in wpt.children()) {
128                     tags[tag.name().localName]=tag.toString();
129                 }
130                 var node:Node = layer.createNode(tags, wpt.@lat, wpt.@lon);
131                                 layer.registerPOI(node);
132             }
133
134                         default xml namespace = new Namespace("");
135             layer.paint.updateEntityUIs(true, false);
136         }
137     }
138 }