Add trk serialization for ol.format.GPX
This commit is contained in:
@@ -514,7 +514,44 @@ ol.format.GPX.writeRte_ = function(node, feature, objectStack) {
|
|||||||
* @param {ol.Feature} feature Feature.
|
* @param {ol.Feature} feature Feature.
|
||||||
* @param {Array.<*>} objectStack Object stack.
|
* @param {Array.<*>} objectStack Object stack.
|
||||||
* @private
|
* @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) {
|
ol.format.GPX.writeWpt_ = function(node, feature, objectStack) {
|
||||||
var context = objectStack[objectStack.length - 1];
|
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
|
* @const
|
||||||
* @type {Object.<string, Array.<string>>}
|
* @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.GPX_SERIALIZERS_ = ol.xml.makeStructureNS(
|
||||||
ol.format.GPX.NAMESPACE_URIS_, {
|
ol.format.GPX.NAMESPACE_URIS_, {
|
||||||
'rte': ol.xml.makeChildAppender(ol.format.GPX.writeRte_),
|
'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_)
|
'wpt': ol.xml.makeChildAppender(ol.format.GPX.writeWpt_)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,17 +5,13 @@ describe('ol.format.GPX', function() {
|
|||||||
|
|
||||||
var format;
|
var format;
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
format = new ol.format.GPX();
|
format = new ol.format.GPX.V1_1();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('readFeatures', function() {
|
describe('readFeatures', function() {
|
||||||
|
|
||||||
describe('rte', function() {
|
describe('rte', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
|
||||||
format = new ol.format.GPX.V1_1();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can read an empty rte', function() {
|
it('can read an empty rte', function() {
|
||||||
var text =
|
var text =
|
||||||
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
'<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);
|
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 =
|
var text =
|
||||||
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
||||||
' <trk>' +
|
' <trk>' +
|
||||||
@@ -132,9 +128,11 @@ describe('ol.format.GPX', function() {
|
|||||||
expect(f.get('linkType')).to.be('Link type');
|
expect(f.get('linkType')).to.be('Link type');
|
||||||
expect(f.get('number')).to.be(1);
|
expect(f.get('number')).to.be(1);
|
||||||
expect(f.get('type')).to.be('Type');
|
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 =
|
var text =
|
||||||
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
||||||
' <trk>' +
|
' <trk>' +
|
||||||
@@ -149,9 +147,11 @@ describe('ol.format.GPX', function() {
|
|||||||
expect(g).to.be.an(ol.geom.MultiLineString);
|
expect(g).to.be.an(ol.geom.MultiLineString);
|
||||||
expect(g.getCoordinates()).to.eql([[]]);
|
expect(g.getCoordinates()).to.eql([[]]);
|
||||||
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
|
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 =
|
var text =
|
||||||
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
||||||
' <trk>' +
|
' <trk>' +
|
||||||
@@ -177,9 +177,11 @@ describe('ol.format.GPX', function() {
|
|||||||
[[2, 1, 3, 1263115752], [6, 5, 7, 1263115812]]
|
[[2, 1, 3, 1263115752], [6, 5, 7, 1263115812]]
|
||||||
]);
|
]);
|
||||||
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
|
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 =
|
var text =
|
||||||
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
||||||
' <trk>' +
|
' <trk>' +
|
||||||
@@ -216,16 +218,14 @@ describe('ol.format.GPX', function() {
|
|||||||
[[9, 8, 10, 1263115872], [12, 11, 13, 1263115932]]
|
[[9, 8, 10, 1263115872], [12, 11, 13, 1263115932]]
|
||||||
]);
|
]);
|
||||||
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
|
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() {
|
describe('wpt', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
|
||||||
format = new ol.format.GPX.V1_1();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can read and write a wpt', function() {
|
it('can read and write a wpt', function() {
|
||||||
var text =
|
var text =
|
||||||
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
|
||||||
@@ -355,6 +355,10 @@ describe('ol.format.GPX', function() {
|
|||||||
|
|
||||||
describe('XML namespace support', function() {
|
describe('XML namespace support', function() {
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
format = new ol.format.GPX();
|
||||||
|
});
|
||||||
|
|
||||||
it('can read features with a version 1.0 namespace', function() {
|
it('can read features with a version 1.0 namespace', function() {
|
||||||
var text =
|
var text =
|
||||||
'<gpx xmlns="http://www.topografix.com/GPX/1/0">' +
|
'<gpx xmlns="http://www.topografix.com/GPX/1/0">' +
|
||||||
|
|||||||
Reference in New Issue
Block a user