Make the various GPX parsers a bit less mad
authorTom Hughes <tom@compton.nu>
Thu, 2 Dec 2010 19:24:41 +0000 (19:24 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 2 Dec 2010 19:24:41 +0000 (19:24 +0000)
net/systemeD/halcyon/connection/Trace.as
net/systemeD/potlatch2/utils/GpxImporter.as
net/systemeD/potlatch2/utils/TrackLoader.as

index 0df4ec0..8d222a7 100644 (file)
@@ -99,33 +99,31 @@ package net.systemeD.halcyon.connection {
         }
 
         private function process():void {
-            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")) {
-                    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); }
-                    }
+            default xml namespace = new Namespace("http://www.topografix.com/GPX/1/0");
+            var file:XML = new XML(_traceData);
+
+            for each (var trkseg:XML in file..trkseg) {
+                var way:Way;
+                var nodestring:Array = [];
+                for each (var trkpt:XML in trkseg.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 wpt:XML in file.wpt) {
+                var tags:Object = {};
                 for each (var tag:XML in wpt.children()) {
                     tags[tag.name()]=tag.toString();
                 }
-                var node:Node=layer.createNode(tags, wpt.@lat, wpt.@lon);
+                var node:Node = layer.createNode(tags, wpt.@lat, wpt.@lon);
                                layer.registerPOI(node);
             }
+
             layer.paint.updateEntityUIs(layer.getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
         }
     }
-}
\ No newline at end of file
+}
index 03532c6..2092e24 100644 (file)
@@ -17,30 +17,27 @@ package net.systemeD.potlatch2.utils {
                }
 
                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]);
+            default xml namespace = new Namespace("http://www.topografix.com/GPX/1/0");
+                       var file:XML = new XML(files[0]);
 
-                       for each (var trk:XML in file.child("trk")) {
-                               for each (var trkseg:XML in trk.child("trkseg")) {
-                                       var way:Way;
-                                       var nodestring:Array=[];
-                                       for each (var trkpt:XML in trkseg.child("trkpt")) {
-                                               nodestring.push(container.createNode({}, trkpt.@lat, trkpt.@lon));
-                                       }
-                                       if (nodestring.length>0) {
-                                               way=container.createWay({}, nodestring);
-                                               if (simplify) { Simplify.simplify(way, paint.map, false); }
-                                       }
+                       for each (var trkseg:XML in file..trkseg) {
+                               var way:Way;
+                var nodestring:Array = [];
+                for each (var trkpt:XML in trkseg.trkpt) {
+                                       nodestring.push(container.createNode({}, trkpt.@lat, trkpt.@lon));
+                               }
+                if (nodestring.length > 0) {
+                                       way = container.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 wpt:XML in file.wpt) {
+                               var tags:Object = {};
                                for each (var tag:XML in wpt.children()) {
                                        tags[tag.name()]=tag.toString();
                                }
-                               var node:Node=container.createNode(tags, wpt.@lat, wpt.@lon);
+                               var node:Node = container.createNode(tags, wpt.@lat, wpt.@lon);
                                container.registerPOI(node);
                        }
                }
index 11017e3..a18a7df 100644 (file)
@@ -47,29 +47,26 @@ package net.systemeD.potlatch2.utils {
                }
 
                public function parseGPX(event:Event):void {
-                       var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
-                       var xsiPattern:RegExp = new RegExp("xsi[^\"]*\"[^\"]*\"", "gi");
-                       var raw:String = String(event.target.data).replace(xmlnsPattern, "").replace(xsiPattern, "");
-                       var file:XML=new XML(raw);
+            default xml namespace = new Namespace("http://www.topografix.com/GPX/1/0/");
+                       var file:XML = new XML(event.target.data);
 
-                       for each (var trk:XML in file.child("trk")) {
-                               for each (var trkseg:XML in trk.child("trkseg")) {
-                                       var nodestring:Array=[];
-                                       var lat:Number=NaN, lastlat:Number=NaN;
-                                       var lon:Number=NaN, lastlon:Number=NaN;
-                                       for each (var trkpt:XML in trkseg.child("trkpt")) {
-                                               lat=trkpt.@lat;
-                                               lon=trkpt.@lon;
-                                               if (lastlat && nodestring.length>0 && greatCircle(lat,lon,lastlat,lastlon)>30) {
-                                                       layer.createWay({}, nodestring);
-                                                       nodestring=[];
-                                               }
-                                               nodestring.push(layer.createNode({}, lat, lon));
-                                               lastlat=lat; lastlon=lon;
-                                       }
-                                       if (nodestring.length>0) { layer.createWay({}, nodestring); }
+                       for each (var trkseg:XML in file..trkseg) {
+                               var nodestring:Array = [];
+                               var lat:Number = NaN, lastlat:Number = NaN;
+                               var lon:Number = NaN, lastlon:Number = NaN;
+                for each (var trkpt:XML in trkseg.trkpt) {
+                                       lat = trkpt.@lat;
+                    lon = trkpt.@lon;
+                    if (lastlat && nodestring.length > 0 && greatCircle(lat, lon, lastlat, lastlon) > 30) {
+                        layer.createWay({}, nodestring);
+                        nodestring = [];
+                    }
+                    nodestring.push(layer.createNode({}, lat, lon));
+                    lastlat = lat; lastlon = lon;
                                }
+                if (nodestring.length > 0) { layer.createWay({}, nodestring); }
                        }
+            
                        layer.paint.updateEntityUIs(layer.getObjectsByBbox(left,right,top,bottom), false, false);
                }