Share <trkseg> parsing code between Trace and TrackLoader,
authorRichard Fairhurst <richard@systemeD.net>
Tue, 1 Nov 2011 14:28:24 +0000 (14:28 +0000)
committerRichard Fairhurst <richard@systemeD.net>
Tue, 1 Nov 2011 14:28:24 +0000 (14:28 +0000)
and introduce a simple filter to remove unnecessary points.

net/systemeD/halcyon/connection/Trace.as
net/systemeD/potlatch2/utils/TrackLoader.as

index 07eca87..2de1bdb 100644 (file)
@@ -126,18 +126,8 @@ package net.systemeD.halcyon.connection {
                                }
                        }
 
-            for each (var trkseg:XML in file..trkseg) {
-                var way:Way;
-                var nodestring:Array = [];
-                for each (var trkpt:XML in trkseg.trkpt) {
-                    nodestring.push(connection.createNode({}, trkpt.@lat, trkpt.@lon, action.push));
-                }
-                if (nodestring.length > 0) {
-                    way = connection.createWay({}, nodestring, action.push);
-                    //if (simplify) { Simplify.simplify(way, paint.map, false); }
-                }
-            }
-
+                       Trace.parseTrkSegs(file,connection,action);
+                       
             for each (var wpt:XML in file.wpt) {
                 var tags:Object = {};
                 for each (var tag:XML in wpt.children()) {
@@ -151,5 +141,48 @@ package net.systemeD.halcyon.connection {
                        default xml namespace = new Namespace("");
             layer.updateEntityUIs(true, false);
         }
+
+               /* Draw ways from <trkseg>s, with elementary filter to remove points within 3 metres of each other. 
+                  FIXME: do auto-joining of dupes as per Importer. */
+
+               public static function parseTrkSegs(file:XML, connection:Connection, action:CompositeUndoableAction):void {
+                       for each (var ns:Namespace in file.namespaceDeclarations()) {
+                               if (ns.uri.match(/^http:\/\/www\.topografix\.com\/GPX\/1\/[01]$/)) { default xml namespace = ns; }
+                       }
+                       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;
+                               var dist:Number=0;
+                               for each (var trkpt:XML in trkseg.trkpt) {
+                                       lat = trkpt.@lat;
+                                       lon = trkpt.@lon;
+                                       if (isNaN(lastlat)) { lastlat = lat; lastlon = lon; }
+                                       dist=Trace.greatCircle(lat, lon, lastlat, lastlon);
+                                       if (dist>3) {
+                                               if (dist>50 || nodestring.length>500) {
+                                                       if (dist<=50) nodestring.push(connection.createNode({}, lat, lon, action.push));
+                                                       if (nodestring.length>1) connection.createWay({}, nodestring, action.push);
+                                                       nodestring=[];
+                                               }
+                                               nodestring.push(connection.createNode({}, lat, lon, action.push));
+                                               lastlat=lat; lastlon=lon;
+                                       }
+                               }
+                               if (nodestring.length > 1) { connection.createWay({}, nodestring, action.push); }
+                       }
+               }
+               
+               public static function greatCircle(lat1:Number,lon1:Number,lat2:Number,lon2:Number):Number {
+                       var dlat:Number=(lat2-lat1)*Math.PI/180;
+                       var dlon:Number=(lon2-lon1)*Math.PI/180;
+                       var a:Number=Math.pow(Math.sin(dlat / 2),2) + 
+                                    Math.cos(lat1*Math.PI/180) * 
+                                    Math.cos(lat2*Math.PI/180) * 
+                                    Math.pow(Math.sin(dlon / 2),2);
+                       a=Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
+                       return a*3958.75*1609;
+               }
+               
     }
 }
index a476d75..2c7dcbe 100644 (file)
@@ -57,6 +57,8 @@ package net.systemeD.potlatch2.utils {
                        loader.addEventListener(Event.COMPLETE, parseGPX);
                }
 
+               /* Load GPX data and add it to the connection as nodes/ways. */
+
                private function parseGPX(event:Event):void {
                        var file:XML = new XML(event.target.data);
                        var action:CompositeUndoableAction = new CompositeUndoableAction("add track objects");
@@ -66,24 +68,9 @@ package net.systemeD.potlatch2.utils {
                                }
                        }
 
-                       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) {
-                        connection.createWay({}, nodestring, action.push);
-                        nodestring = [];
-                    }
-                    nodestring.push(connection.createNode({}, lat, lon, action.push));
-                    lastlat = lat; lastlon = lon;
-                               }
-                if (nodestring.length > 0) { connection.createWay({}, nodestring, action.push); trace("create way");}
-                       }
-
+                       Trace.parseTrkSegs(file,connection,action);
             action.doAction(); /* just do it, don't add to undo stack */
+
                        default xml namespace = new Namespace("");
                        layer.updateEntityUIs(false, false);
                }
@@ -96,16 +83,5 @@ package net.systemeD.potlatch2.utils {
             return mp;
                }
                
-               private function greatCircle(lat1:Number,lon1:Number,lat2:Number,lon2:Number):Number {
-                       var dlat:Number=(lat2-lat1)*Math.PI/180;
-                       var dlon:Number=(lon2-lon1)*Math.PI/180;
-                       var a:Number=Math.pow(Math.sin(dlat / 2),2) + 
-                                    Math.cos(lat1*Math.PI/180) * 
-                                    Math.cos(lat2*Math.PI/180) * 
-                                    Math.pow(Math.sin(dlon / 2),2);
-                       a=Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
-                       return a*3958.75*1609;
-               }
-               
        }
 }