From f92820925689faf55c31d61001c38b235769117e Mon Sep 17 00:00:00 2001 From: Joao Gouveia Date: Tue, 15 Aug 2017 09:10:17 +0200 Subject: [PATCH 1/2] Fix srsDimension read on GML3 for Geoserver Contrary to Mapserver, Geoserver sets the srsDimension attribute on the child of the geometry node, not on the points list. This fix searches that node as well. A small unit test was also added. --- src/ol/format/gml3.js | 9 ++- src/ol/format/gmlbase.js | 1 + test/spec/ol/format/gml.test.js | 31 ++++++++ test/spec/ol/format/gml/geoserverFeatures.xml | 72 +++++++++++++++++++ 4 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 test/spec/ol/format/gml/geoserverFeatures.xml diff --git a/src/ol/format/gml3.js b/src/ol/format/gml3.js index d2041ed356..4ada7507c0 100644 --- a/src/ol/format/gml3.js +++ b/src/ol/format/gml3.js @@ -345,7 +345,7 @@ ol.format.GML3.prototype.readFlatPosList_ = function(node, objectStack) { var s = ol.xml.getAllTextContent(node, false).replace(/^\s*|\s*$/g, ''); var context = objectStack[0]; var containerSrs = context['srsName']; - var containerDimension = node.parentNode.getAttribute('srsDimension'); + var contextDimension = context['srsDimension']; var axisOrientation = 'enu'; if (containerSrs) { var proj = ol.proj.get(containerSrs); @@ -360,8 +360,11 @@ ol.format.GML3.prototype.readFlatPosList_ = function(node, objectStack) { } else if (node.getAttribute('dimension')) { dim = ol.format.XSD.readNonNegativeIntegerString( node.getAttribute('dimension')); - } else if (containerDimension) { - dim = ol.format.XSD.readNonNegativeIntegerString(containerDimension); + } else if (node.parentNode.getAttribute('srsDimension')) { + dim = ol.format.XSD.readNonNegativeIntegerString( + node.parentNode.getAttribute('srsDimension')); + } else if (contextDimension) { + dim = ol.format.XSD.readNonNegativeIntegerString(contextDimension); } var x, y, z; var flatCoordinates = []; diff --git a/src/ol/format/gmlbase.js b/src/ol/format/gmlbase.js index 1371ffa453..2d68b65aa7 100644 --- a/src/ol/format/gmlbase.js +++ b/src/ol/format/gmlbase.js @@ -198,6 +198,7 @@ ol.format.GMLBase.prototype.readFeaturesInternal = function(node, objectStack) { ol.format.GMLBase.prototype.readGeometryElement = function(node, objectStack) { var context = /** @type {Object} */ (objectStack[0]); context['srsName'] = node.firstElementChild.getAttribute('srsName'); + context['srsDimension'] = node.firstElementChild.getAttribute('srsDimension'); /** @type {ol.geom.Geometry} */ var geometry = ol.xml.pushParseAndPop(null, this.GEOMETRY_PARSERS_, node, objectStack, this); diff --git a/test/spec/ol/format/gml.test.js b/test/spec/ol/format/gml.test.js index 2764eab7ab..d9225ede22 100644 --- a/test/spec/ol/format/gml.test.js +++ b/test/spec/ol/format/gml.test.js @@ -1466,4 +1466,35 @@ describe('ol.format.GML3', function() { }); + describe('when parsing srsDimension from WFS (Geoserver)', function() { + + var features, feature; + before(function(done) { + afterLoadText('spec/ol/format/gml/geoserverFeatures.xml', function(xml) { + try { + var config = { + 'featureNS': 'http://www.openplans.org/topp', + 'featureType': 'states' + }; + features = new ol.format.GML(config).readFeatures(xml); + } catch (e) { + done(e); + } + done(); + }); + }); + + it('creates 3 features', function() { + expect(features).to.have.length(3); + }); + + it('creates a polygon for Illinois', function() { + feature = features[0]; + expect(feature.getId()).to.equal('states.1'); + expect(feature.get('STATE_NAME')).to.equal('Illinois'); + expect(feature.getGeometry()).to.be.an(ol.geom.MultiPolygon); + }); + + }); + }); diff --git a/test/spec/ol/format/gml/geoserverFeatures.xml b/test/spec/ol/format/gml/geoserverFeatures.xml new file mode 100644 index 0000000000..c390eb8e49 --- /dev/null +++ b/test/spec/ol/format/gml/geoserverFeatures.xml @@ -0,0 +1,72 @@ + + + + + + 51.91122415 4.46386854 46.04679351 51.91120839 4.46382399 46.04679382 51.91122877 4.46383897 46.04679306 51.91123013 4.46385411 46.04679315 51.91123016 4.46386244 46.04679328 + + + + + + + + + 51.91119276 4.46385491 46.06074531 51.91118582 4.4638264 46.06074609 51.91121772 4.46380612 46.06074168 51.91122465 4.46383463 46.06074089 51.91119276 4.46385491 46.06074531 + + + + + + + + + + + 51.91123321 4.46384921 46.04679306 51.91123242 4.46381475 46.0467932 51.91122926 4.46381494 46.04679323 51.91123004 4.4638494 46.04679309 51.91123321 4.46384921 46.04679306 + + + + + + + + + + + 51.91123372 4.46388232 46.04679377 51.91122788 4.46388684 46.04679399 51.91123214 4.46390122 46.04679455 51.91123798 4.4638967 46.04679433 51.91123372 4.46388232 46.04679377 + + + + + + + + + 51.91117684 4.46386436 46.06565659 51.91115683 4.46386502 46.0656567 + + + + + + + 51.91117878 4.46385015 46.06636351 51.91115877 4.46385081 46.06636361 + + + + + + + 51.91121687 4.46390244 46.08019498 51.91119686 4.4639031 46.08019509 + + + + + + + 51.91125849 4.46383715 46.04679348 + + + + + From 163fc2e8e65ee8bc384ca768558f5f3e6c902b70 Mon Sep 17 00:00:00 2001 From: Joao Gouveia Date: Wed, 16 Aug 2017 12:13:18 +0200 Subject: [PATCH 2/2] Adds Unit testing --- test/spec/ol/format/gml.test.js | 72 ++++++++++++++----- .../ol/format/gml/geoserver3DFeatures.xml | 33 +++++++++ test/spec/ol/format/gml/geoserverFeatures.xml | 72 ------------------- 3 files changed, 89 insertions(+), 88 deletions(-) create mode 100644 test/spec/ol/format/gml/geoserver3DFeatures.xml delete mode 100644 test/spec/ol/format/gml/geoserverFeatures.xml diff --git a/test/spec/ol/format/gml.test.js b/test/spec/ol/format/gml.test.js index d9225ede22..df7621abcb 100644 --- a/test/spec/ol/format/gml.test.js +++ b/test/spec/ol/format/gml.test.js @@ -1470,29 +1470,69 @@ describe('ol.format.GML3', function() { var features, feature; before(function(done) { - afterLoadText('spec/ol/format/gml/geoserverFeatures.xml', function(xml) { - try { - var config = { - 'featureNS': 'http://www.openplans.org/topp', - 'featureType': 'states' - }; - features = new ol.format.GML(config).readFeatures(xml); - } catch (e) { - done(e); - } - done(); - }); + afterLoadText('spec/ol/format/gml/geoserver3DFeatures.xml', + function(xml) { + try { + var config = { + 'featureNS': 'http://www.opengeospatial.net/cite', + 'featureType': 'geoserver_layer' + }; + features = new ol.format.GML(config).readFeatures(xml); + } catch (e) { + done(e); + } + done(); + }); }); it('creates 3 features', function() { expect(features).to.have.length(3); }); - it('creates a polygon for Illinois', function() { + it('creates a LineString', function() { feature = features[0]; - expect(feature.getId()).to.equal('states.1'); - expect(feature.get('STATE_NAME')).to.equal('Illinois'); - expect(feature.getGeometry()).to.be.an(ol.geom.MultiPolygon); + expect(feature.getId()).to.equal('geoserver_layer.1'); + expect(feature.getGeometry()).to.be.an(ol.geom.LineString); + }); + + it('creates a Polygon', function() { + feature = features[1]; + expect(feature.getId()).to.equal('geoserver_layer.2'); + expect(feature.getGeometry()).to.be.an(ol.geom.Polygon); + }); + + it('creates a Point', function() { + feature = features[2]; + expect(feature.getId()).to.equal('geoserver_layer.3'); + expect(feature.getGeometry()).to.be.an(ol.geom.Point); + }); + + + it('creates 3D Features with the expected geometries', function() { + var expectedGeometry1 = [ + 4.46386854, 51.91122415, 46.04679351, + 4.46382399, 51.91120839, 46.04679382 + ]; + var expectedGeometry2 = [ + 4.46385491, 51.91119276, 46.06074531, + 4.4638264, 51.91118582, 46.06074609, + 4.46380612, 51.91121772, 46.06074168, + 4.46383463, 51.91122465, 46.06074089, + 4.46385491, 51.91119276, 46.06074531 + ]; + var expectedGeometry3 = [ + 4.46383715, 51.91125849, 46.04679348 + ]; + + feature = features[0]; + expect(feature.getGeometry().getFlatCoordinates()) + .to.eql(expectedGeometry1); + feature = features[1]; + expect(feature.getGeometry().getFlatCoordinates()) + .to.eql(expectedGeometry2); + feature = features[2]; + expect(feature.getGeometry().getFlatCoordinates()) + .to.eql(expectedGeometry3); }); }); diff --git a/test/spec/ol/format/gml/geoserver3DFeatures.xml b/test/spec/ol/format/gml/geoserver3DFeatures.xml new file mode 100644 index 0000000000..0848e5c27d --- /dev/null +++ b/test/spec/ol/format/gml/geoserver3DFeatures.xml @@ -0,0 +1,33 @@ + + + + + + + 51.91122415 4.46386854 46.04679351 51.91120839 4.46382399 46.04679382 + + + + + + + + + + + 51.91119276 4.46385491 46.06074531 51.91118582 4.4638264 46.06074609 51.91121772 4.46380612 46.06074168 51.91122465 4.46383463 46.06074089 51.91119276 4.46385491 46.06074531 + + + + + + + + + + 51.91125849 4.46383715 46.04679348 + + + + + diff --git a/test/spec/ol/format/gml/geoserverFeatures.xml b/test/spec/ol/format/gml/geoserverFeatures.xml deleted file mode 100644 index c390eb8e49..0000000000 --- a/test/spec/ol/format/gml/geoserverFeatures.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - - - - 51.91122415 4.46386854 46.04679351 51.91120839 4.46382399 46.04679382 51.91122877 4.46383897 46.04679306 51.91123013 4.46385411 46.04679315 51.91123016 4.46386244 46.04679328 - - - - - - - - - 51.91119276 4.46385491 46.06074531 51.91118582 4.4638264 46.06074609 51.91121772 4.46380612 46.06074168 51.91122465 4.46383463 46.06074089 51.91119276 4.46385491 46.06074531 - - - - - - - - - - - 51.91123321 4.46384921 46.04679306 51.91123242 4.46381475 46.0467932 51.91122926 4.46381494 46.04679323 51.91123004 4.4638494 46.04679309 51.91123321 4.46384921 46.04679306 - - - - - - - - - - - 51.91123372 4.46388232 46.04679377 51.91122788 4.46388684 46.04679399 51.91123214 4.46390122 46.04679455 51.91123798 4.4638967 46.04679433 51.91123372 4.46388232 46.04679377 - - - - - - - - - 51.91117684 4.46386436 46.06565659 51.91115683 4.46386502 46.0656567 - - - - - - - 51.91117878 4.46385015 46.06636351 51.91115877 4.46385081 46.06636361 - - - - - - - 51.91121687 4.46390244 46.08019498 51.91119686 4.4639031 46.08019509 - - - - - - - 51.91125849 4.46383715 46.04679348 - - - - -