1

I've been working to incorporate GPX file handling within my application with a pretty good degree of success but have just encountered an issue which I don't know how to solve.

When drag/dropping a file which contains shapes I've discovered that these are incorrectly interpreted as routes and it seems that the API can not read the extensions node by default. The trouble is I have no idea how to configure the readExtensions function of the GPX format to deal with this.

I don't really want to have to parse these features manually as the default drag/drop parsing is superfast and I'm sure there'd be a performance hit. This is what I have so far...

 var CustomFormatGPX = function () {
    ol.format.GPX.call(this, {
        // custom options
        readExtensions: function (feature, extensionsNode) {
            console.log(extensionsNode);
            console.log(feature);
        }
    });
};
ol.inherits(CustomFormatGPX, ol.format.GPX);

session.routeFeaturesGPX = formatGPX.writeFeatures(event.features);

So event.features is Openlayers' GeoJSON interpretation of the information and formatGPX.writeFeatures will rewrite this as GPX albeit without reading the extensions correctly. Is there a third party solution which will reparse this as GeoJSON or am I approaching this the wrong way?

There is an issue on the OL3 Github pages but my javascript knowledge is sometimes deficient and I'm struggling to understand what to place in this function. A colleague suggested XPath to try to read and manipulate the data but apparently cross-browser support is patchy. Does anyone have any experience of this?

1 Answer 1

1

I've finally managed to solve this although I'm not sure how 'hacky' the solution might be.

The problem I encountered IMHO is a common one because the GPX schema doesn't support polygon shapes and therefore renders these as open-ended routes. Many of the other systems I tested worked this way and it was therefore necessary to interrupt the generation of features in order to re-interpret any offenders in the right way.

By extending the native ol3 gpx format I was able to read and redraw the features where necessary. As it's helped me I thought I'd share the code in case anyone else encounters the problem.

var CustomFormatGPX = function () {
ol.format.GPX.call(this, {
    // custom options
    readExtensions: function (feature, extensionsNode) {
        if (extensionsNode == null) {
            return;
        }
        console.log(extensionsNode);
        console.log(feature);

        session.gpxFeature = xml2json(extensionsNode, '');
        session.gpxFeature = JSON.parse(session.gpxFeature);

        var typeNodes = jsonPath(session.gpxFeature, '$..Shape.Type').toString();

        if (typeNodes == "Area") {
            console.log(feature);
            console.info(session.gpxFeature);
            var coords = feature.getGeometry().getCoordinates();
            feature.setGeometry(new ol.geom.Polygon([coords]));
        }
    }
});
};

ol.inherits(CustomFormatGPX, ol.format.GPX);

It was necessary to utilise two 3rd-party libraries, xml2json and jsonPath and my thanks go to the respective authors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.