Skip to content

Commit

Permalink
broaden wptType property support
Browse files Browse the repository at this point in the history
Support all string properties of the GPX wptType type. For the name
and description tag, the user callback is used as a fallback. The
callbacks are always used for tags that are not of the wptType type.
  • Loading branch information
joukewitteveen committed Jan 4, 2019
1 parent a679798 commit c10e30f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ The conversion from GeoJSON to GPX is (by definition) lossy, because not every G
* Points are converted to [Waypoints](http://www.topografix.com/gpx/1/1/#type_wptType).
* Lines are converted to [Tracks](http://www.topografix.com/gpx/1/1/#type_trkType).
* (Multi)Polygons are represented as a [Track](http://www.topografix.com/gpx/1/1/#type_trkType) of their outline(s).
* By default, the `name` tag of GPX elements will be determined by a simple heuristic that searches for the following GeoJSON properties to construct a meaningful title: `name`, `ref`, `id`
* By default, the `desc` tag of GPX elements will be constructed by concatenating all respective GeoJSON properties.
* Elevation is included in the output if the GeoJSON coordinates contain altitude as a third value (`[lon, lat, altitude]`)
* Elevation is included in the output if the GeoJSON coordinates contain altitude as a third value (`[lon, lat, altitude]`).
* Timestamps are included in the GPX output if the GeoJSON has a `times` or `coordTimes` property that is an array of UTC ISO 8601 timestamp strings. See the `featureCoordTimes` option for customizing this behaviour.
* Properties of a point that match the name of a supported GPX waypoint tag are included in the output. For waypoints, the `featureTitle` and `featureDescription` callbacks are only used as fallbacks.
* By default, the `name` tag of GPX elements will be determined by a simple heuristic that searches for the following GeoJSON properties to construct a meaningful title: `name`, `ref`, `id`.
* By default, the `desc` tag of GPX elements will be constructed by concatenating all respective GeoJSON properties.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ function togpx( geojson, options ) {
if (coord[2] !== undefined) pt.ele = coord[2];
if (time) pt.time = time;
if (props !== undefined) {
pt.name = options.featureTitle(props);
pt.desc = options.featureDescription(props);
["name", "cmt", "desc", "src", "sym", "type"].forEach(function(k) {
if (props[k] !== undefined) pt[k] = props[k];
});
if (pt.name === undefined)
pt.name = options.featureTitle(props);
if (pt.desc === undefined)
pt.desc = options.featureDescription(props);
add_feature_link(pt, props);
}
return pt;
Expand Down Expand Up @@ -107,7 +112,9 @@ function togpx( geojson, options ) {
var coords = f.geometry.coordinates;
if (f.geometry.type == "Point") coords = [coords];
coords.forEach(function(c) {
gpx.gpx.wpt.push(make_wpt(c, undefined, f.properties));
gpx.gpx.wpt.push(
make_wpt(c, f.properties && f.properties.time, f.properties)
);
});
break;
// LineStrings
Expand Down
33 changes: 31 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ describe("properties", function () {
features: [{
type: "Feature",
properties: {
tags: { name: "name" },
name: "not_name"
tags: { id: "name" },
ref: "not_name"
},
geometry: {
type: "Point",
Expand Down Expand Up @@ -443,6 +443,13 @@ describe("properties", function () {
var wpt = result.getElementsByTagName("wpt")[0];
expect(wpt.getElementsByTagName("desc")).to.have.length(1);
expect(wpt.getElementsByTagName("desc")[0].textContent).to.equal("p1=foo\np2=bar");
// explicitely set description
geojson.features[0].properties.desc = "description";
result = togpx(geojson);
result = (new DOMParser()).parseFromString(result, 'text/xml');
wpt = result.getElementsByTagName("wpt")[0];
expect(wpt.getElementsByTagName("desc")).to.have.length(1);
expect(wpt.getElementsByTagName("desc")[0].textContent).to.equal("description");
});

it('Description (from tags)', function() {
Expand Down Expand Up @@ -549,6 +556,28 @@ describe("properties", function () {
expect(pts[1].getElementsByTagName("time")).to.have.length(1);
expect(pts[1].getElementsByTagName("time")[0].textContent).to.equal("2014-06-23T20:29:11Z");
});

it('Comments', function() {
var geojson, result;
geojson = {
type: "FeatureCollection",
features: [{
type: "Feature",
properties: {
cmt: "comment"
},
geometry: {
type: "Point",
coordinates: [1.0,2.0]
}
}]
};
result = togpx(geojson);
result = (new DOMParser()).parseFromString(result, 'text/xml');
var wpt = result.getElementsByTagName("wpt")[0];
expect(wpt.getElementsByTagName("cmt")).to.have.length(1);
expect(wpt.getElementsByTagName("cmt")[0].textContent).to.equal("comment");
});
});

describe("elevation", function () {
Expand Down
13 changes: 10 additions & 3 deletions togpx.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ function togpx( geojson, options ) {
if (coord[2] !== undefined) pt.ele = coord[2];
if (time) pt.time = time;
if (props !== undefined) {
pt.name = options.featureTitle(props);
pt.desc = options.featureDescription(props);
["name", "cmt", "desc", "src", "sym", "type"].forEach(function(k) {
if (props[k] !== undefined) pt[k] = props[k];
});
if (pt.name === undefined)
pt.name = options.featureTitle(props);
if (pt.desc === undefined)
pt.desc = options.featureDescription(props);
add_feature_link(pt, props);
}
return pt;
Expand Down Expand Up @@ -108,7 +113,9 @@ function togpx( geojson, options ) {
var coords = f.geometry.coordinates;
if (f.geometry.type == "Point") coords = [coords];
coords.forEach(function(c) {
gpx.gpx.wpt.push(make_wpt(c, undefined, f.properties));
gpx.gpx.wpt.push(
make_wpt(c, f.properties && f.properties.time, f.properties)
);
});
break;
// LineStrings
Expand Down

0 comments on commit c10e30f

Please sign in to comment.