From 480a6a85ba22f725e520610e7038edba8bcfc805 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Fri, 21 Feb 2014 20:27:04 +0100 Subject: [PATCH] Add trk serialization for ol.format.GPX --- src/ol/format/gpxformat.js | 98 ++++++++++++++++++++++++++- test/spec/ol/format/gpxformat.test.js | 30 ++++---- 2 files changed, 113 insertions(+), 15 deletions(-) diff --git a/src/ol/format/gpxformat.js b/src/ol/format/gpxformat.js index 08532fbd45..248e3cf104 100644 --- a/src/ol/format/gpxformat.js +++ b/src/ol/format/gpxformat.js @@ -514,7 +514,44 @@ ol.format.GPX.writeRte_ = function(node, feature, objectStack) { * @param {ol.Feature} feature Feature. * @param {Array.<*>} objectStack Object stack. * @private - * @template T + */ +ol.format.GPX.writeTrk_ = function(node, feature, objectStack) { + var properties = feature.getProperties(); + var context = {node: node, 'properties': properties}; + var geometry = feature.getGeometry(); + if (goog.isDef(geometry)) { + goog.asserts.assertInstanceof(geometry, ol.geom.MultiLineString); + goog.object.set(properties, 'trkseg', geometry.getLineStrings()); + } + var parentNode = objectStack[objectStack.length - 1].node; + var orderedKeys = ol.format.GPX.TRK_SEQUENCE_[parentNode.namespaceURI]; + var values = ol.xml.makeSequence(properties, orderedKeys); + ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ (context), + ol.format.GPX.TRK_SERIALIZERS_, ol.xml.OBJECT_PROPERTY_NODE_FACTORY, + values, objectStack, orderedKeys); +}; + + +/** + * @param {Node} node Node. + * @param {ol.geom.LineString} lineString LineString. + * @param {Array.<*>} objectStack Object stack. + * @private + */ +ol.format.GPX.writeTrkSeg_ = function(node, lineString, objectStack) { + var context = {node: node, 'geometryLayout': lineString.getLayout(), + 'properties': {}}; + ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ (context), + ol.format.GPX.TRKSEG_SERIALIZERS_, ol.format.GPX.TRKSEG_NODE_FACTORY_, + lineString.getCoordinates(), objectStack); +}; + + +/** + * @param {Node} node Node. + * @param {ol.Feature} feature Feature. + * @param {Array.<*>} objectStack Object stack. + * @private */ ol.format.GPX.writeWpt_ = function(node, feature, objectStack) { var context = objectStack[objectStack.length - 1]; @@ -585,6 +622,63 @@ ol.format.GPX.RTE_SERIALIZERS_ = ol.xml.makeStructureNS( }); +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GPX.TRK_SEQUENCE_ = ol.xml.makeStructureNS( + ol.format.GPX.NAMESPACE_URIS_, [ + 'name', 'cmt', 'desc', 'src', 'link', 'number', 'type', 'trkseg' + ]); + + +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GPX.TRK_SERIALIZERS_ = ol.xml.makeStructureNS( + ol.format.GPX.NAMESPACE_URIS_, { + 'name': ol.xml.makeChildAppender(ol.xml.makeSimpleTypeWriter( + ol.format.XSD.writeStringTextNode)), + 'cmt': ol.xml.makeChildAppender(ol.xml.makeSimpleTypeWriter( + ol.format.XSD.writeStringTextNode)), + 'desc': ol.xml.makeChildAppender(ol.xml.makeSimpleTypeWriter( + ol.format.XSD.writeStringTextNode)), + 'src': ol.xml.makeChildAppender(ol.xml.makeSimpleTypeWriter( + ol.format.XSD.writeStringTextNode)), + 'link': ol.xml.makeChildAppender(ol.format.GPX.writeLink_), + 'number': ol.xml.makeChildAppender(ol.xml.makeSimpleTypeWriter( + ol.format.XSD.writeNonNegativeIntegerTextNode)), + 'type': ol.xml.makeChildAppender(ol.xml.makeSimpleTypeWriter( + ol.format.XSD.writeStringTextNode)), + 'trkseg': ol.xml.makeChildrenAppender(ol.format.GPX.writeTrkSeg_) + }); + + +/** + * @const + * @param {*} value Value. + * @param {Array.<*>} objectStack Object stack. + * @param {string=} opt_nodeName Node name. + * @return {Node} Node. + * @private + */ +ol.format.GPX.TRKSEG_NODE_FACTORY_ = ol.xml.makeSimpleNodeFactory('trkpt'); + + +/** + * @const + * @type {Object.>} + * @private + */ +ol.format.GPX.TRKSEG_SERIALIZERS_ = ol.xml.makeStructureNS( + ol.format.GPX.NAMESPACE_URIS_, { + 'trkpt': ol.xml.makeChildAppender(ol.format.GPX.writeWptType_) + }); + + /** * @const * @type {Object.>} @@ -682,7 +776,7 @@ ol.format.GPX.GPX_NODE_FACTORY_ = function(value, objectStack, opt_nodeName) { ol.format.GPX.GPX_SERIALIZERS_ = ol.xml.makeStructureNS( ol.format.GPX.NAMESPACE_URIS_, { 'rte': ol.xml.makeChildAppender(ol.format.GPX.writeRte_), - //'MultiLineString': ol.xml.makeChildAppender(ol.format.GPX.writeTrk_), + 'trk': ol.xml.makeChildAppender(ol.format.GPX.writeTrk_), 'wpt': ol.xml.makeChildAppender(ol.format.GPX.writeWpt_) }); diff --git a/test/spec/ol/format/gpxformat.test.js b/test/spec/ol/format/gpxformat.test.js index 8a2c14c1e5..bb9fa2bc22 100644 --- a/test/spec/ol/format/gpxformat.test.js +++ b/test/spec/ol/format/gpxformat.test.js @@ -5,17 +5,13 @@ describe('ol.format.GPX', function() { var format; beforeEach(function() { - format = new ol.format.GPX(); + format = new ol.format.GPX.V1_1(); }); describe('readFeatures', function() { describe('rte', function() { - beforeEach(function() { - format = new ol.format.GPX.V1_1(); - }); - it('can read an empty rte', function() { var text = '' + @@ -103,7 +99,7 @@ describe('ol.format.GPX', function() { expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM); }); - it('can read various trk attributes', function() { + it('can read and write various trk attributes', function() { var text = '' + ' ' + @@ -132,9 +128,11 @@ describe('ol.format.GPX', function() { expect(f.get('linkType')).to.be('Link type'); expect(f.get('number')).to.be(1); expect(f.get('type')).to.be('Type'); + var serialized = format.writeFeatures(fs); + expect(serialized).to.xmleql(ol.xml.load(text)); }); - it('can read a trk with an empty trkseg', function() { + it('can read and write a trk with an empty trkseg', function() { var text = '' + ' ' + @@ -149,9 +147,11 @@ describe('ol.format.GPX', function() { expect(g).to.be.an(ol.geom.MultiLineString); expect(g.getCoordinates()).to.eql([[]]); expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM); + var serialized = format.writeFeatures(fs); + expect(serialized).to.xmleql(ol.xml.load(text)); }); - it('can read a trk with a trkseg with multiple trkpts', function() { + it('can read/write a trk with a trkseg with multiple trkpts', function() { var text = '' + ' ' + @@ -177,9 +177,11 @@ describe('ol.format.GPX', function() { [[2, 1, 3, 1263115752], [6, 5, 7, 1263115812]] ]); expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM); + var serialized = format.writeFeatures(fs); + expect(serialized).to.xmleql(ol.xml.load(text)); }); - it('can read a trk with multiple trksegs', function() { + it('can read and write a trk with multiple trksegs', function() { var text = '' + ' ' + @@ -216,16 +218,14 @@ describe('ol.format.GPX', function() { [[9, 8, 10, 1263115872], [12, 11, 13, 1263115932]] ]); expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM); + var serialized = format.writeFeatures(fs); + expect(serialized).to.xmleql(ol.xml.load(text)); }); }); describe('wpt', function() { - beforeEach(function() { - format = new ol.format.GPX.V1_1(); - }); - it('can read and write a wpt', function() { var text = '' + @@ -355,6 +355,10 @@ describe('ol.format.GPX', function() { describe('XML namespace support', function() { + beforeEach(function() { + format = new ol.format.GPX(); + }); + it('can read features with a version 1.0 namespace', function() { var text = '' +