From 6256bf1989afba3fb67410502e4a122a19984497 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Tue, 25 Feb 2014 16:28:10 +0100 Subject: [PATCH] Add feature id (fid) parsing --- src/ol/format/gmlformat.js | 15 ++--- src/ol/xml.js | 89 +++++++++++++++++++++++++++ test/spec/ol/format/gmlformat.test.js | 5 ++ 3 files changed, 102 insertions(+), 7 deletions(-) diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index 3767012bde..d3381742f3 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -123,6 +123,8 @@ ol.format.GML.readGeometry_ = function(node, objectStack) { */ ol.format.GML.readFeature_ = function(node, objectStack) { var n; + var fid = node.getAttribute('fid') || + ol.xml.getAttributeNS(node, 'http://www.opengis.net/gml', 'id'); var values = {}, geometryName; for (n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) { @@ -145,6 +147,9 @@ ol.format.GML.readFeature_ = function(node, objectStack) { if (goog.isDef(geometryName)) { feature.setGeometryName(geometryName); } + if (!goog.isNull(fid)) { + feature.setId(fid); + } return feature; }; @@ -165,8 +170,6 @@ ol.format.GML.readPoint_ = function(node, objectStack) { goog.asserts.assert(flatCoordinates.length == 3); point.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates); return point; - } else { - return undefined; } }; @@ -185,8 +188,6 @@ ol.format.GML.readMultiPoint_ = function(node, objectStack) { ol.format.GML.MULTIPOINT_PARSERS_, node, objectStack); if (goog.isDefAndNotNull(coordinates)) { return new ol.geom.MultiPoint(coordinates); - } else { - return undefined; } }; @@ -652,7 +653,7 @@ ol.format.GML.readFlatPos_ = function(node, objectStack) { goog.asserts.assert(goog.isObject(context)); var containerSrs = goog.object.get(context, 'srsName'); var axisOrientation = 'enu'; - if (containerSrs !== null) { + if (!goog.isNull(containerSrs)) { var proj = ol.proj.get(containerSrs); axisOrientation = proj.getAxisOrientation(); } @@ -683,7 +684,7 @@ ol.format.GML.readFlatPosList_ = function(node, objectStack) { var containerSrs = goog.object.get(context, 'srsName'); var containerDimension = node.parentNode.getAttribute('srsDimension'); var axisOrientation = 'enu'; - if (containerSrs !== null) { + if (!goog.isNull(containerSrs)) { var proj = ol.proj.get(containerSrs); axisOrientation = proj.getAxisOrientation(); } @@ -691,7 +692,7 @@ ol.format.GML.readFlatPosList_ = function(node, objectStack) { // The "dimension" attribute is from the GML 3.0.1 spec. var dim = parseInt(node.getAttribute('srsDimension') || node.getAttribute('dimension'), 10) || - (containerDimension !== null) ? + (!goog.isNull(containerDimension)) ? parseInt(containerDimension, 10) : 2; var x, y, z; var flatCoordinates = []; diff --git a/src/ol/xml.js b/src/ol/xml.js index a0d9dde9c5..89d0042ed3 100644 --- a/src/ol/xml.js +++ b/src/ol/xml.js @@ -201,6 +201,95 @@ ol.xml.isNodeIE_ = function(value) { ol.xml.isNode = goog.userAgent.IE ? ol.xml.isNodeIE_ : ol.xml.isNode_; +/** + * @param {Node} node Node. + * @param {?string} namespaceURI Namespace URI. + * @param {string} name Attribute name. + * @return {string} Value + * @private + */ +ol.xml.getAttributeNS_ = function(node, namespaceURI, name) { + return node.getAttributeNS(namespaceURI, name) || ''; +}; + + +/** + * @param {Node} node Node. + * @param {?string} namespaceURI Namespace URI. + * @param {string} name Attribute name. + * @return {string} Value + * @private + */ +ol.xml.getAttributeNSActiveX_ = function(node, namespaceURI, name) { + var attributeValue = ''; + var attributeNode = ol.xml.getAttributeNodeNS(node, namespaceURI, name); + if (goog.isDef(attributeNode)) { + attributeValue = attributeNode.nodeValue; + } + return attributeValue; +}; + + +/** + * @param {Node} node Node. + * @param {?string} namespaceURI Namespace URI. + * @param {string} name Attribute name. + * @return {string} Value + */ +ol.xml.getAttributeNS = + (document.implementation && document.implementation.createDocument) ? + ol.xml.getAttributeNS_ : ol.xml.getAttributeNSActiveX_; + + +/** + * @param {Node} node Node. + * @param {?string} namespaceURI Namespace URI. + * @param {string} name Attribute name. + * @return {?Node} Attribute node or null if none found. + * @private + */ +ol.xml.getAttributeNodeNS_ = function(node, namespaceURI, name) { + return node.getAttributeNodeNS(namespaceURI, name); +}; + + +/** + * @param {Node} node Node. + * @param {?string} namespaceURI Namespace URI. + * @param {string} name Attribute name. + * @return {?Node} Attribute node or null if none found. + * @private + */ +ol.xml.getAttributeNodeNSActiveX_ = function(node, namespaceURI, name) { + var attributeNode = null; + var attributes = node.attributes; + var potentialNode, fullName; + for (var i = 0, len = attributes.length; i < len; ++i) { + potentialNode = attributes[i]; + if (potentialNode.namespaceURI == namespaceURI) { + fullName = (potentialNode.prefix) ? + (potentialNode.prefix + ':' + name) : name; + if (fullName == potentialNode.nodeName) { + attributeNode = potentialNode; + break; + } + } + } + return attributeNode; +}; + + +/** + * @param {Node} node Node. + * @param {?string} namespaceURI Namespace URI. + * @param {string} name Attribute name. + * @return {?Node} Attribute node or null if none found. + */ +ol.xml.getAttributeNodeNS = + (document.implementation && document.implementation.createDocument) ? + ol.xml.getAttributeNodeNS_ : ol.xml.getAttributeNodeNSActiveX_; + + /** * @param {Node} node Node. * @param {?string} namespaceURI Namespace URI. diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index 044a2df508..6f952fe3ed 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -652,6 +652,10 @@ describe('ol.format.GML', function() { expect(features).to.have.length(10); }); + it('creates the right id for the feature', function() { + expect(features[0].getId()).to.equal('states.1'); + }); + }); describe('when parsing TOPP states GML from WFS', function() { @@ -678,6 +682,7 @@ describe('ol.format.GML', function() { 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); });