Merge pull request #2746 from fgravin/gmlVersionFormat

Add versioning for GML format
This commit is contained in:
Antoine Abt
2014-10-27 15:44:06 +01:00
9 changed files with 1326 additions and 728 deletions

View File

@@ -1740,6 +1740,12 @@ olx.format.WFSWriteGetFeatureOptions.prototype.maxFeatures;
*/
olx.format.WFSWriteGetFeatureOptions.prototype.geometryName;
/**
* GML format to use within the WFS format.
* @type {ol.format.GMLBase|undefined}
* @api
*/
olx.format.WFSWriteGetFeatureOptions.prototype.gmlFormat;
/**
* Extent to use for the BBOX filter.

218
src/ol/format/gml/gml2.js Normal file
View File

@@ -0,0 +1,218 @@
goog.provide('ol.format.GML2');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.NodeType');
goog.require('goog.object');
goog.require('ol.extent');
goog.require('ol.format.GML');
goog.require('ol.format.GMLBase');
goog.require('ol.format.XSD');
goog.require('ol.proj');
goog.require('ol.xml');
/**
* @classdesc
* Feature format for reading and writing data in the GML format,
* version 2.1.2.
*
* @constructor
* @param {olx.format.GMLOptions=} opt_options Optional configuration object.
* @extends {ol.format.GMLBase}
* @api
*/
ol.format.GML2 = function(opt_options) {
var options = /** @type {olx.format.GMLOptions} */
(goog.isDef(opt_options) ? opt_options : {});
goog.base(this, options);
/**
* @inheritDoc
*/
this.schemaLocation = goog.isDef(options.schemaLocation) ?
options.schemaLocation : ol.format.GML2.schemaLocation_;
};
goog.inherits(ol.format.GML2, ol.format.GMLBase);
/**
* @const
* @type {string}
* @private
*/
ol.format.GML2.schemaLocation_ = 'http://www.opengis.net/gml ' +
'http://schemas.opengis.net/gml/2.1.2/feature.xsd';
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
* @return {Array.<number>|undefined} Flat coordinates.
*/
ol.format.GML2.prototype.readFlatCoordinates_ = function(node, objectStack) {
var s = ol.xml.getAllTextContent(node, false).replace(/^\s*|\s*$/g, '');
var context = objectStack[0];
goog.asserts.assert(goog.isObject(context));
var containerSrs = goog.object.get(context, 'srsName');
var containerDimension = node.parentNode.getAttribute('srsDimension');
var axisOrientation = 'enu';
if (!goog.isNull(containerSrs)) {
var proj = ol.proj.get(containerSrs);
axisOrientation = proj.getAxisOrientation();
}
var coords = s.split(/[\s,]+/);
// The "dimension" attribute is from the GML 3.0.1 spec.
var dim = 2;
if (!goog.isNull(node.getAttribute('srsDimension'))) {
dim = ol.format.XSD.readNonNegativeIntegerString(
node.getAttribute('srsDimension'));
} else if (!goog.isNull(node.getAttribute('dimension'))) {
dim = ol.format.XSD.readNonNegativeIntegerString(
node.getAttribute('dimension'));
} else if (!goog.isNull(containerDimension)) {
dim = ol.format.XSD.readNonNegativeIntegerString(containerDimension);
}
var x, y, z;
var flatCoordinates = [];
for (var i = 0, ii = coords.length; i < ii; i += dim) {
x = parseFloat(coords[i]);
y = parseFloat(coords[i + 1]);
z = (dim === 3) ? parseFloat(coords[i + 2]) : 0;
if (axisOrientation.substr(0, 2) === 'en') {
flatCoordinates.push(x, y, z);
} else {
flatCoordinates.push(y, x, z);
}
}
return flatCoordinates;
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
* @return {ol.Extent|undefined} Envelope.
*/
ol.format.GML2.prototype.readBox_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'Box');
var flatCoordinates = ol.xml.pushParseAndPop(
/** @type {Array.<number>} */ ([null]),
this.BOX_PARSERS_, node, objectStack, this);
return ol.extent.createOrUpdate(flatCoordinates[1][0],
flatCoordinates[1][1], flatCoordinates[1][3],
flatCoordinates[1][4]);
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
*/
ol.format.GML2.prototype.innerBoundaryIsParser_ =
function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'innerBoundaryIs');
var flatLinearRing = ol.xml.pushParseAndPop(
/** @type {Array.<number>|undefined} */ (undefined),
this.RING_PARSERS, node, objectStack, this);
if (goog.isDef(flatLinearRing)) {
var flatLinearRings = /** @type {Array.<Array.<number>>} */
(objectStack[objectStack.length - 1]);
goog.asserts.assert(goog.isArray(flatLinearRings));
goog.asserts.assert(flatLinearRings.length > 0);
flatLinearRings.push(flatLinearRing);
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
*/
ol.format.GML2.prototype.outerBoundaryIsParser_ =
function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'outerBoundaryIs');
var flatLinearRing = ol.xml.pushParseAndPop(
/** @type {Array.<number>|undefined} */ (undefined),
this.RING_PARSERS, node, objectStack, this);
if (goog.isDef(flatLinearRing)) {
var flatLinearRings = /** @type {Array.<Array.<number>>} */
(objectStack[objectStack.length - 1]);
goog.asserts.assert(goog.isArray(flatLinearRings));
goog.asserts.assert(flatLinearRings.length > 0);
flatLinearRings[0] = flatLinearRing;
}
};
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GML2.prototype.GEOMETRY_FLAT_COORDINATES_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'coordinates': ol.xml.makeReplacer(
ol.format.GML2.prototype.readFlatCoordinates_)
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GML2.prototype.FLAT_LINEAR_RINGS_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'innerBoundaryIs': ol.format.GML2.prototype.innerBoundaryIsParser_,
'outerBoundaryIs': ol.format.GML2.prototype.outerBoundaryIsParser_
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GML2.prototype.BOX_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'coordinates': ol.xml.makeArrayPusher(
ol.format.GML2.prototype.readFlatCoordinates_)
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GML2.prototype.GEOMETRY_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'Point': ol.xml.makeReplacer(ol.format.GMLBase.prototype.readPoint),
'MultiPoint': ol.xml.makeReplacer(
ol.format.GMLBase.prototype.readMultiPoint),
'LineString': ol.xml.makeReplacer(
ol.format.GMLBase.prototype.readLineString),
'MultiLineString': ol.xml.makeReplacer(
ol.format.GMLBase.prototype.readMultiLineString),
'LinearRing' : ol.xml.makeReplacer(
ol.format.GMLBase.prototype.readLinearRing),
'Polygon': ol.xml.makeReplacer(ol.format.GMLBase.prototype.readPolygon),
'MultiPolygon': ol.xml.makeReplacer(
ol.format.GMLBase.prototype.readMultiPolygon),
'Box': ol.xml.makeReplacer(ol.format.GML2.prototype.readBox_)
}
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,560 @@
// FIXME Envelopes should not be treated as geometries! readEnvelope_ is part
// of GEOMETRY_PARSERS_ and methods using GEOMETRY_PARSERS_ do not expect
// envelopes/extents, only geometries!
goog.provide('ol.format.GMLBase');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.NodeType');
goog.require('goog.object');
goog.require('goog.string');
goog.require('ol.Feature');
goog.require('ol.array');
goog.require('ol.format.Feature');
goog.require('ol.format.XMLFeature');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LineString');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.proj');
goog.require('ol.xml');
/**
* @classdesc
* Abstract base class; normally only used for creating subclasses and not
* instantiated in apps.
* Feature base format for reading and writing data in the GML format.
* This class cannot be instantiate, it contains only base content that
* is shared with versioned format classes ol.format.GML2 and
* ol.format.GML3.
*
* @constructor
* @param {olx.format.GMLOptions=} opt_options
* Optional configuration object.
* @extends {ol.format.XMLFeature}
* @api
*/
ol.format.GMLBase = function(opt_options) {
var options = /** @type {olx.format.GMLOptions} */
(goog.isDef(opt_options) ? opt_options : {});
/**
* @protected
* @type {string}
*/
this.featureType = options.featureType;
/**
* @protected
* @type {string}
*/
this.featureNS = options.featureNS;
/**
* @protected
* @type {string}
*/
this.srsName = options.srsName;
/**
* @protected
* @type {string}
*/
this.schemaLocation = '';
goog.base(this);
};
goog.inherits(ol.format.GMLBase, ol.format.XMLFeature);
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {Array.<ol.Feature>} Features.
* @private
*/
ol.format.GMLBase.prototype.readFeatures_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
var localName = ol.xml.getLocalName(node);
var features;
if (localName == 'FeatureCollection') {
features = ol.xml.pushParseAndPop(null,
this.FEATURE_COLLECTION_PARSERS, node,
objectStack, this);
} else if (localName == 'featureMembers' || localName == 'featureMember') {
var context = objectStack[0];
goog.asserts.assert(goog.isObject(context));
var featureType = goog.object.get(context, 'featureType');
if (!goog.isDef(featureType) && !goog.isNull(node.firstElementChild)) {
var member = node.firstElementChild;
featureType = member.nodeName.split(':').pop();
goog.object.set(context, 'featureType', featureType);
goog.object.set(context, 'featureNS', member.namespaceURI);
}
var parsers = {};
var parsersNS = {};
parsers[featureType] = (localName == 'featureMembers') ?
ol.xml.makeArrayPusher(this.readFeature_, this) :
ol.xml.makeReplacer(this.readFeature_, this);
parsersNS[goog.object.get(context, 'featureNS')] = parsers;
features = ol.xml.pushParseAndPop([], parsersNS, node, objectStack);
}
if (!goog.isDef(features)) {
features = [];
}
return features;
};
/**
* @type {Object.<string, Object.<string, Object>>}
*/
ol.format.GMLBase.prototype.FEATURE_COLLECTION_PARSERS = Object({
'http://www.opengis.net/gml': {
'featureMember': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.readFeatures_),
'featureMembers': ol.xml.makeReplacer(
ol.format.GMLBase.prototype.readFeatures_)
}
});
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.geom.Geometry|undefined} Geometry.
*/
ol.format.GMLBase.prototype.readGeometryElement = function(node, objectStack) {
var context = objectStack[0];
goog.asserts.assert(goog.isObject(context));
goog.object.set(context, 'srsName',
node.firstElementChild.getAttribute('srsName'));
var geometry = ol.xml.pushParseAndPop(/** @type {ol.geom.Geometry} */(null),
this.GEOMETRY_PARSERS_, node, objectStack, this);
if (goog.isDefAndNotNull(geometry)) {
return /** @type {ol.geom.Geometry} */ (
ol.format.Feature.transformWithOptions(geometry, false, context));
} else {
return undefined;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.Feature} Feature.
* @private
*/
ol.format.GMLBase.prototype.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) {
// Assume attribute elements have one child node and that the child
// is a text node. Otherwise assume it is a geometry node.
if (n.childNodes.length === 0 ||
(n.childNodes.length === 1 &&
n.firstChild.nodeType === 3)) {
var value = ol.xml.getAllTextContent(n, false);
if (goog.string.isEmpty(value)) {
value = undefined;
}
values[ol.xml.getLocalName(n)] = value;
} else {
geometryName = ol.xml.getLocalName(n);
values[geometryName] = this.readGeometryElement(n, objectStack);
}
}
var feature = new ol.Feature(values);
if (goog.isDef(geometryName)) {
feature.setGeometryName(geometryName);
}
if (fid) {
feature.setId(fid);
}
return feature;
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.geom.Point|undefined} Point.
*/
ol.format.GMLBase.prototype.readPoint = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'Point');
var flatCoordinates =
this.readFlatCoordinatesFromNode_(node, objectStack);
if (goog.isDefAndNotNull(flatCoordinates)) {
var point = new ol.geom.Point(null);
goog.asserts.assert(flatCoordinates.length == 3);
point.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates);
return point;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.geom.MultiPoint|undefined} MultiPoint.
*/
ol.format.GMLBase.prototype.readMultiPoint = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'MultiPoint');
var coordinates = ol.xml.pushParseAndPop(
/** @type {Array.<Array.<number>>} */ ([]),
this.MULTIPOINT_PARSERS_, node, objectStack, this);
if (goog.isDef(coordinates)) {
return new ol.geom.MultiPoint(coordinates);
} else {
return undefined;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.geom.MultiLineString|undefined} MultiLineString.
*/
ol.format.GMLBase.prototype.readMultiLineString = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'MultiLineString');
var lineStrings = ol.xml.pushParseAndPop(
/** @type {Array.<ol.geom.LineString>} */ ([]),
this.MULTILINESTRING_PARSERS_, node, objectStack, this);
if (goog.isDef(lineStrings)) {
var multiLineString = new ol.geom.MultiLineString(null);
multiLineString.setLineStrings(lineStrings);
return multiLineString;
} else {
return undefined;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.geom.MultiPolygon|undefined} MultiPolygon.
*/
ol.format.GMLBase.prototype.readMultiPolygon = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'MultiPolygon');
var polygons = ol.xml.pushParseAndPop(
/** @type {Array.<ol.geom.Polygon>} */ ([]),
this.MULTIPOLYGON_PARSERS_, node, objectStack, this);
if (goog.isDef(polygons)) {
var multiPolygon = new ol.geom.MultiPolygon(null);
multiPolygon.setPolygons(polygons);
return multiPolygon;
} else {
return undefined;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
*/
ol.format.GMLBase.prototype.pointMemberParser_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'pointMember' ||
node.localName == 'pointMembers');
ol.xml.parse(this.POINTMEMBER_PARSERS_,
node, objectStack, this);
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
*/
ol.format.GMLBase.prototype.lineStringMemberParser_ =
function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'lineStringMember' ||
node.localName == 'lineStringMembers');
ol.xml.parse(this.LINESTRINGMEMBER_PARSERS_,
node, objectStack, this);
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
*/
ol.format.GMLBase.prototype.polygonMemberParser_ =
function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'polygonMember' ||
node.localName == 'polygonMembers');
ol.xml.parse(this.POLYGONMEMBER_PARSERS_, node,
objectStack, this);
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.geom.LineString|undefined} LineString.
*/
ol.format.GMLBase.prototype.readLineString = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'LineString');
var flatCoordinates =
this.readFlatCoordinatesFromNode_(node, objectStack);
if (goog.isDefAndNotNull(flatCoordinates)) {
var lineString = new ol.geom.LineString(null);
lineString.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates);
return lineString;
} else {
return undefined;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
* @return {Array.<number>|undefined} LinearRing flat coordinates.
*/
ol.format.GMLBase.prototype.readFlatLinearRing_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'LinearRing');
var ring = ol.xml.pushParseAndPop(/** @type {Array.<number>} */(null),
this.GEOMETRY_FLAT_COORDINATES_PARSERS_, node,
objectStack, this);
if (goog.isDefAndNotNull(ring)) {
return ring;
} else {
return undefined;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.geom.LinearRing|undefined} LinearRing.
*/
ol.format.GMLBase.prototype.readLinearRing = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'LinearRing');
var flatCoordinates =
this.readFlatCoordinatesFromNode_(node, objectStack);
if (goog.isDef(flatCoordinates)) {
var ring = new ol.geom.LinearRing(null);
ring.setFlatCoordinates(ol.geom.GeometryLayout.XYZ, flatCoordinates);
return ring;
} else {
return undefined;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @return {ol.geom.Polygon|undefined} Polygon.
*/
ol.format.GMLBase.prototype.readPolygon = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
goog.asserts.assert(node.localName == 'Polygon');
var flatLinearRings = ol.xml.pushParseAndPop(
/** @type {Array.<Array.<number>>} */ ([null]),
this.FLAT_LINEAR_RINGS_PARSERS_, node, objectStack, this);
if (goog.isDef(flatLinearRings) &&
!goog.isNull(flatLinearRings[0])) {
var polygon = new ol.geom.Polygon(null);
var flatCoordinates = flatLinearRings[0];
var ends = [flatCoordinates.length];
var i, ii;
for (i = 1, ii = flatLinearRings.length; i < ii; ++i) {
ol.array.safeExtend(flatCoordinates, flatLinearRings[i]);
ends.push(flatCoordinates.length);
}
polygon.setFlatCoordinates(
ol.geom.GeometryLayout.XYZ, flatCoordinates, ends);
return polygon;
} else {
return undefined;
}
};
/**
* @param {Node} node Node.
* @param {Array.<*>} objectStack Object stack.
* @private
* @return {Array.<number>} Flat coordinates.
*/
ol.format.GMLBase.prototype.readFlatCoordinatesFromNode_ =
function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
return /** @type {Array.<number>} */ (ol.xml.pushParseAndPop(
null,
this.GEOMETRY_FLAT_COORDINATES_PARSERS_, node,
objectStack, this));
};
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GMLBase.prototype.MULTIPOINT_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'pointMember': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.pointMemberParser_),
'pointMembers': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.pointMemberParser_)
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GMLBase.prototype.MULTILINESTRING_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'lineStringMember': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.lineStringMemberParser_),
'lineStringMembers': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.lineStringMemberParser_)
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GMLBase.prototype.MULTIPOLYGON_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'polygonMember': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.polygonMemberParser_),
'polygonMembers': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.polygonMemberParser_)
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GMLBase.prototype.POINTMEMBER_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'Point': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.readFlatCoordinatesFromNode_)
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GMLBase.prototype.LINESTRINGMEMBER_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'LineString': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.readLineString)
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @private
*/
ol.format.GMLBase.prototype.POLYGONMEMBER_PARSERS_ = Object({
'http://www.opengis.net/gml' : {
'Polygon': ol.xml.makeArrayPusher(
ol.format.GMLBase.prototype.readPolygon)
}
});
/**
* @const
* @type {Object.<string, Object.<string, ol.xml.Parser>>}
* @protected
*/
ol.format.GMLBase.prototype.RING_PARSERS = Object({
'http://www.opengis.net/gml' : {
'LinearRing': ol.xml.makeReplacer(
ol.format.GMLBase.prototype.readFlatLinearRing_)
}
});
/**
* @inheritDoc
*/
ol.format.GMLBase.prototype.readGeometryFromNode =
function(node, opt_options) {
var geometry = this.readGeometryElement(node,
[this.getReadOptions(node, goog.isDef(opt_options) ? opt_options : {})]);
return goog.isDef(geometry) ? geometry : null;
};
/**
* Read all features from a GML FeatureCollection.
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Options.
* @return {Array.<ol.Feature>} Features.
* @api stable
*/
ol.format.GMLBase.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.GMLBase.prototype.readFeaturesFromNode =
function(node, opt_options) {
var options = {
'featureType': this.featureType,
'featureNS': this.featureNS
};
if (goog.isDef(opt_options)) {
goog.object.extend(options, this.getReadOptions(node, opt_options));
}
return this.readFeatures_(node, [options]);
};
/**
* @inheritDoc
*/
ol.format.GMLBase.prototype.readProjectionFromNode = function(node) {
return ol.proj.get(goog.isDef(this.srsName_) ? this.srsName_ :
node.firstElementChild.getAttribute('srsName'));
};

