Add feature id (fid) parsing

This commit is contained in:
Bart van den Eijnden
2014-02-25 16:28:10 +01:00
parent 98368a554d
commit 6256bf1989
3 changed files with 102 additions and 7 deletions

View File

@@ -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 = [];

View File

@@ -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.

View File

@@ -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);
});