Share <trkseg> parsing code between Trace and TrackLoader,
[potlatch2.git] / net / systemeD / potlatch2 / utils / TrackLoader.as
index a476d75746abcd5b6ba71a5162d3dfc51746084d..2c7dcbe7bf0e69c704bde885e2bc5515006e221f 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;
-               }
-               
        }
 }