Refactor all the Importer classes to use connections instead of vectorlayers
authorAndy Allan <andy@gravitystorm.co.uk>
Thu, 26 May 2011 15:03:39 +0000 (16:03 +0100)
committerAndy Allan <andy@gravitystorm.co.uk>
Thu, 26 May 2011 15:03:39 +0000 (16:03 +0100)
net/systemeD/potlatch2/VectorSourceDialog.mxml
net/systemeD/potlatch2/utils/GpxImporter.as
net/systemeD/potlatch2/utils/Importer.as
net/systemeD/potlatch2/utils/KmlImporter.as
net/systemeD/potlatch2/utils/OsmImporter.as
net/systemeD/potlatch2/utils/ShpImporter.as
potlatch2.mxml

index 5dbbe72..754d19c 100644 (file)
@@ -33,7 +33,7 @@
            </mx:columns>
        </mx:DataGrid>
        <mx:HBox horizontalAlign="right" width="100%">
-               <mx:LinkButton label="Delete" click="removeVectorLayer()" enabled="{dataGrid.selectedItem != null? true : false}"/>
+               <mx:LinkButton label="Delete" click="removeLayer()" enabled="{dataGrid.selectedItem != null? true : false}"/>
        </mx:HBox>
 
        <mx:VBox width="100%" paddingTop="10">
                }
        }
 
-       private function removeVectorLayer():void {
+       private function removeLayer():void {
                map.removeLayerByName(dataGrid.selectedItem.name);
                dispatchEvent(new Event("layers_changed"));
        }
                if (type=='gpx') { stylesheet="stylesheets/gpx.css"; }
 
                var connection:Connection = new Connection(name, url, null, null);
-               var mp:MapPaint = map.addLayer(connection, stylesheet);
-
-// REFACTOR this. commented out to let docs compile.
-        trace("loadFiles not implemented");
-//                var filesLoaded:Function = function(success:Boolean,message:String=null):void {
-//                        if (success) {
-//                                map.addVectorLayer(vectorlayer);
-//                                dispatchEvent(new Event("layers_changed"));
-//                        } else {
-//                                Alert.show(message, 'Error', mx.controls.Alert.OK);
-//                        }
-//                }
-
-//             if (type=='gpx') {
-//                     var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
-//             } else if (type=='kml') {
-//                     var kml:KmlImporter=new KmlImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
-//             } else if (type=='osm') {
-//                     var osm:OsmImporter=new OsmImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
-//             } else {
-//                     var re:RegExp=/.shp$/i; url=url.replace(re,'');
-//                     var shp:ShpImporter=new ShpImporter(vectorlayer,
-//                                                         vectorlayer.paint,
-//                                                         [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify);
-//             }
+
+                var filesLoaded:Function = function(success:Boolean,message:String=null):void {
+                        if (success) {
+                            var paint:MapPaint = map.addLayer(connection, stylesheet);
+                            paint.updateEntityUIs(false, false);
+                            dispatchEvent(new Event("layers_changed"));
+                        } else {
+                            Alert.show(message, 'Error', mx.controls.Alert.OK);
+                        }
+                }
+
+               if (type=='gpx') {
+                       var gpx:GpxImporter=new GpxImporter(connection, map, [url], filesLoaded, simplify);
+               } else if (type=='kml') {
+                       var kml:KmlImporter=new KmlImporter(connection, map, [url], filesLoaded, simplify);
+               } else if (type=='osm') {
+                       var osm:OsmImporter=new OsmImporter(connection, map, [url], filesLoaded, simplify);
+               } else {
+                       var re:RegExp=/.shp$/i; url=url.replace(re,'');
+                       var shp:ShpImporter=new ShpImporter(connection,
+                                                           map,
+                                                           [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify);
+               }
        }
 
               ]]>
