From 6f3fa14c53f62353ddc57d5448b3b4de7615d476 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 29 Jul 2013 16:20:14 +0200 Subject: [PATCH] Sensible axisOrientation and srsName defaults --- src/objectliterals.jsdoc | 6 +++--- src/ol/parser/ogc/gml.js | 29 +++++++++++++------------- src/ol/parser/ogc/gml_v2.js | 17 ++++++--------- src/ol/parser/ogc/gml_v3.js | 17 ++++++++------- test/spec/ol/parser/ogc/gml_v2.test.js | 4 +++- test/spec/ol/parser/ogc/gml_v3.test.js | 28 ++++++++----------------- 6 files changed, 45 insertions(+), 56 deletions(-) diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index c6ff3e0bc3..4bdaf15193 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -357,9 +357,9 @@ /** * @typedef {Object} ol.parser.GMLOptions * @property {string|undefined} axisOrientation The axis orientation as - * specified in Proj4. If this is not provided, 'enu' will be used for GML2, - * and the projection's axis orientation for GML3, with a fallback to 'enu' - * if `projection` was not configured and is not provided by the data. + * specified in Proj4. If this is not provided, the projection's axis + * orientation will be used if `projection` is not overridden (i.e. the + * projection defined in the data is used). Otherwise the default is 'enu'. * @property {boolean|undefined} curve Write gml:Curve instead of * gml:LineString elements. This also affects the elements in multi-part * geometries. Default is `false“. This only applies to GML version 3. diff --git a/src/ol/parser/ogc/gml.js b/src/ol/parser/ogc/gml.js index 688802f3c7..a7015a7ee6 100644 --- a/src/ol/parser/ogc/gml.js +++ b/src/ol/parser/ogc/gml.js @@ -29,7 +29,7 @@ ol.parser.ogc.GML = function(opt_options) { var options = /** @type {ol.parser.GMLOptions} */ (goog.isDef(opt_options) ? opt_options : {}); this.axisOrientation = goog.isDef(options.axisOrientation) ? - options.axisOrientation : 'enu'; + options.axisOrientation : null; this.extractAttributes = goog.isDef(options.extractAttributes) ? options.extractAttributes : true; this.surface = goog.isDef(options.surface) ? @@ -47,14 +47,13 @@ ol.parser.ogc.GML = function(opt_options) { * @private * @type {string|undefined} */ - this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName; + this.srsName_; /** * @private * @type {string|undefined} */ - this.axisOrientation_ = goog.isNull(this.srsName) ? undefined : - ol.proj.get(this.srsName).getAxisOrientation(); + this.axisOrientation_; if (goog.isDef(options.schemaLocation)) { this.schemaLocation = options.schemaLocation; @@ -319,25 +318,24 @@ ol.parser.ogc.GML = function(opt_options) { } // TODO: Deal with GML documents that do not have the same SRS for all // geometries. + var srsName; if (!goog.isDef(this.srsName_)) { for (var i = node.childNodes.length - 1; i >= 0; --i) { var child = node.childNodes[i]; if (child.nodeType == 1) { - var srsName = child.getAttribute('srsName'); + srsName = child.getAttribute('srsName'); if (goog.isDef(srsName)) { - this.srsName_ = srsName.replace(ol.parser.ogc.GML.regExes.epsg, - 'EPSG:'); - if (!goog.isDef(this.axisOrientation_)) { - var projection = ol.proj.get(this.srsName_); - if (!goog.isNull(projection)) { - this.axisOrientation_ = projection.getAxisOrientation(); - } - } + this.srsName_ = srsName; } break; } } } + if (!goog.isDef(this.axisOrientation_)) { + if (goog.isDef(srsName)) { + this.axisOrientation_ = ol.proj.get(srsName).getAxisOrientation(); + } + } this.readChildNodes(node, obj); }, '_attribute': function(node, obj) { @@ -516,12 +514,13 @@ ol.parser.ogc.GML.prototype.read = function(data) { if (data && data.nodeType == 9) { data = data.documentElement; } + this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName; + this.axisOrientation_ = goog.isNull(this.axisOrientation) ? + undefined : this.axisOrientation; var obj = /** @type {ol.parser.ReadFeaturesResult} */ ({features: [], metadata: {}}); this.readNode(data, obj, true); obj.metadata.projection = this.srsName_; - this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName; - delete this.axisOrientation_; return obj; }; diff --git a/src/ol/parser/ogc/gml_v2.js b/src/ol/parser/ogc/gml_v2.js index 9d9fa91984..d704830209 100644 --- a/src/ol/parser/ogc/gml_v2.js +++ b/src/ol/parser/ogc/gml_v2.js @@ -45,7 +45,7 @@ ol.parser.ogc.GML_v2 = function(opt_options) { for (var i = 0; i < numCoordinates; ++i) { var coord = coordinates[i]; var part = goog.array.concat(coord); - if (this.axisOrientation.substr(0, 2) !== 'en') { + if (this.getAxisOrientation().substr(0, 2) !== 'en') { part[0] = coord[1]; part[1] = coord[0]; } @@ -101,8 +101,8 @@ ol.parser.ogc.GML_v2 = function(opt_options) { this.writeNode('coordinates', [[extent.minX, extent.minY], [extent.maxX, extent.maxY]], null, node); // srsName attribute is optional for gml:Box - if (goog.isDef(this.srsName)) { - node.setAttribute('srsName', this.srsName); + if (goog.isDef(this.getSrsName())) { + node.setAttribute('srsName', this.getSrsName()); } return node; } @@ -111,14 +111,6 @@ ol.parser.ogc.GML_v2 = function(opt_options) { goog.inherits(ol.parser.ogc.GML_v2, ol.parser.ogc.GML); -/** - * @return {string?} Axis orientation that was configured with this instance. - */ -ol.parser.ogc.GML_v2.prototype.getAxisOrientation = function() { - return this.axisOrientation; -}; - - /** * @param {Object} obj Object structure to write out as XML. * @return {string} An string representing the XML document. @@ -129,5 +121,8 @@ ol.parser.ogc.GML_v2.prototype.write = function(obj) { this.setAttributeNS( root, 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation', this.schemaLocation); + this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName; + this.axisOrientation_ = goog.isNull(this.axisOrientation) ? + undefined : this.axisOrientation; return this.serialize(root); }; diff --git a/src/ol/parser/ogc/gml_v3.js b/src/ol/parser/ogc/gml_v3.js index cf0fde9dc8..8d7ebf8aca 100644 --- a/src/ol/parser/ogc/gml_v3.js +++ b/src/ol/parser/ogc/gml_v3.js @@ -55,8 +55,8 @@ ol.parser.ogc.GML_v3 = function(opt_options) { } else if (type === ol.geom.GeometryType.GEOMETRYCOLLECTION) { child = this.writeNode('MultiGeometry', geometry, null, node); } - if (goog.isDef(this.srsName)) { - this.setAttributeNS(child, null, 'srsName', this.srsName); + if (goog.isDef(this.getSrsName())) { + this.setAttributeNS(child, null, 'srsName', this.getSrsName()); } return node; }; @@ -238,7 +238,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) { 'pos': function(point) { // only 2d for simple features profile var pos; - if (this.axisOrientation.substr(0, 2) === 'en') { + if (this.getAxisOrientation().substr(0, 2) === 'en') { pos = (point[0] + ' ' + point[1]); } else { pos = (point[1] + ' ' + point[0]); @@ -274,7 +274,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) { var point; for (var i = 0; i < len; ++i) { point = points[i]; - if (this.axisOrientation.substr(0, 2) === 'en') { + if (this.getAxisOrientation().substr(0, 2) === 'en') { parts[i] = point[0] + ' ' + point[1]; } else { parts[i] = point[1] + ' ' + point[0]; @@ -373,15 +373,15 @@ ol.parser.ogc.GML_v3 = function(opt_options) { this.writeNode('lowerCorner', bounds, null, node); this.writeNode('upperCorner', bounds, null, node); // srsName attribute is required for gml:Envelope - if (this.srsName) { - node.setAttribute('srsName', this.srsName); + if (this.getSrsName()) { + node.setAttribute('srsName', this.getSrsName()); } return node; }, 'lowerCorner': function(bounds) { // only 2d for simple features profile var pos; - if (this.axisOrientation.substr(0, 2) === 'en') { + if (this.getAxisOrientation().substr(0, 2) === 'en') { pos = (bounds.left + ' ' + bounds.bottom); } else { pos = (bounds.bottom + ' ' + bounds.left); @@ -416,5 +416,8 @@ ol.parser.ogc.GML_v3.prototype.write = function(obj) { this.setAttributeNS( root, 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation', this.schemaLocation); + this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName; + this.axisOrientation_ = goog.isNull(this.axisOrientation) ? + undefined : this.axisOrientation; return this.serialize(root); }; diff --git a/test/spec/ol/parser/ogc/gml_v2.test.js b/test/spec/ol/parser/ogc/gml_v2.test.js index 76257059d4..413e501fd1 100644 --- a/test/spec/ol/parser/ogc/gml_v2.test.js +++ b/test/spec/ol/parser/ogc/gml_v2.test.js @@ -243,6 +243,7 @@ describe('ol.parser.gml_v2', function() { 'http://www.opengis.net/wfs http://demo.opengeo.org/' + 'geoserver/schemas/wfs/1.0.0/WFS-basic.xsd'; var p = new ol.parser.ogc.GML_v2({ + axisOrientation: 'enu', featureType: 'states', featureNS: 'http://www.openplans.org/topp', schemaLocation: schemaLoc}); @@ -261,7 +262,8 @@ describe('ol.parser.gml_v2', function() { expect(attributes['SUB_REGION']).to.eql('E N Cen'); expect(attributes['STATE_ABBR']).to.eql('IL'); expect(attributes['LAND_KM']).to.eql('143986.61'); - expect(obj.metadata.projection).to.eql('EPSG:4326'); + expect(ol.proj.get(obj.metadata.projection) instanceof ol.proj.EPSG4326) + .to.be.ok(); done(); }); }); diff --git a/test/spec/ol/parser/ogc/gml_v3.test.js b/test/spec/ol/parser/ogc/gml_v3.test.js index 8ca9196fbc..3bd932d468 100644 --- a/test/spec/ol/parser/ogc/gml_v3.test.js +++ b/test/spec/ol/parser/ogc/gml_v3.test.js @@ -2,7 +2,8 @@ goog.provide('ol.test.parser.gml_v3'); describe('ol.parser.gml_v3', function() { - var parser = new ol.parser.ogc.GML_v3(); + var parser = new ol.parser.ogc.GML_v3( + {srsName: 'foo', axisOrientation: 'enu'}); describe('Test GML v3 parser', function() { it('Envelope read correctly', function(done) { @@ -18,10 +19,8 @@ describe('ol.parser.gml_v3', function() { afterLoadXml(url, function(xml) { var obj = parser.read(xml); var geom = parser.createGeometry_({geometry: obj.geometry}); - parser.srsName = 'foo'; var node = parser.featureNSWiters_['_geometry'].apply(parser, [geom]).firstChild; - parser.srsName = null; expect(goog.dom.xml.loadXml(parser.serialize(node))).to.xmleql(xml); expect(obj.geometry.type).to.eql('linearring'); expect(obj.geometry.coordinates).to.eql([[1, 2], [3, 4], [5, 6], @@ -34,10 +33,8 @@ describe('ol.parser.gml_v3', function() { afterLoadXml(url, function(xml) { var obj = parser.read(xml); var geom = parser.createGeometry_({geometry: obj.geometry}); - parser.srsName = 'foo'; var node = parser.featureNSWiters_['_geometry'].apply(parser, [geom]).firstChild; - parser.srsName = null; expect(goog.dom.xml.loadXml(parser.serialize(node))).to.xmleql(xml); expect(obj.geometry.type).to.eql('linestring'); expect(obj.geometry.coordinates).to.eql([[1, 2], [3, 4]]); @@ -98,12 +95,10 @@ describe('ol.parser.gml_v3', function() { var url = 'spec/ol/parser/ogc/xml/gml_v3/multicurve-singular.xml'; afterLoadXml(url, function(xml) { var obj = parser.read(xml); - parser.srsName = 'foo'; var geom = parser.createGeometry_({geometry: obj.geometry}); var node = parser.featureNSWiters_['_geometry'].apply(parser, [geom]).firstChild; expect(goog.dom.xml.loadXml(parser.serialize(node))).to.xmleql(xml); - parser.srsName = null; expect(obj.geometry.type).to.eql('multilinestring'); expect(obj.geometry.parts.length).to.eql(2); expect(obj.geometry.parts[0].type).to.eql('linestring'); @@ -142,12 +137,10 @@ describe('ol.parser.gml_v3', function() { var url = 'spec/ol/parser/ogc/xml/gml_v3/multipoint-singular.xml'; afterLoadXml(url, function(xml) { var obj = parser.read(xml); - parser.srsName = 'foo'; var geom = parser.createGeometry_({geometry: obj.geometry}); var node = parser.featureNSWiters_['_geometry'].apply(parser, [geom]).firstChild; expect(goog.dom.xml.loadXml(parser.serialize(node))).to.xmleql(xml); - parser.srsName = null; expect(obj.geometry.type).to.eql('multipoint'); expect(obj.geometry.parts.length).to.eql(3); expect(obj.geometry.parts[0].type).to.eql('point'); @@ -194,12 +187,10 @@ describe('ol.parser.gml_v3', function() { var url = 'spec/ol/parser/ogc/xml/gml_v3/multisurface-singular.xml'; afterLoadXml(url, function(xml) { var obj = parser.read(xml); - parser.srsName = 'foo'; var geom = parser.createGeometry_({geometry: obj.geometry}); var node = parser.featureNSWiters_['_geometry'].apply(parser, [geom]).firstChild; expect(goog.dom.xml.loadXml(parser.serialize(node))).to.xmleql(xml); - parser.srsName = null; expect(obj.geometry.type).to.eql('multipolygon'); expect(obj.geometry.parts.length).to.eql(2); expect(obj.geometry.parts[0].type).to.eql('polygon'); @@ -226,10 +217,8 @@ describe('ol.parser.gml_v3', function() { afterLoadXml(url, function(xml) { var obj = parser.read(xml); var geom = parser.createGeometry_({geometry: obj.geometry}); - parser.srsName = 'foo'; var node = parser.featureNSWiters_['_geometry'].apply(parser, [geom]).firstChild; - parser.srsName = null; expect(goog.dom.xml.loadXml(parser.serialize(node))).to.xmleql(xml); expect(obj.geometry.type).to.eql('point'); expect(obj.geometry.coordinates).to.eql([1, 2]); @@ -241,10 +230,8 @@ describe('ol.parser.gml_v3', function() { afterLoadXml(url, function(xml) { var obj = parser.read(xml); var geom = parser.createGeometry_({geometry: obj.geometry}); - parser.srsName = 'foo'; var node = parser.featureNSWiters_['_geometry'].apply(parser, [geom]).firstChild; - parser.srsName = null; expect(goog.dom.xml.loadXml(parser.serialize(node))).to.xmleql(xml); expect(obj.geometry.type).to.eql('polygon'); done(); @@ -272,9 +259,8 @@ describe('ol.parser.gml_v3', function() { 'http://www.opengis.net/gml ' + 'http://schemas.opengis.net/gml/3.2.1/gml.xsd'; var p = new ol.parser.ogc.GML_v3({schemaLocation: schemaLoc}); + debugger var obj = p.read(xml); - p.srsName = 'urn:x-ogc:def:crs:EPSG:4326'; - p.axisOrientation = 'neu'; var output = p.write(obj); expect(goog.dom.xml.loadXml(output)).to.xmleql(xml); expect(p.geometryName).to.eql('the_geom'); @@ -289,13 +275,15 @@ describe('ol.parser.gml_v3', function() { expect(attributes['SUB_REGION']).to.eql('E N Cen'); expect(attributes['STATE_ABBR']).to.eql('IL'); expect(attributes['LAND_KM']).to.eql('143986.61'); - expect(obj.metadata.projection).to.eql('EPSG:4326'); + expect(ol.proj.get(obj.metadata.projection) instanceof ol.proj.EPSG4326) + .to.be.ok(); done(); }); }); it('FeatureCollection from WFS read correctly', function(done) { var url = 'spec/ol/parser/ogc/xml/gml_v3/topp-states-wfs.xml'; afterLoadXml(url, function(xml) { + parser.srsName = null; var obj = parser.read(xml); expect(obj.features.length).to.eql(3); var feature = obj.features[0]; @@ -308,7 +296,9 @@ describe('ol.parser.gml_v3', function() { expect(attributes['SUB_REGION']).to.eql('E N Cen'); expect(attributes['STATE_ABBR']).to.eql('IL'); expect(attributes['LAND_KM']).to.eql('143986.61'); - expect(obj.metadata.projection).to.eql('EPSG:4326'); + expect(ol.proj.get(obj.metadata.projection) instanceof ol.proj.EPSG4326) + .to.be.ok(); + parser.srsName = "foo"; done(); }); });