Add trk serialization for ol.format.GPX

This commit is contained in:
ahocevar
2014-02-21 20:27:04 +01:00
parent 791cb2415b
commit 480a6a85ba
2 changed files with 113 additions and 15 deletions

View File

@@ -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.<string, Array.<string>>}
* @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.<string, Object.<string, ol.xml.Serializer>>}
* @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.<string, Object.<string, ol.xml.Serializer>>}
* @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.<string, Array.<string>>}
@@ -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_)
});

View File

@@ -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 =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
@@ -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 =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <trk>' +
@@ -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 =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <trk>' +
@@ -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 =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <trk>' +
@@ -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 =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <trk>' +
@@ -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 =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
@@ -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 =
'<gpx xmlns="http://www.topografix.com/GPX/1/0">' +