Implement read transforms for ol.format.OSMXML

This commit is contained in:
tsauerwein
2014-08-08 17:25:05 +02:00
parent b05f880f7c
commit bfb6c2e3b8
2 changed files with 109 additions and 1 deletions

View File

@@ -189,6 +189,7 @@ ol.format.OSMXML.NODE_PARSERS_ = ol.xml.makeParsersNS(
* *
* @function * @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source. * @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Features. * @return {Array.<ol.Feature>} Features.
* @api * @api
*/ */
@@ -198,7 +199,7 @@ ol.format.OSMXML.prototype.readFeatures;
/** /**
* @inheritDoc * @inheritDoc
*/ */
ol.format.OSMXML.prototype.readFeaturesFromNode = function(node) { ol.format.OSMXML.prototype.readFeaturesFromNode = function(node, opt_options) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
if (node.localName == 'osm') { if (node.localName == 'osm') {
var state = ol.xml.pushParseAndPop({ var state = ol.xml.pushParseAndPop({
@@ -206,6 +207,8 @@ ol.format.OSMXML.prototype.readFeaturesFromNode = function(node) {
features: [] features: []
}, ol.format.OSMXML.PARSERS_, node, []); }, ol.format.OSMXML.PARSERS_, node, []);
if (goog.isDef(state.features)) { if (goog.isDef(state.features)) {
ol.format.XMLFeature.transformFeaturesWithOptions(
state.features, false, this.getReadOptions(node, opt_options));
return state.features; return state.features;
} }
} }

View File

@@ -0,0 +1,105 @@
goog.provide('ol.test.format.OSMXML');
describe('ol.format.OSMXML', function() {
var format;
beforeEach(function() {
format = new ol.format.OSMXML();
});
describe('#readFeatures', function() {
it('can read an empty document', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(0);
});
it('can read nodes', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(2);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([2, 1]);
});
it('can read nodes and ways', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
' <way id="3">' +
' <tag k="name" v="3"/>' +
' <nd ref="1" />' +
' <nd ref="2" />' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(3);
var point = fs[0];
expect(point).to.be.an(ol.Feature);
var g = point.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([2, 1]);
var line = fs[2];
expect(line).to.be.an(ol.Feature);
g = line.getGeometry();
expect(g).to.be.an(ol.geom.LineString);
expect(g.getCoordinates()).to.eql([[2, 1], [4, 3]]);
});
it('can transform and read nodes', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(2);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql(
ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857'));
});
});
});
goog.require('goog.dom.xml');
goog.require('ol.Feature');
goog.require('ol.format.OSMXML');
goog.require('ol.geom.Point');
goog.require('ol.geom.LineString');
goog.require('ol.proj');