4 Replies Latest reply on Dec 27, 2007 11:28 AM by buabco

    Parsing GPX Format

    k0nG86
      Hi. Has anyone managed to parse gpx data as xml. The xml looks like this:

      <gpx version="1.1" creator="Mobile Trail Explorer">
      <trk>
      <trkseg>
      <trkpt lat="51.539498" lon="-0.488798">
      <ele>31.5</ele>
      <time>2007-11-30T13:07:03Z</time>
      </trkpt>
      </trk>
      </gpx>

      so far my parse function looks like this:

      public static function parseGPS(journey:FileStream):ArrayCollection{
      var gpe:GPSEntry = new GPSEntry();
      var ac:ArrayCollection = new ArrayCollection();
      var str:String = journey.readUTFBytes(journey.bytesAvailable);
      var xml:XML = XML(str);
      var trkptLength:Number = xml.childNodes[0].childNodes[0].childNodes.length;
      var gpsTagCnt:Number = 3;
      var trackpointArr:Array = new Array(gpsTagCnt);

      for (var i:uint=0; i<trkptLength; i++) {
      trackpointArr = new Array(gpsTagCnt);
      var now_trkpt:XML = xml.childNodes[0].childNodes[0].childNodes
      ;
      trackpointArr [0] = now_trkpt.attributes.lat;
      trackpointArr
      [1] = now_trkpt.attributes.lon;
      trackpointArr [2] = now_trkpt.childNodes[0].firstChild;
      trackpointArr
      [3] = now_trkpt.childNodes[1].firstChild;

      gpe.ele = trackpointArr [2];
      gpe.lat = trackpointArr
      [0];
      gpe.long = trackpointArr [1];
      gpe.time = trackpointArr
      [3];

      ac.addItem(gpe);
      }

      return ac;

      }


      The file stream is passed from the mxml app. It returns a array collection to display in a datagrid
        • 1. Parsing GPX Format
          k0nG86 Level 1
          I've managed to do it. Here is the answer.

          public static function parseGPS(journey:FileStream):ArrayCollection{

          var ac:ArrayCollection = new ArrayCollection();
          var str:String = journey.readUTFBytes(journey.bytesAvailable);
          var xml:XML = XML(str);
          xml.ignoreWhite = true;
          var nodes:XMLList = xml.children();
          var trkptLength:Number = nodes.children()[0].children().length();


          for (var i:Number=0; i<trkptLength; i++) {
          var gpe:GPSEntry = new GPSEntry();
          gpe.lat = nodes.children()[0].children() .@lat;
          gpe.long = nodes.children()[0].children()
          .@lon;
          gpe.ele = nodes.children()[0].children() .children()[0];
          gpe.time = nodes.children()[0].children()
          .children()[1];

          ac.addItem(gpe);
          }

          return ac;

          }
          • 2. Re: Parsing GPX Format
            buabco
            HI!

            You'll need this function too:

            static public function parseGPXDate(fecha:String):Date {
            var ipos:int = fecha.indexOf("T", 0);
            var epos:int = 0;

            var datePart:String = "";
            var year:int = 0;
            var month:int = 0;
            var day:int = 0;

            var timePart:String = "";
            var hour:int = 0;
            var min:int = 0;
            var sec:int = 0;

            //Partimos la fecha y la hora
            if (ipos != -1) {
            datePart = fecha.substr(0, ipos);
            timePart = fecha.substr(ipos + 1);

            //Ahora procesemos la fecha
            ipos = datePart.indexOf("-", 0);
            if (ipos != -1) {
            year = int(datePart.substr(0, ipos));

            epos = datePart.indexOf("-", ipos + 1);
            if (epos != -1) month = int(datePart.substring(ipos + 1, epos));
            day = int(datePart.substring(epos + 1));

            }

            //Ahora la hora
            ipos = timePart.indexOf(":", 0);
            if (ipos != -1) {
            hour = int(timePart.substr(0, ipos));
            epos = timePart.indexOf(":", ipos + 1);
            if (epos != -1) min = int(timePart.substring(ipos + 1, epos));
            sec = int(timePart.substring(epos + 1, timePart.length - 1));
            }
            }
            return new Date(year, month, day, hour, min, sec);
            }
            • 3. Re: Parsing GPX Format
              k0nG86 Level 1
              Ah brilliant! Thats just what i need. But for some reason its returning my dates + 1 month. For instance instead of saying Thu 27 Dec it was saying Thu 27 Jan

              So all i did was alter the return variable:

              return new Date(year, month - 1, day, hour, min, sec);
              • 4. Re: Parsing GPX Format
                buabco Level 1
                ugh... sorry that was the old version of the function :), anyways you have to change the last line to:

                return new Date(year, month-1, day, hour, min, sec);

                Thats becauese flash uses 0 for the first month.