Menu

Add gpx extension for heart rate

2013-09-16
2013-09-17
  • Flefrancois

    Flefrancois - 2013-09-16

    Hi,

    First, thank you for your job.

    I need some help to create an ExtensionParser. I want to get nodes with
    heart rate. In my GPX, this look like:

    <trkpt lat="43.864633664488792" lon="-1.323068914934993">
    <ele>-11.880000000000001</ele>

    <extensions>
    <gpxtpx:TrackPointExtension>
    <gpxtpx:hr>85</gpxtpx:hr>
    </gpxtpx:TrackPointExtension>
    </extensions>
    </trkpt>

    How can i obtain the "85" ?

    Thanks for your help

    Fabien (France)

     
  • Gheorghe Bot

    Gheorghe Bot - 2013-09-16

    Hello,

    In order to get information from the "extensions" node, you need to write a class that implements the IExtensionParser interface. This class will define methods for handling "extensions" data from all standard gpx entities (trk, rte, wpt). After you have implemented your class, you need to register it with the GPXParser:

    GPXParser p = new GPXParser();
    p.addExtensionParser(an instance of your parser class);
    p.parseGPX("your gpx file");
    

    if you need additional help, please post a fragment of a gpx file, so I may see the xml structure that needs to be parsed.

    Here is a link with some explanation on how to use extension parsers:

    http://gpxparser.alternativevision.ro/pages/extensionsHowTo.html

     

    Last edit: Gheorghe Bot 2013-09-16
  • Flefrancois

    Flefrancois - 2013-09-17

    Thanks for your help. I created this topic via mail, so the XML structure is not visible.

      <trkpt lat="43.86337042786181" lon="-1.326098805293441">
        <ele>19.69140625</ele>
        <time>2013-09-05T09:14:41Z</time>
        <extensions>
          <gpxtpx:TrackPointExtension>
            <gpxtpx:hr>101</gpxtpx:hr>
          </gpxtpx:TrackPointExtension>
        </extensions>
      </trkpt>
    
     
  • Gheorghe Bot

    Gheorghe Bot - 2013-09-17

    Hello, this is how to add the extension parser:

    GPXParser p = new GPXParser();
            HeartrateExtensionParser parser = new HeartrateExtensionParser();
            p.addExtensionParser(parser);
            try {
                GPX gpx = p.parseGPX(new FileInputStream("fells_loop.gpx"));
                Iterator<Waypoint> itw = gpx.getWaypoints().iterator();
                while(itw.hasNext()) {
                    Waypoint wpt = itw.next();
                    Integer heartrate = (Integer)wpt.getExtensionData(parser.getId());
                    HashMap<String, Object> ext = wpt.getExtensionData();
                    System.out.println(heartrate);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    

    This is a sample HeartrateExtensionParser:

    ~~~~~~~~~
    import org.alternativevision.gpx.beans.GPX;
    import org.alternativevision.gpx.beans.Route;
    import org.alternativevision.gpx.beans.Track;
    import org.alternativevision.gpx.beans.Waypoint;
    import org.alternativevision.gpx.extensions.IExtensionParser;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;

    public class HeartrateExtensionParser implements IExtensionParser {

    public static final String myID = "HeartrateExtension";
    
    public String getId() {
        return myID;
    }
    
    public Object parseGPXExtension(Node arg0) {
        Integer hr = new Integer(2);
        return hr;
    }
    
    public Object parseRouteExtension(Node arg0) {
        Integer hr = new Integer(2);
        return hr;
    }
    
    public Object parseTrackExtension(Node arg0) {
        Integer hr = new Integer(2);
        return hr;
    }
    
    public Object parseWaypointExtension(Node node) {
        Integer hr = new Integer(2);
        String val = "";
        for(int idx = 0; idx < node.getChildNodes().getLength(); idx++) {
            Node currentNode = node.getChildNodes().item(idx);
            if("gpxtpx:TrackPointExtension".equals(currentNode.getNodeName())) {
                for(int idx1 = 0; idx < currentNode.getChildNodes().getLength(); idx++) {
                    Node hrNode = currentNode.getChildNodes().item(idx);
                    if("gpxtpx:hr".equals(hrNode.getNodeName())){
                        val = hrNode.getFirstChild().getNodeValue();
                        hr = Integer.parseInt(val);
                    }
                }
            }
        }
        return hr;
    }
    
    public void writeGPXExtensionData(Node arg0, GPX arg1, Document arg2) {
        // TODO Auto-generated method stub
    
    }
    
    public void writeRouteExtensionData(Node arg0, Route arg1, Document arg2) {
        // TODO Auto-generated method stub
    
    }
    
    public void writeTrackExtensionData(Node arg0, Track arg1, Document arg2) {
        // TODO Auto-generated method stub
    
    }
    
    public void writeWaypointExtensionData(Node arg0, Waypoint arg1,
            Document arg2) {
        // TODO Auto-generated method stub
    
    }
    

    }

    ~~~~~~~

    I made the extension parsing to a waypoint node, you will need to add node processing to the corresponding nodes from the gpx entities.

    Cheers,
    Ghita

     
  • Flefrancois

    Flefrancois - 2013-09-17

    Thanks ! I will try this.

     
  • Flefrancois

    Flefrancois - 2013-09-17

    Perfect, it's works, thanks !!

     
  • Gheorghe Bot

    Gheorghe Bot - 2013-09-17

    Great.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.