View File

@@ -3,7 +3,8 @@ goog.provide('ol.format.WFS');
goog.require('goog.asserts');
goog.require('goog.dom.NodeType');
goog.require('goog.object');
goog.require('ol.format.GML');
goog.require('ol.format.GML3');
goog.require('ol.format.GMLBase');
goog.require('ol.format.XMLFeature');
goog.require('ol.format.XSD');
goog.require('ol.geom.Geometry');
@@ -15,8 +16,9 @@ goog.require('ol.xml');
/**
* @classdesc
* Feature format for reading and writing data in the WFS format.
* Currently only supports WFS version 1.1.0.
* Also see {@link ol.format.GML} which is used by this format.
* By default, supports WFS version 1.1.0. You can pass a GML format
* as option if you want to read a WFS that contains GML2 (WFS 1.0.0).
* Also see {@link ol.format.GMLBase} which is used by this format.
*
* @constructor
* @param {olx.format.WFSOptions=} opt_options
@@ -40,6 +42,13 @@ ol.format.WFS = function(opt_options) {
*/
this.featureNS_ = options.featureNS;
/**
* @private
* @type {ol.format.GMLBase}
*/
this.gmlFormat_ = goog.isDef(options.gmlFormat) ?
options.gmlFormat : new ol.format.GML3();
/**
* @private
* @type {string}
@@ -119,7 +128,8 @@ ol.format.WFS.prototype.readFeaturesFromNode = function(node, opt_options) {
goog.isDef(opt_options) ? opt_options : {}));
var objectStack = [context];
var features = ol.xml.pushParseAndPop([],
ol.format.GML.FEATURE_COLLECTION_PARSERS, node, objectStack);
this.gmlFormat_.FEATURE_COLLECTION_PARSERS, node,
objectStack, this.gmlFormat_);
if (!goog.isDef(features)) {
features = [];
}
@@ -196,7 +206,7 @@ ol.format.WFS.prototype.readFeatureCollectionMetadataFromDocument =
ol.format.WFS.FEATURE_COLLECTION_PARSERS_ = {
'http://www.opengis.net/gml': {
'boundedBy': ol.xml.makeObjectPropertySetter(
ol.format.GML.readGeometry, 'bounds')
ol.format.GMLBase.prototype.readGeometryElement, 'bounds')
}
};
@@ -215,7 +225,7 @@ ol.format.WFS.prototype.readFeatureCollectionMetadataFromNode = function(node) {
goog.object.set(result, 'numberOfFeatures', value);
return ol.xml.pushParseAndPop(
/** @type {ol.format.WFS.FeatureCollectionMetadata} */ (result),
ol.format.WFS.FEATURE_COLLECTION_PARSERS_, node, []);
ol.format.WFS.FEATURE_COLLECTION_PARSERS_, node, [], this.gmlFormat_);
};
@@ -363,7 +373,7 @@ ol.format.WFS.writeFeature_ = function(node, feature, objectStack) {
var featureNS = goog.object.get(context, 'featureNS');
var child = ol.xml.createElementNS(featureNS, featureType);
node.appendChild(child);
ol.format.GML.writeFeature(child, feature, objectStack);
ol.format.GML3.prototype.writeFeatureElement(child, feature, objectStack);
};
@@ -457,7 +467,8 @@ ol.format.WFS.writeProperty_ = function(node, pair, objectStack) {
var value = ol.xml.createElementNS('http://www.opengis.net/wfs', 'Value');
node.appendChild(value);
if (pair.value instanceof ol.geom.Geometry) {
ol.format.GML.writeGeometry(value, pair.value, objectStack);
ol.format.GML3.prototype.writeGeometryElement(value,
pair.value, objectStack);
} else {
ol.format.XSD.writeStringTextNode(value, pair.value);
}
@@ -564,7 +575,7 @@ ol.format.WFS.writeOgcBBOX_ = function(node, bbox, objectStack) {
var bboxNode = ol.xml.createElementNS('http://www.opengis.net/ogc', 'BBOX');
node.appendChild(bboxNode);
ol.format.WFS.writeOgcPropertyName_(bboxNode, geometryName, objectStack);
ol.format.GML.writeGeometry(bboxNode, bbox, objectStack);
ol.format.GML3.prototype.writeGeometryElement(bboxNode, bbox, objectStack);
};
@@ -746,7 +757,7 @@ ol.format.WFS.prototype.readProjectionFromNode = function(node) {
(n.childNodes.length === 1 &&
n.firstChild.nodeType === 3))) {
var objectStack = [{}];
ol.format.GML.readGeometry(n, objectStack);
this.gmlFormat_.readGeometryElement(n, objectStack);
return ol.proj.get(objectStack.pop().srsName);
}
}

