Import GPX files (though we really need to simplify them too)
authorRichard Fairhurst <richard@systemed.net>
Sat, 1 May 2010 18:14:33 +0000 (18:14 +0000)
committerRichard Fairhurst <richard@systemed.net>
Sat, 1 May 2010 18:14:33 +0000 (18:14 +0000)
net/systemeD/potlatch2/VectorSourceDialog.mxml
net/systemeD/potlatch2/utils/GpxImporter.as [new file with mode: 0644]

index 8fc7524..ef1b6f0 100644 (file)
@@ -9,6 +9,7 @@
        import net.systemeD.halcyon.Globals;
        import net.systemeD.halcyon.vectorlayers.*;
        import net.systemeD.potlatch2.utils.Importer;
+       import net.systemeD.potlatch2.utils.GpxImporter;
        import net.systemeD.potlatch2.utils.ShpImporter;
     import mx.managers.PopUpManager;
     import mx.events.CloseEvent;
         PopUpManager.removePopUp(this);
     }
 
-       private function loadSHP(url:String):void {
+       private function loadFiles(url:String,filetype:String):void {
         PopUpManager.removePopUp(this);
                var theMap:Map = Globals.vars.root;
                var vectorlayer:CustomVectorLayer=new CustomVectorLayer(url,theMap,"potlatch.css");
                theMap.vectorlayers.push(vectorlayer);
                theMap.vectorbg.addChild(vectorlayer.paint);
 
-               var re:RegExp=/.shp$/i;
-               url=url.replace(re,'');
-
-               var shp:ShpImporter=new ShpImporter(vectorlayer,
-                                                   vectorlayer.paint,
-                                                   [url+".shp",url+".shx",url+".dbf"]);
+               if (filetype=='gpx') {
+                       var gpx:GpxImporter=new GpxImporter(vectorlayer,vectorlayer.paint,[url]);
+               } else {
+                       var re:RegExp=/.shp$/i; url=url.replace(re,'');
+                       var shp:ShpImporter=new ShpImporter(vectorlayer,
+                                                           vectorlayer.paint,
+                                                           [url+".shp",url+".shx",url+".dbf"]);
+               }
        }
 
               ]]>
   </mx:Script>
   <mx:Text>
     <mx:text>
-      Input the URL of a shapefile to import.
+      Input the URL of a file to import.
     </mx:text>
   </mx:Text>
   <mx:TextInput id="src" text="" />
+  <mx:RadioButtonGroup id="filetype" />
+  <mx:RadioButton groupName="{filetype}" value="shp" id="shp" label="Shapefile" selected="true" />
+  <mx:RadioButton groupName="{filetype}" value="gpx" id="gpx" label="GPX" />
   <mx:ControlBar>
     <mx:Spacer width="100%"/>
-    <mx:Button label="Ok" click="loadSHP(src.text);"/>
+    <mx:Button label="Ok" click="loadFiles(src.text, shp.selected?'shp':'gpx');"/>
   </mx:ControlBar>
 
-</mx:TitleWindow>
\ No newline at end of file
+</mx:TitleWindow>
diff --git a/net/systemeD/potlatch2/utils/GpxImporter.as b/net/systemeD/potlatch2/utils/GpxImporter.as
new file mode 100644 (file)
index 0000000..ce0983f
--- /dev/null
@@ -0,0 +1,40 @@
+package net.systemeD.potlatch2.utils {
+
+       import net.systemeD.halcyon.MapPaint;
+       import net.systemeD.halcyon.Globals;
+       import net.systemeD.halcyon.connection.Node;
+
+       public class GpxImporter extends Importer {
+
+               public function GpxImporter(container:*, paint:MapPaint, filenames:Array) {
+trace("started gpximporter");
+                       super(container,paint,filenames);
+               }
+
+               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 file:XML=new XML(files[0]);
+
+                       for each (var trk:XML in file.child("trk")) {
+                               trace("trk");
+                               for each (var trkseg:XML in trk.child("trkseg")) {
+                                       trace("trkseg");
+                                       var nodestring:Array=[];
+                                       for each (var trkpt:XML in trkseg.child("trkpt")) {
+                                               nodestring.push(container.createNode({}, trkpt.@lat, trkpt.@lon));
+                                               trace("adding point at "+trkpt.@lat+","+trkpt.@lon);
+                                       }
+                                       if (nodestring.length>0) {
+                                               paint.createWayUI(container.createWay({}, nodestring));
+                                       }
+                               }
+                       }
+                       for each (var wpt:XML in file.child("wpt")) {
+                               // ** could potentially get the children and add them as gpx:tags
+                               paint.createNodeUI(container.createNode({}, wpt.lat, wpt.lon));
+                       }
+               }
+       }
+}