From 9716652baacfe804035a0ba35522aad456845221 Mon Sep 17 00:00:00 2001 From: Jouke Witteveen Date: Wed, 17 Oct 2018 14:41:02 +0200 Subject: [PATCH] broaden wptType property support 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. --- README.md | 7 ++++--- index.js | 13 ++++++++++--- test/test.js | 33 +++++++++++++++++++++++++++++++-- togpx.js | 13 ++++++++++--- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 7dfc312..32ba66d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index 2a82561..97da8dd 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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 diff --git a/test/test.js b/test/test.js index e667e03..0decce6 100644 --- a/test/test.js +++ b/test/test.js @@ -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", @@ -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() { @@ -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 () { diff --git a/togpx.js b/togpx.js index aeb7135..ffe400b 100644 --- a/togpx.js +++ b/togpx.js @@ -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; @@ -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