View File

@@ -379,7 +379,8 @@ ol.xml.makeArrayPusher = function(valueReader, opt_this) {
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this, node, objectStack);
var value = valueReader.call(goog.isDef(opt_this) ? opt_this : this,
node, objectStack);
if (goog.isDef(value)) {
var array = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isArray(array));
@@ -402,7 +403,8 @@ ol.xml.makeReplacer = function(valueReader, opt_this) {
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this, node, objectStack);
var value = valueReader.call(goog.isDef(opt_this) ? opt_this : this,
node, objectStack);
if (goog.isDef(value)) {
objectStack[objectStack.length - 1] = value;
}
@@ -426,7 +428,8 @@ ol.xml.makeObjectPropertyPusher =
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this, node, objectStack);
var value = valueReader.call(goog.isDef(opt_this) ? opt_this : this,
node, objectStack);
if (goog.isDef(value)) {
var object = /** @type {Object} */
(objectStack[objectStack.length - 1]);
@@ -456,7 +459,8 @@ ol.xml.makeObjectPropertySetter =
* @param {Array.<*>} objectStack Object stack.
*/
function(node, objectStack) {
var value = valueReader.call(opt_this, node, objectStack);
var value = valueReader.call(goog.isDef(opt_this) ? opt_this : this,
node, objectStack);
if (goog.isDef(value)) {
var object = /** @type {Object} */
(objectStack[objectStack.length - 1]);
@@ -494,7 +498,8 @@ ol.xml.makeParsersNS = function(namespaceURIs, parsers, opt_parsersNS) {
*/
ol.xml.makeChildAppender = function(nodeWriter, opt_this) {
return function(node, value, objectStack) {
nodeWriter.call(opt_this, node, value, objectStack);
nodeWriter.call(goog.isDef(opt_this) ? opt_this : this,
node, value, objectStack);
var parent = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isObject(parent));
var parentNode = parent.node;

View File

@@ -8,12 +8,112 @@ var readGeometry = function(format, text, opt_options) {
return format.readGeometryFromNode(node, opt_options);
};
describe('ol.format.GML', function() {
describe('ol.format.GML2', function() {
var format, formatWGS84, formatNoSrs;
beforeEach(function() {
format = new ol.format.GML2({srsName: 'CRS:84'});
formatWGS84 = new ol.format.GML2({
srsName: 'urn:x-ogc:def:crs:EPSG:4326'
});
formatNoSrs = new ol.format.GML2();
});
describe('#readGeometry', function() {
describe('gml 2.1.2', function() {
it('can read a point geometry', function() {
var text = '<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="urn:x-ogc:def:crs:EPSG:4326">' +
' <gml:coordinates>-90,-180</gml:coordinates>' +
'</gml:Point>';
var g = readGeometry(format, text);
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([-180, -90, 0]);
});
it('can read a box element', function() {
var text = '<gml:Box xmlns:gml="http://www.opengis.net/gml" ' +
'srsName="EPSG:4326">' +
' <gml:coordinates>-0.768746,47.003018 ' +
' 3.002191,47.925567</gml:coordinates>' +
'</gml:Box>';
var g = readGeometry(format, text);
expect(g).to.eql([47.003018, -0.768746, 47.925567, 3.002191]);
});
it('can read a multipolygon with gml:coordinates', function() {
var text =
'<gml:MultiPolygon xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="EPSG:4326">' +
' <gml:polygonMember>' +
' <gml:Polygon>' +
' <gml:outerBoundaryIs>' +
' <gml:LinearRing>' +
' <gml:coordinates>-0.318987,47.003018 ' +
' -0.768746,47.358268 ' +
' -0.574463,47.684285 -0.347374,47.854602 ' +
' -0.006740,47.925567 ' +
' 0.135191,47.726864 0.149384,47.599127 0.419052,' +
' 47.670092 0.532597,47.428810 ' +
' 0.305508,47.443003 0.475824,47.144948 0.064225,' +
' 47.201721 ' +
' -0.318987,47.003018 </gml:coordinates>' +
' </gml:LinearRing>' +
' </gml:outerBoundaryIs>' +
' <gml:innerBoundaryIs>' +
' <gml:LinearRing>' +
' <gml:coordinates>-0.035126,47.485582 -0.035126,' +
' 47.485582 ' +
' -0.049319,47.641706 -0.233829,47.655899 ' +
' -0.375760,47.457196 ' +
' -0.276408,47.286879 -0.035126,47.485582 ' +
' </gml:coordinates>' +
' </gml:LinearRing>' +
' </gml:innerBoundaryIs>' +
' </gml:Polygon>' +
' </gml:polygonMember>' +
'</gml:MultiPolygon>';
var g = readGeometry(format, text);
expect(g).to.be.an(ol.geom.MultiPolygon);
expect(g.getCoordinates()).to.eql([
[
[
[47.003018, -0.318987, 0], [47.358268, -0.768746, 0],
[47.684285, -0.574463, 0], [47.854602, -0.347374, 0],
[47.925567, -0.00674, 0], [47.726864, 0.135191, 0],
[47.599127, 0.149384, 0], [47.670092, 0.419052, 0],
[47.42881, 0.532597, 0], [47.443003, 0.305508, 0],
[47.144948, 0.475824, 0], [47.201721, 0.064225, 0],
[47.003018, -0.318987, 0]
],
[
[47.485582, -0.035126, 0], [47.485582, -0.035126, 0],
[47.641706, -0.049319, 0], [47.655899, -0.233829, 0],
[47.457196, -0.37576, 0], [47.286879, -0.276408, 0],
[47.485582, -0.035126, 0]
]
]
]);
});
});
});
});
describe('ol.format.GML3', function() {
var format, formatWGS84, formatNoSrs;
beforeEach(function() {
format = new ol.format.GML({srsName: 'CRS:84'});
formatWGS84 = new ol.format.GML({srsName: 'urn:x-ogc:def:crs:EPSG:4326'});
formatWGS84 = new ol.format.GML({
srsName: 'urn:x-ogc:def:crs:EPSG:4326'
});
formatNoSrs = new ol.format.GML();
});
@@ -216,7 +316,8 @@ describe('ol.format.GML', function() {
var g = readGeometry(format, text);
expect(g.getCoordinates()[0][0][0][0]).to.equal(-77.0081);
expect(g.getCoordinates()[0][0][0][1]).to.equal(38.9661);
format = new ol.format.GML({srsName: 'urn:x-ogc:def:crs:EPSG:4326',
format = new ol.format.GML({
srsName: 'urn:x-ogc:def:crs:EPSG:4326',
surface: false});
var serialized = format.writeGeometry(g);
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
@@ -971,6 +1072,8 @@ describe('ol.format.GML', function() {
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('ol.format.GML');
goog.require('ol.format.GML2');
goog.require('ol.format.GML3');
goog.require('ol.geom.LineString');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.MultiPoint');

View File

@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<wfs:FeatureCollection xmlns:ms="http://mapserver.gis.umn.edu/mapserver" xmlns:wfs="http://www.opengis.net/wfs" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd http://mapserver.gis.umn.edu/mapserver http://aneto.oco/cgi-bin/worldwfs?SERVICE=WFS&amp;VERSION=1.0.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=polygon&amp;OUTPUTFORMAT=XMLSCHEMA">
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-0.768746,47.003018 3.002191,47.925567</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<gml:featureMember>
<ms:polygon fid="1">
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>-0.768746,47.003018 0.532597,47.925567</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<ms:msGeometry>
<gml:MultiPolygon srsName="EPSG:4326">
<gml:polygonMember>
<gml:Polygon>
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>-0.318987,47.003018 -0.768746,47.358268 -0.574463,47.684285 -0.347374,47.854602 -0.006740,47.925567 0.135191,47.726864 0.149384,47.599127 0.419052,47.670092 0.532597,47.428810 0.305508,47.443003 0.475824,47.144948 0.064225,47.201721 -0.318987,47.003018 </gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
<gml:innerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>-0.035126,47.485582 -0.035126,47.485582 -0.049319,47.641706 -0.233829,47.655899 -0.375760,47.457196 -0.276408,47.286879 -0.035126,47.485582 </gml:coordinates>
</gml:LinearRing>
</gml:innerBoundaryIs>
</gml:Polygon>
</gml:polygonMember>
</gml:MultiPolygon>
</ms:msGeometry>
<ms:ogc_fid>1</ms:ogc_fid>
<ms:name>My Polygon with hole</ms:name>
<ms:id>0</ms:id>
</ms:polygon>
</gml:featureMember>
<gml:featureMember>
<ms:polygon fid="2">
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>1.511919,47.088176 3.002191,47.882988</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<ms:msGeometry>
<gml:Polygon srsName="EPSG:4326">
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>1.625463,47.357844 1.511919,47.741057 1.880938,47.882988 2.420275,47.797830 2.789295,47.485582 3.002191,47.457196 2.874453,47.088176 2.178993,47.343651 1.625463,47.357844 </gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
</ms:msGeometry>
<ms:ogc_fid>2</ms:ogc_fid>
<ms:name>My simple Polygon</ms:name>
<ms:id>0</ms:id>
</ms:polygon>
</gml:featureMember>
<gml:featureMember>
<ms:polygon fid="3">
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates>0.000000,45.000000 2.000000,47.000000</gml:coordinates>
</gml:Box>
</gml:boundedBy>
<ms:msGeometry>
<gml:MultiPolygon srsName="EPSG:4326">
<gml:polygonMember>
<gml:Polygon>
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>0.000000,45.000000 2.000000,45.000000 2.000000,47.000000 0.000000,47.000000 0.000000,45.000000 </gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
<gml:innerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>0.500000,45.500000 1.500000,45.500000 1.500000,46.500000 0.500000,46.500000 0.500000,45.500000 </gml:coordinates>
</gml:LinearRing>
</gml:innerBoundaryIs>
</gml:Polygon>
</gml:polygonMember>
</gml:MultiPolygon>
</ms:msGeometry>
<ms:ogc_fid>3</ms:ogc_fid>
<ms:name>my polygon with hole</ms:name>
<ms:id>3</ms:id>
</ms:polygon>
</gml:featureMember>
</wfs:FeatureCollection>

View File

@@ -50,6 +50,45 @@ describe('ol.format.WFS', function() {
});
describe('when parsing mapserver GML2 polygon', function() {
var features, feature, xml;
var config = {
'featureNS': 'http://mapserver.gis.umn.edu/mapserver',
'featureType': 'polygon',
'gmlFormat': new ol.format.GML2()
};
before(function(done) {
proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
afterLoadText('spec/ol/format/wfs/polygonv2.xml', function(data) {
try {
xml = data;
features = new ol.format.WFS(config).readFeatures(xml);
} catch (e) {
done(e);
}
done();
});
});
it('creates 3 features', function() {
expect(features).to.have.length(3);
});
it('creates a polygon for My Polygon with hole', function() {
feature = features[0];
expect(feature.getId()).to.equal('1');
expect(feature.get('name')).to.equal('My Polygon with hole');
expect(feature.get('boundedBy')).to.eql(
[47.003018, -0.768746, 47.925567, 0.532597]);
expect(feature.getGeometry()).to.be.an(ol.geom.MultiPolygon);
expect(feature.getGeometry().getFlatCoordinates()).
to.have.length(60);
});
});
describe('when parsing FeatureCollection', function() {
var response;
before(function(done) {
@@ -403,5 +442,6 @@ goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon');
goog.require('ol.format.GML2');
goog.require('ol.format.WFS');
goog.require('ol.proj');