add OSM import for vector background layers
authorRichard Fairhurst <richard@systemed.net>
Fri, 9 Jul 2010 09:03:55 +0000 (09:03 +0000)
committerRichard Fairhurst <richard@systemed.net>
Fri, 9 Jul 2010 09:03:55 +0000 (09:03 +0000)
TODO.txt
net/systemeD/potlatch2/VectorSourceDialog.mxml
net/systemeD/potlatch2/utils/OsmImporter.as [new file with mode: 0755]

index 5d9ef20..9047eff 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
@@ -15,8 +15,8 @@ Potlatch 2: main outstanding issues
 == Vector background layers ==
 
 * TagTransform (cf http://wiki.openstreetmap.org/wiki/Osmosis/TagTransform)
-* Import from OSM (is this worth sharing with the XML API stuff?)
 * Load GPX from API (probably into a designated 'GPX' layer which is automatically created)
+* Connectivity isn't preserved when bringing ways through (e.g. from OSM layer)
 
 == Saving ==
 
index 5349f09..ca09205 100644 (file)
                <mx:Label htmlText="&lt;b&gt;Add new vector layer&lt;/b&gt;" />
                <mx:HBox>
                        <mx:RadioButtonGroup id="filetype" />
-                       <mx:RadioButton width="100%" groupName="{filetype}" value="gpx" id="gpx" label="GPX" selected="true" />
-                       <mx:RadioButton width="100%" groupName="{filetype}" value="shp" id="shp" label="Shapefile" />
+                       <mx:RadioButton width="100%" groupName="filetype" value="gpx" id="gpx" label="GPX" selected="true" />
+                       <mx:RadioButton width="100%" groupName="filetype" value="osm" id="osm" label="OSM" />
+                       <mx:RadioButton width="100%" groupName="filetype" value="shp" id="shp" label="Shapefile" />
                        <mx:CheckBox width="100%" label="Simplify paths" selected="true" id="simplify" />
                </mx:HBox>
                <mx:HBox>
                        <mx:Text text="URL:"/>
                        <mx:TextInput width="100%" id="src" text="" />
-                       <mx:Button label="Load" click="loadFiles(src.text, shp.selected?'shp':'gpx', simplify.selected);" enabled="{src.text == '' ? false : true}"/>
+                       <mx:Button label="Load" click="loadFiles(src.text, filetype.selectedValue.toString(), simplify.selected);" enabled="{src.text == '' ? false : true}"/>
                </mx:HBox>
        </mx:VBox>
 
@@ -58,6 +59,7 @@
        import net.systemeD.potlatch2.utils.Importer;
        import net.systemeD.potlatch2.utils.GpxImporter;
        import net.systemeD.potlatch2.utils.ShpImporter;
+       import net.systemeD.potlatch2.utils.OsmImporter;
     import mx.managers.PopUpManager;
     import mx.events.DataGridEvent;
     import mx.events.CloseEvent;
         PopUpManager.removePopUp(this);
     }
 
-       private function loadFiles(url:String,filetype:String,simplify:Boolean):void {
+       private function loadFiles(url:String,type:String,simplify:Boolean):void {
                var vectorlayer:VectorLayer=new VectorLayer(url,map,"potlatch.css");
                vectorlayer.url=url;
                map.vectorlayers[url]=vectorlayer;
                map.vectorbg.addChild(vectorlayer.paint);
 
-               if (filetype=='gpx') {
+               if (type=='gpx') {
                        var gpx:GpxImporter=new GpxImporter(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,
diff --git a/net/systemeD/potlatch2/utils/OsmImporter.as b/net/systemeD/potlatch2/utils/OsmImporter.as
new file mode 100755 (executable)
index 0000000..f98c961
--- /dev/null
@@ -0,0 +1,69 @@
+package net.systemeD.potlatch2.utils {
+
+       import net.systemeD.halcyon.MapPaint;
+       import net.systemeD.halcyon.Globals;
+       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);
+               }
+
+               // http://127.0.0.1/~richard/osm/burton.osm
+
+               override protected function doImport():void {
+                       var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
+                       var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
+                       files[0] = String(files[0]).replace(xmlnsPattern, "").replace(xsiPattern, "");
+                       var map:XML=new XML(files[0]);
+                       var data:XML;
+                       
+            var oldid:Number;
+            var tags:Object;
+
+                       var nodemap:Object={};
+                       var waymap:Object={};
+                       var relationmap:Object={};
+
+            for each(data in map.node) {
+                oldid = Number(data.@id);
+                               nodemap[oldid] = container.createNode(parseTags(data.tag), Number(data.@lat), Number(data.@lon));
+            }
+
+            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);
+            }
+            
+            for each(data in map.relation) {
+                oldid = Number(data.@id);
+                var members:Array = [];
+                for each(var memberXML:XML in data.member) {
+                    var type:String = memberXML.@type.toLowerCase();
+                    var role:String = memberXML.@role;
+                    var memberID:Number = Number(memberXML.@ref);
+                    var member:Entity = null;
+                                       switch (memberXML.@type.toLowerCase()) {
+                                               case 'node':            member=nodemap[memberID]; break;
+                                               case 'way':                     member=waymap[memberID]; break;
+                                               case 'relation':        break;  // ** TODO - cope with evil nested relations
+                                       }
+                                       if (member!=null) { members.push(new RelationMember(member,role)); }
+                }
+                               relationmap[oldid] = container.createRelation(parseTags(data.tag), members);
+            }
+        }
+
+        private function parseTags(tagElements:XMLList):Object {
+            var tags:Object = {};
+            for each (var tagEl:XML in tagElements)
+                tags[tagEl.@k] = tagEl.@v;
+            return tags;
+        }
+
+       }
+}