Menu

Using setExtensionData

2014-12-01
2014-12-02
  • Andrew Shiels

    Andrew Shiels - 2014-12-01

    Hi,

    Thanks for a great library. It has helped me a lot but I have a few questions.

    Is there an example available using setExtensionData? So far I have extended IExtensionParser and implemented writeTrackExtensionData (below):

    @Override
    public void writeTrackExtensionData(Node node, Track wpt, Document doc) {
    // append node1
    final Node node1 = doc.createElement("Node1");
    node1.setTextContent(mNode1Value);
    node.appendChild(distanceNode);

        // append node2
        final Node node2 = doc.createElement("Node2");
        node2.setTextContent(mNode2Value);
        node.appendChild(node2);
    }
    

    In my exporter class I have create a HashMap and add my Parser ID as key but I have no idea what the 2nd Parameter is - I have set it to null.

    final HashMap<string, object=""> extParserList = new HashMap<string, object="">();
    extParsersList.put(GpsLogExtensionParser.PARSER_ID, null);
    track.setExtensionData(extParserList);</string,></string,>

    The code seems to work OK but what is the 2nd parameter for? I thought it be an object I could pass to the IExtension parse class but this doesn't seem to be the case.

    I would also like to set extension data for each track point but how can I pass data from my importer class to the IEntensionParser class?

    I want something like this:

                Track track = new Track();
    
        // create list for our list of waypoints
        final ArrayList<Waypoint> arrayWayPoints = new ArrayList<Waypoint>();
    
        for (MyItem item : MyItems) {
            Waypoint waypoint = new Waypoint();
            waypoint.setLatitude(item.getLatitude());
            waypoint.setLongitude(item.getLongitude());
            waypoint.setTime(item.getDate());
            waypoint.setElevation(item.getAltitude());
    
                        // what do I do here? Can I pass my 'item' to the writeWaypointExtensionData function?
            waypoint.setExtensionData(extensionData);
    
            arrayWayPoints.add(waypoint);
        }
    
                track.setTrackPoints(arrayWayPoints);
    

    I appreciate any help anyone can offer!
    Thanks,
    Andy

     
  • Gheorghe Bot

    Gheorghe Bot - 2014-12-02

    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);
    This is very important because this will make GPXParser aware of your custom parser.
    You are on the right track to implement the IExtensionParser interface with the following code:
    @Override
    public void writeTrackExtensionData(Node node, Track wpt, Document doc) {
    // append node1
    final Node node1 = doc.createElement("Node1");
    node1.setTextContent(mNode1Value);
    node.appendChild(distanceNode);
    // append node2
    final Node node2 = doc.createElement("Node2");
    node2.setTextContent(mNode2Value);
    node.appendChild(node2);
    }
    Now, form what I see in your code, you need to add this custom parser to GPXParser:
    GPXParser p = new GPXParser();
    p.addExtensionParser(new GpsLogExtensionParser());
    This will call your custom parser when writing the GPX structure to a file.
    In order to create a waypoint with extension information, you need to add some useful information to be written as custom data:
    Waypoint p = new Waypoint();
    ....
    p.addExtensionData(GpsLogExtensionParser.PARSER_ID, "some useful information that you will want to write in the gpx file using the custom extension parser");
    The second parameter can be any java object that you will use to pass information to the custom parser.
    In the parser (GpsLogExtensionParser), you need to use:
    public void writeWaypointExtensionData(Node node, Waypoint wpt, Document doc);
    String myData = (String)wpt.getExtensionData(GpsLogExtensionParser.PARSER_ID)
    you will have access to the extension data that you stored in your waypoint bean.
    After that, you can use that information to process it the way you want it.

    Hope this helps,
    Ghita

     

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.