Refactor server-based trace loading. Next: remove all the crud
authorAndy Allan <gravitystorm@gmail.com>
Fri, 15 Oct 2010 17:20:13 +0000 (17:20 +0000)
committerAndy Allan <gravitystorm@gmail.com>
Fri, 15 Oct 2010 17:20:13 +0000 (17:20 +0000)
net/systemeD/halcyon/connection/Connection.as
net/systemeD/halcyon/connection/Trace.as [new file with mode: 0644]
net/systemeD/halcyon/connection/XMLConnection.as
net/systemeD/potlatch2/mygpx/MyGpxDialog.mxml

index 93562e3..f10c340 100755 (executable)
@@ -364,6 +364,7 @@ package net.systemeD.halcyon.connection {
                public function closeChangeset():void {}
            public function uploadChanges():void {}
         public function fetchUserTraces():void {}
+        public function fetchTrace(id:Number, callback:Function):void {}
         public function signOAuthGet(url:String):String {return 'example.com';}
     }
 
diff --git a/net/systemeD/halcyon/connection/Trace.as b/net/systemeD/halcyon/connection/Trace.as
new file mode 100644 (file)
index 0000000..fdd9bdb
--- /dev/null
@@ -0,0 +1,134 @@
+package net.systemeD.halcyon.connection {
+
+    import flash.events.EventDispatcher;
+    import flash.utils.Dictionary;
+    import flash.events.*;
+    import flash.net.URLLoader;
+
+    import net.systemeD.halcyon.connection.*;
+    import net.systemeD.halcyon.Map;
+    import net.systemeD.halcyon.Globals;
+    import net.systemeD.halcyon.VectorLayer;
+
+    public class Trace extends EventDispatcher {
+        private var _id:Number;
+        private var _description:String;
+        private var tags:Array = []; // N.B. trace tags are NOT k/v pairs
+        private var _isLoaded:Boolean;
+        private var _filename:String;
+        private var _url:String; // migrate so that only XML connection knows this
+        private var _traceData:String;
+        private var map:Map;
+        private var _layer:VectorLayer;
+        private var simplify:Boolean = false;
+
+        private static const STYLESHEET:String="gpx.css";
+
+        public function Trace() {
+            map = Globals.vars.root;
+        }
+
+        /* Create a new trace, from the XML description given by the user/traces call */
+        public function fromXML(xml:XML):Trace {
+            _id = Number(xml.@id);
+            _filename = xml.@name;
+            _description = xml.description;
+            for each(var tag:XML in xml.tag) {
+              tags.push(String(tag));
+            }
+            _url = Connection.getConnectionInstance().apiBase+"gpx/"+_id+"/data.xml";
+            return this;
+        }
+
+        public function get id():Number {
+            return _id;
+        }
+
+        public function get description():String {
+            return _description;
+        }
+
+        public function get filename():String {
+            return _filename;
+        }
+
+        public function get tagsText():String {
+            return tags.join(", ");
+        }
+
+        // I'd rather this wasn't public
+        public function get url():String {
+            return _url;
+        }
+
+        public function get traceData():XML {
+            return XML(_traceData);
+        }
+
+        private function fetchFromServer():void {
+            trace("fetchfromServer");
+            Connection.getConnectionInstance().fetchTrace(id, saveTraceData);
+            dispatchEvent(new Event("loading_data"));
+        }
+
+        private function saveTraceData(event:Event):void {
+            trace("saveTraceData");
+            _traceData = String(URLLoader(event.target).data);
+            dispatchEvent(new Event("loaded_data"));
+        }
+
+        private function get layer():VectorLayer {
+            if (!_layer) {
+                _layer=new VectorLayer(filename,map,STYLESHEET);
+                map.addVectorLayer(_layer);
+            }
+            return _layer;
+        }
+
+        public function addToMap():void {
+            if (!_isLoaded) {
+              addEventListener("loaded_data", processEvent);
+              fetchFromServer();
+              return;
+            } else {
+              process();
+            }
+        }
+
+        private function processEvent(e:Event):void {
+            trace("processEvent");
+            process();
+        }
+
+        private function process():void {
+            trace("process");
+            var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
+            var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
+            var raw:String = _traceData.replace(xmlnsPattern, "").replace(xsiPattern, "");
+            var file:XML=new XML(raw);
+
+            for each (var trk:XML in file.child("trk")) {
+                for each (var trkseg:XML in trk.child("trkseg")) {
+                    trace("trkseg");
+                    var way:Way;
+                    var nodestring:Array=[];
+                    for each (var trkpt:XML in trkseg.child("trkpt")) {
+                        nodestring.push(layer.createNode({}, trkpt.@lat, trkpt.@lon));
+                    }
+                    if (nodestring.length>0) {
+                        way=layer.createWay({}, nodestring);
+                        //if (simplify) { Simplify.simplify(way, paint.map, false); }
+                    }
+                }
+            }
+            for each (var wpt:XML in file.child("wpt")) {
+                var tags:Object={};
+                for each (var tag:XML in wpt.children()) {
+                    tags[tag.name()]=tag.toString();
+                }
+                layer.createNode(tags, wpt.@lat, wpt.@lon);
+            }
+            layer.paint.redraw();
+        }
+    }
+}
\ No newline at end of file
index 8d382ca..f952686 100644 (file)
@@ -334,21 +334,17 @@ package net.systemeD.halcyon.connection {
             clearTraces();
             var files:XML = new XML(URLLoader(event.target).data);
             for each(var traceData:XML in files.gpx_file) {
-              var t:Object = {};
-              t.id = Number(traceData.@id);
-              t.name = traceData.@name;
-              t.description = traceData.description;
-              var tags:Array = [];
-              for each(var tag:XML in traceData.tag) {
-                tags.push(String(tag));
-              }
-              t.tags = tags.join(" ");
-              t.url = Connection.apiBaseURL+"gpx/"+t.id+"/data.xml";
+              var t:Trace = new Trace().fromXML(traceData);
               addTrace(t);
             }
             trace("loaded gpx files");
             dispatchEvent(new Event(LOAD_COMPLETED));
             dispatchEvent(new Event(TRACES_LOADED));
         }
