From 54d82c9c2e8bc82dc483edfa91b8b8bacee91460 Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Thu, 26 May 2011 16:03:39 +0100 Subject: [PATCH] Refactor all the Importer classes to use connections instead of vectorlayers --- .../potlatch2/VectorSourceDialog.mxml | 52 +++++++++---------- net/systemeD/potlatch2/utils/GpxImporter.as | 19 +++---- net/systemeD/potlatch2/utils/Importer.as | 21 ++++---- net/systemeD/potlatch2/utils/KmlImporter.as | 37 ++++++------- net/systemeD/potlatch2/utils/OsmImporter.as | 14 ++--- net/systemeD/potlatch2/utils/ShpImporter.as | 15 +++--- potlatch2.mxml | 20 +++---- 7 files changed, 91 insertions(+), 87 deletions(-) diff --git a/net/systemeD/potlatch2/VectorSourceDialog.mxml b/net/systemeD/potlatch2/VectorSourceDialog.mxml index 5dbbe723..754d19c4 100644 --- a/net/systemeD/potlatch2/VectorSourceDialog.mxml +++ b/net/systemeD/potlatch2/VectorSourceDialog.mxml @@ -33,7 +33,7 @@ - + @@ -129,7 +129,7 @@ } } - private function removeVectorLayer():void { + private function removeLayer():void { map.removeLayerByName(dataGrid.selectedItem.name); dispatchEvent(new Event("layers_changed")); } @@ -144,31 +144,29 @@ 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); + } } ]]> diff --git a/net/systemeD/potlatch2/utils/GpxImporter.as b/net/systemeD/potlatch2/utils/GpxImporter.as index 6c6eac4b..c3d64190 100644 --- a/net/systemeD/potlatch2/utils/GpxImporter.as +++ b/net/systemeD/potlatch2/utils/GpxImporter.as @@ -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(""); diff --git a/net/systemeD/potlatch2/utils/Importer.as b/net/systemeD/potlatch2/utils/Importer.as index 7e228423..c664530d 100644 --- a/net/systemeD/potlatch2/utils/Importer.as +++ b/net/systemeD/potlatch2/utils/Importer.as @@ -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."); } diff --git a/net/systemeD/potlatch2/utils/KmlImporter.as b/net/systemeD/potlatch2/utils/KmlImporter.as index 947240a8..cfed8331 100644 --- a/net/systemeD/potlatch2/utils/KmlImporter.as +++ b/net/systemeD/potlatch2/utils/KmlImporter.as @@ -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; diff --git a/net/systemeD/potlatch2/utils/OsmImporter.as b/net/systemeD/potlatch2/utils/OsmImporter.as index f456899f..c3d84f2f 100644 --- a/net/systemeD/potlatch2/utils/OsmImporter.as +++ b/net/systemeD/potlatch2/utils/OsmImporter.as @@ -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); } } diff --git a/net/systemeD/potlatch2/utils/ShpImporter.as b/net/systemeD/potlatch2/utils/ShpImporter.as index 6c46fab6..9f5cd9ce 100644 --- a/net/systemeD/potlatch2/utils/ShpImporter.as +++ b/net/systemeD/potlatch2/utils/ShpImporter.as @@ -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); } } } } diff --git a/potlatch2.mxml b/potlatch2.mxml index 2358b50d..d439af39 100644 --- a/potlatch2.mxml +++ b/potlatch2.mxml @@ -285,15 +285,17 @@ // 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 -- 2.36.1