diff --git a/README.md b/README.md index 32ba66d..1940f80 100644 --- a/README.md +++ b/README.md @@ -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 `` tag. Usefull for providing information like `copyright`, `time`, `desc`, etc. * `featureTitle`: Defines a callback that is used to construct a title (``) 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 (``) 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 (``) 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 (``) 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. diff --git a/index.js b/index.js index 64181d7..dc701d0 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/test/test.js b/test/test.js index 0decce6..fad3944 100644 --- a/test/test.js +++ b/test/test.js @@ -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 () { diff --git a/togpx.js b/togpx.js index 6a10e4a..07b5843 100644 --- a/togpx.js +++ b/togpx.js @@ -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) {