+
+        override public function fetchTrace(id:Number, callback:Function):void {
+            sendOAuthGet(Connection.apiBaseURL+"gpx/"+id+"/data.xml", callback, errorOnMapLoad, mapLoadStatus); // needs error handlers
+            dispatchEvent(new Event(LOAD_STARTED)); //specifc to map or reusable?
+        }
        }
 }
index 5bd2189..5778580 100644 (file)
         return conn.getTraces();
     }
 
-    public function loadFile(o:Object):void {
-        var vectorlayer:VectorLayer=new VectorLayer(o.name,map,"gpx.css");
-        vectorlayer.url=o.url;
-        map.addVectorLayer(vectorlayer);
-        var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [o.url], filesLoaded, false);
+    public function loadFile(t:Trace):void {
+        t.addToMap();
+        //var vectorlayer:VectorLayer=new VectorLayer(t.filename,map,"gpx.css");
+        //vectorlayer.url=t.url;
+        //map.addVectorLayer(vectorlayer);
+        //var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [t.url], filesLoaded, false);
     }
 
     public function filesLoaded(success:Boolean,message:String=null):void {
@@ -79,9 +80,9 @@
   <mx:DataGrid dataProvider="{traces}" width="100%" height="100%" id="traceGrid">
     <mx:columns>
             <mx:DataGridColumn editable="false" dataField="id" headerText="id" width="45"/>
-            <mx:DataGridColumn editable="false" dataField="name" headerText="filename"/>
+            <mx:DataGridColumn editable="false" dataField="filename" headerText="filename"/>
             <mx:DataGridColumn editable="false" dataField="description" headerText="description"/>
-            <mx:DataGridColumn editable="false" dataField="tags" headerText="tags"/>
+            <mx:DataGridColumn editable="false" dataField="tagsText" headerText="tags"/>
             <mx:DataGridColumn editable="false">
               <mx:itemRenderer>
                 <mx:Component>