index 6c6eac4..c3d6419 100644 (file)
@@ -1,6 +1,7 @@
 package net.systemeD.potlatch2.utils {
 
-       import net.systemeD.halcyon.MapPaint;
+       import net.systemeD.halcyon.Map;
+       import net.systemeD.halcyon.connection.Connection;
        import net.systemeD.halcyon.connection.Node;
        import net.systemeD.halcyon.connection.Way;
        import net.systemeD.potlatch2.tools.Simplify;
@@ -11,11 +12,11 @@ package net.systemeD.potlatch2.utils {
     */
        public class GpxImporter extends Importer {
 
-               public function GpxImporter(container:*, paint:MapPaint, filenames:Array, callback:Function=null, simplify:Boolean=false) {
-                       super(container,paint,filenames,callback,simplify);
+               public function GpxImporter(connection:Connection, map:Map, filenames:Array, callback:Function=null, simplify:Boolean=false) {
+                       super(connection,map,filenames,callback,simplify);
                }
 
-               override protected function doImport(): void {
+               override protected function doImport(push:Function): void {
                        var file:XML = new XML(files[0]);
                        for each (var ns:Namespace in file.namespaceDeclarations()) {
                                if (ns.uri.match(/^http:\/\/www\.topografix\.com\/GPX\/1\/[01]$/)) {
@@ -27,11 +28,11 @@ package net.systemeD.potlatch2.utils {
                                var way:Way;
                 var nodestring:Array = [];
                 for each (var trkpt:XML in trkseg.trkpt) {
-                                       nodestring.push(container.createNode({}, trkpt.@lat, trkpt.@lon));
+                                       nodestring.push(connection.createNode({}, trkpt.@lat, trkpt.@lon, push));
                                }
                 if (nodestring.length > 0) {
-                                       way = container.createWay({}, nodestring);
-                                       if (simplify) { Simplify.simplify(way, paint.map, false); }
+                                       way = connection.createWay({}, nodestring, push);
+                                       if (simplify) { Simplify.simplify(way, map, false); }
                                }
                        }
 
@@ -40,8 +41,8 @@ package net.systemeD.potlatch2.utils {
                                for each (var tag:XML in wpt.children()) {
                                        tags[tag.name().localName]=tag.toString();
                                }
-                               var node:Node = container.createNode(tags, wpt.@lat, wpt.@lon);
-                               container.registerPOI(node);
+                               var node:Node = connection.createNode(tags, wpt.@lat, wpt.@lon, push);
+                               connection.registerPOI(node);
                        }
 
                        default xml namespace = new Namespace("");
index 7e22842..c664530 100644 (file)
@@ -1,6 +1,6 @@
 package net.systemeD.potlatch2.utils {
 
-       import net.systemeD.halcyon.MapPaint;
+       import net.systemeD.halcyon.Map;
        import net.systemeD.halcyon.ExtendedURLLoader;
        import net.systemeD.halcyon.DebugURLRequest;
     import net.systemeD.halcyon.connection.*;
@@ -11,8 +11,8 @@ package net.systemeD.potlatch2.utils {
 
        public class Importer {
 
-               protected var container:Object;                         // destination object for way/node/relations data
-               protected var paint:MapPaint;                           // destination sprite for WayUIs/NodeUIs
+        protected var connection:Connection;    // destination connection for way/node/relations data
+        protected var map:Map;                  // map being used - used only in Simplify calls
 
                public var files:Array=[];
                protected var filenames:Array;
@@ -20,9 +20,9 @@ package net.systemeD.potlatch2.utils {
                protected var callback:Function;
                protected var simplify:Boolean;
 
-               public function Importer(container:*, paint:MapPaint, filenames:Array, callback:Function, simplify:Boolean) {
-                       this.container = container;
-                       this.paint = paint;
+               public function Importer(connection:Connection, map:Map, filenames:Array, callback:Function, simplify:Boolean) {
+                       this.connection = connection;
+                       this.map = map;
                        this.filenames=filenames;
                        this.callback=callback;
                        this.simplify=simplify;
@@ -48,14 +48,15 @@ package net.systemeD.potlatch2.utils {
                        trace("loaded file "+filenum); 
                        files[filenum]=e.target.data;
                        filesloaded++;
-                       if (filesloaded==filenames.length) { 
-                               doImport();
-                               paint.updateEntityUIs(false, false);
+                       if (filesloaded==filenames.length) {
+                var action:CompositeUndoableAction = new CompositeUndoableAction("Import layer "+connection.name);
+                               doImport(action.push);
+                               action.doAction(); // just do it, don't add to undo stack
                                if (callback!=null) { callback(true); }
                        }
                }
                
-               protected function doImport():void {
+               protected function doImport(push:Function):void {
                }
 
                protected function securityErrorHandler( event:SecurityErrorEvent ):void { callback(false,"You don't have permission to open that file."); }
index 947240a..cfed833 100644 (file)
@@ -1,6 +1,7 @@
 package net.systemeD.potlatch2.utils {
 
-    import net.systemeD.halcyon.MapPaint;
+    import net.systemeD.halcyon.Map;
+    import net.systemeD.halcyon.connection.Connection;
     import net.systemeD.halcyon.connection.Node;
     import net.systemeD.halcyon.connection.Way;
     import net.systemeD.halcyon.connection.Relation;
@@ -12,11 +13,11 @@ package net.systemeD.potlatch2.utils {
      */
     public class KmlImporter extends Importer {
 
-        public function KmlImporter(container:*, paint:MapPaint, filenames:Array, callback:Function=null, simplify:Boolean=false) {
-            super(container, paint, filenames, callback, simplify);
+        public function KmlImporter(connection:Connection, map:Map, filenames:Array, callback:Function=null, simplify:Boolean=false) {
+            super(connection, map, filenames, callback, simplify);
         }
 
-        override protected function doImport(): void {
+        override protected function doImport(push:Function): void {
             var kml:XML = new XML(files[0]);
 
             for each (var ns:Namespace in kml.namespaceDeclarations()) {
@@ -39,15 +40,15 @@ package net.systemeD.potlatch2.utils {
                 }
 
                 for each (var point:XML in placemark.Point) {
-                    importNode(point.coordinates, tags);
+                    importNode(point.coordinates, tags, push);
                 }
 
                 for each (var linestring:XML in placemark.LineString) {
-                    importWay(linestring.coordinates, tags, false);
+                    importWay(linestring.coordinates, tags, false, push);
                 }
 
                 for each (var linearring:XML in placemark.LinearRing) {
-                    importWay(linearring.coordinates, tags, true);
+                    importWay(linearring.coordinates, tags, true, push);
                 }
 
                 for each (var polygon:XML in placemark.Polygon) {
@@ -55,39 +56,39 @@ package net.systemeD.potlatch2.utils {
                         var members:Array = [];
                         var way:Way;
 
-                        way = importWay(polygon.outerBoundaryIs.LinearRing.coordinates, {}, true);
+                        way = importWay(polygon.outerBoundaryIs.LinearRing.coordinates, {}, true, push);
                         members.push(new RelationMember(way, "outer"));
 
                         for each (var inner:XML in polygon.innerBoundaryIs) {
-                            way = importWay(inner.LinearRing.coordinates, {}, true);
+                            way = importWay(inner.LinearRing.coordinates, {}, true, push);
                             members.push(new RelationMember(way, "inner"));
                         }
 
                         tags["type"] = "multipolygon";
 
-                        container.createRelation(tags, members);
+                        connection.createRelation(tags, members, push);
                     } else {
-                        importWay(polygon.outerBoundaryIs.LinearRing.coordinates, tags, true);
+                        importWay(polygon.outerBoundaryIs.LinearRing.coordinates, tags, true, push);
                     }
                 }
             }
                        default xml namespace = new Namespace("");
         }
 
-        private function importNode(coordinates:String, tags:Object): Node {
+        private function importNode(coordinates:String, tags:Object, push:Function): Node {
             var coords:Array = coordinates.split(",");
             var lon:Number = coords[0];
             var lat:Number = coords[1];
             //var ele:Number = coords[2];
 
-            var node:Node = container.createNode(tags, lat, lon);
+            var node:Node = connection.createNode(tags, lat, lon, push);
 
-            container.registerPOI(node);
+            connection.registerPOI(node);
 
             return node;
         }
 
-        private function importWay(coordinates:String, tags:Object, polygon:Boolean): Way {
+        private function importWay(coordinates:String, tags:Object, polygon:Boolean, push:Function): Way {
             var way:Way;
             var nodestring:Array = [];
 
@@ -101,7 +102,7 @@ package net.systemeD.potlatch2.utils {
                 var lat:Number = coords[1];
                 //var ele:Number = coords[2];
 
-                nodestring.push(container.createNode({}, lat, lon));
+                nodestring.push(connection.createNode({}, lat, lon, push));
             }
 
             if (polygon) {
@@ -109,8 +110,8 @@ package net.systemeD.potlatch2.utils {
             }
 
             if (nodestring.length > 0) {
-                way = container.createWay(tags, nodestring);
-                if (simplify) { Simplify.simplify(way, paint.map, false); }
+                way = connection.createWay(tags, nodestring, push);
+                if (simplify) { Simplify.simplify(way, map, false); }
             }
 
             return way;
index f456899..c3d84f2 100644 (file)
@@ -1,16 +1,16 @@
 package net.systemeD.potlatch2.utils {
 
-       import net.systemeD.halcyon.MapPaint;
+       import net.systemeD.halcyon.Map;
        import net.systemeD.halcyon.connection.*;
        import net.systemeD.potlatch2.tools.Simplify;
 
        public class OsmImporter extends Importer {
 
-               public function OsmImporter(container:*, paint:MapPaint, filenames:Array, callback:Function=null, simplify:Boolean=false) {
-                       super(container,paint,filenames,callback,simplify);
+               public function OsmImporter(connection:Connection, map:Map, filenames:Array, callback:Function=null, simplify:Boolean=false) {
+                       super(connection,map,filenames,callback,simplify);
                }
 
-               override protected function doImport():void {
+               override protected function doImport(push:Function):void {
                        var map:XML = new XML(files[0]);
                        var data:XML;
                        
@@ -23,14 +23,14 @@ package net.systemeD.potlatch2.utils {
 
             for each(data in map.node) {
                 oldid = Number(data.@id);
-                               nodemap[oldid] = container.createNode(parseTags(data.tag), Number(data.@lat), Number(data.@lon));
+                               nodemap[oldid] = connection.createNode(parseTags(data.tag), Number(data.@lat), Number(data.@lon), push);
             }
 
             for each(data in map.way) {
                 oldid = Number(data.@id);
                 var nodes:Array = [];
                 for each(var nd:XML in data.nd) { nodes.push(nodemap[Number(nd.@ref)]); }
-                               waymap[oldid] = container.createWay(parseTags(data.tag), nodes);
+                               waymap[oldid] = connection.createWay(parseTags(data.tag), nodes, push);
             }
             
             for each(data in map.relation) {
@@ -48,7 +48,7 @@ package net.systemeD.potlatch2.utils {
                                        }
                                        if (member!=null) { members.push(new RelationMember(member,role)); }
                 }
-                               relationmap[oldid] = container.createRelation(parseTags(data.tag), members);
+                               relationmap[oldid] = connection.createRelation(parseTags(data.tag), members, push);
             }
         }
 
index 6c46fab..9f5cd9c 100644 (file)
@@ -2,18 +2,19 @@ package net.systemeD.potlatch2.utils {
 
        import org.vanrijkom.shp.*;
        import org.vanrijkom.dbf.*;
-       import net.systemeD.halcyon.MapPaint;
+       import net.systemeD.halcyon.Map;
+       import net.systemeD.halcyon.connection.Connection;
        import net.systemeD.halcyon.connection.Node;
        import net.systemeD.halcyon.connection.Way;
        import net.systemeD.potlatch2.tools.Simplify;
 
        public class ShpImporter extends Importer {
 
-               public function ShpImporter(container:*, paint:MapPaint, filenames:Array, callback:Function=null, simplify:Boolean=false) {
-                       super(container,paint,filenames,callback,simplify);
+               public function ShpImporter(connection:Connection, map:Map, filenames:Array, callback:Function=null, simplify:Boolean=false) {
+                       super(connection,map,filenames,callback,simplify);
                }
 
-               override protected function doImport(): void {
+               override protected function doImport(push:Function): void {
                        // we load .shp as files[0], .shx as files[1], .dbf as files[2]
                        var shp:ShpHeader=new ShpHeader(files[0]);
                        var dbf:DbfHeader=new DbfHeader(files[2]);
@@ -36,12 +37,12 @@ package net.systemeD.potlatch2.utils {
                                                if (points!=null) {
                                                        for (var k:int=0; k < points.length; k++) {
                                                                var p:ShpPoint = ShpPoint(points[k]);
-                                                               nodestring.push(container.createNode({}, p.y, p.x));
+                                                               nodestring.push(connection.createNode({}, p.y, p.x, push));
                                                        }
                                                }
                                                if (nodestring.length>0) {
-                                                       way=container.createWay({}, nodestring);
-                                                       if (simplify) { Simplify.simplify(way, paint.map, false); }
+                                                       way=connection.createWay({}, nodestring, push);
+                                                       if (simplify) { Simplify.simplify(way, map, false); }
                                                }
                                        }
                                }
index 2358b50..d439af3 100644 (file)
 
                        // Load arbitrary GPX from provided URL
                        if (loaderInfo.parameters['gpx_url']) {
-                               var vectorlayer:VectorLayer=new VectorLayer(name,theMap,"stylesheets/gpx.css");
-                               vectorlayer.url=loaderInfo.parameters['gpx_url'];
-                               var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [vectorlayer.url], 
-                                                                   function(success:Boolean,message:String=null):void {
-                                       if (!success) return;
-                                       // >>>> REFACTOR: vectorlayer commented out
-//                                     theMap.addVectorLayer(vectorlayer);
-                                       dispatchEvent(new Event("layers_changed"));
-                                                                   }, false);
+
+                var gpx_url:String = loaderInfo.parameters['gpx_url'];
+
+                var connection:Connection = new Connection(name, gpx_url, null, null);
+                var gpx:GpxImporter=new GpxImporter(connection, theMap, [gpx_url],
+                                                function(success:Boolean,message:String=null):void {
+                                                    if (!success) return;
+                                                    var paint:MapPaint = theMap.addLayer(connection, "stylesheets/gpx.css");
+                                                    paint.updateEntityUIs(false, false);
+                                                    dispatchEvent(new Event("layers_changed"));
+                                                }, false);
                        }
 
                        // create GPS trackloader