Skip to content

Commit

Permalink
full support for links
Browse files Browse the repository at this point in the history
GPX elements may contain multiple links, each potentially containing a
text element and a type element. Take these from a "links" array in
the properties of a feature if present and fallback to the featureLink
callback otherwise.
  • Loading branch information
joukewitteveen committed Jan 4, 2019
1 parent c10e30f commit 05c12e9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ API
* `metadata`: An object containing [metadata](http://www.topografix.com/gpx/1/1/#type_metadataType) about the to be converted dataset. Will be included in the GPX in the `<metadata>` tag. Usefull for providing information like `copyright`, `time`, `desc`, etc.
* `featureTitle`: Defines a callback that is used to construct a title (`<name>`) for a given GeoJSON feature. The callback is called with the GeoJSON feature's `properties` object.
* `featureDescription`: Defines a callback that is used to construct a description (`<desc>`) for a given GeoJSON feature. The callback is called with the GeoJSON feature's `properties` object.
* `featureLink`: Defines a callback that is used to construct an URL (`<link>`) for a given GeoJSON feature. The callback is called with the GeoJSON feature's `properties` object.
* `featureLink`: Defines a callback that is used to construct a URL (`<link>`) for a given GeoJSON feature in the absence of a `links` property that contains an array of link objects (containing a `href` and, possibly, `text` and `type`). The callback is called with the GeoJSON feature's `properties` object.
* `featureCoordTimes`: Defines a callback that is called for each feature to determine timestamps of each coordinate. Gets called with the current feature as a parameter, must return an array of UTC ISO 8601 timestamp strings for each coordinate of the feature. Alternatively. this option can be a string, in which case the corresponding feature property is used to read the times array.

The result is a string of GPX XML.
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@ function togpx( geojson, options ) {
return feature.properties.times || feature.properties.coordTimes || null;
}
function add_feature_link(o, props) {
if (options.featureLink)
if (props && props.links instanceof Array) {
o.link = [];
props.links.forEach(function(l) {
if (l.href !== undefined) {
var link = { "@href": l.href };
if (l.text !== undefined) link.text = l.text;
if (l.type !== undefined) link.type = l.type;
o.link.push(link);
}
});
} else if (options.featureLink)
o.link = { "@href": options.featureLink(props) };
}
function make_wpt(coord, time, props) {
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,34 @@ describe("properties", function () {
expect(wpt.getElementsByTagName("cmt")).to.have.length(1);
expect(wpt.getElementsByTagName("cmt")[0].textContent).to.equal("comment");
});

it('Links', function() {
var geojson, result;
geojson = {
type: "FeatureCollection",
features: [{
type: "Feature",
properties: {
links: [
{ href: "http://example.com" },
{ href: "localhost", text: "link2" }
]
},
geometry: {
type: "Point",
coordinates: [0,0]
}
}]
};
result = togpx(geojson);
result = (new DOMParser()).parseFromString(result, 'text/xml');
var links = result.getElementsByTagName("wpt")[0]
.getElementsByTagName("link");
expect(links).to.have.length(2);
expect(links[0].getAttribute("href")).to.equal("http://example.com");
expect(links[1].getAttribute("href")).to.equal("localhost");
expect(links[1].getElementsByTagName("text")[0].textContent).to.equal("link2");
});
});

describe("elevation", function () {
Expand Down
12 changes: 11 additions & 1 deletion togpx.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ function togpx( geojson, options ) {
return feature.properties.times || feature.properties.coordTimes || null;
}
function add_feature_link(o, props) {
if (options.featureLink)
if (props && props.links instanceof Array) {
o.link = [];
props.links.forEach(function(l) {
if (l.href !== undefined) {
var link = { "@href": l.href };
if (l.text !== undefined) link.text = l.text;
if (l.type !== undefined) link.type = l.type;
o.link.push(link);
}
});
} else if (options.featureLink)
o.link = { "@href": options.featureLink(props) };
}
function make_wpt(coord, time, props) {
Expand Down

0 comments on commit 05c12e9

Please sign in to comment.