GML parser.

This adds a parser (read/write) for GML v2 and v3. GML v3 is limited to the
simple features profile of GML 3.1.1, just like OpenLayers 2 was. This will
be the basis for the WFS parser, but it only makes sense to continue this work
once feature modification (insert, update, delete) is in place in ol3. So the
WFS parser will be another pull request.
This commit is contained in:
Bart van den Eijnden
2013-05-13 10:23:19 +02:00
parent 42cc4d7683
commit 5ad4734c24
55 changed files with 2679 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
@exportSymbol ol.parser.ogc.GML_v2
@exportProperty ol.parser.ogc.GML_v2.prototype.read
@exportProperty ol.parser.ogc.GML_v2.prototype.write
@exportSymbol ol.parser.ogc.GML_v3
@exportProperty ol.parser.ogc.GML_v3.prototype.read
@exportProperty ol.parser.ogc.GML_v3.prototype.write

556
src/ol/parser/ogc/gml.js Normal file
View File

@@ -0,0 +1,556 @@
goog.provide('ol.parser.ogc.GML');
goog.require('goog.array');
goog.require('goog.dom.xml');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.GeometryType');
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.parser.StringFeatureParser');
goog.require('ol.parser.XML');
/**
* @constructor
* @implements {ol.parser.StringFeatureParser}
* @param {ol.parser.GML2Options|ol.parser.GML3Options=} opt_options
* Optional configuration object.
* @extends {ol.parser.XML}
*/
ol.parser.ogc.GML = function(opt_options) {
if (goog.isDef(opt_options)) {
goog.object.extend(this, opt_options);
}
if (!goog.isDef(this.xy)) {
this.xy = true;
}
if (!goog.isDef(this.extractAttributes)) {
this.extractAttributes = true;
}
this.singleFeatureType = !goog.isDef(opt_options) ||
goog.isString(opt_options.featureType);
this.defaultNamespaceURI = 'http://www.opengis.net/gml';
this.readers = {
'http://www.opengis.net/wfs': {
'FeatureCollection': function(node, obj) {
this.readChildNodes(node, obj);
}
},
'http://www.opengis.net/gml': {
'_inherit': function(node, obj, container) {
// To be implemented by version specific parsers
},
'name': function(node, obj) {
obj.name = this.getChildValue(node);
},
'featureMember': function(node, obj) {
this.readChildNodes(node, obj);
},
'featureMembers': function(node, obj) {
this.readChildNodes(node, obj);
},
'GeometryCollection': function(node, container) {
var parts = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, parts, container]);
this.readChildNodes(node, parts);
container.geometry = {
type: ol.geom.GeometryType.GEOMETRYCOLLECTION,
parts: parts
};
},
'geometryMember': function(node, obj) {
this.readChildNodes(node, obj);
},
'MultiPoint': function(node, container) {
var parts = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, parts, container]);
this.readChildNodes(node, parts);
container.geometry = {
type: ol.geom.GeometryType.MULTIPOINT,
parts: parts
};
},
'pointMember': function(node, obj) {
this.readChildNodes(node, obj);
},
'MultiLineString': function(node, container) {
var parts = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, parts, container]);
this.readChildNodes(node, parts);
container.geometry = {
type: ol.geom.GeometryType.MULTILINESTRING,
parts: parts
};
},
'lineStringMember': function(node, obj) {
this.readChildNodes(node, obj);
},
'MultiPolygon': function(node, container) {
var parts = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, parts, container]);
this.readChildNodes(node, parts);
container.geometry = {
type: ol.geom.GeometryType.MULTIPOLYGON,
parts: parts
};
},
'polygonMember': function(node, obj) {
this.readChildNodes(node, obj);
},
'Point': function(node, container) {
var coordinates = [];
this.readChildNodes(node, coordinates);
var point = {
type: ol.geom.GeometryType.POINT,
coordinates: coordinates[0][0]
};
// in the case of a multi geometry this is parts
if (goog.isArray(container)) {
container.push(point);
} else {
container.geometry = point;
}
},
'LineString': function(node, container) {
var coordinates = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, coordinates, container]);
this.readChildNodes(node, coordinates);
var linestring = {
type: ol.geom.GeometryType.LINESTRING,
coordinates: coordinates[0]
};
// in the case of a multi geometry this is parts
if (goog.isArray(container)) {
container.push(linestring);
} else {
container.geometry = linestring;
}
},
'Polygon': function(node, container) {
var obj = {outer: null, inner: []};
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, obj, container]);
this.readChildNodes(node, obj);
obj.inner.unshift(obj.outer);
var polygon = {
type: ol.geom.GeometryType.POLYGON,
coordinates: obj.inner
};
// in the case of a multi geometry this is parts
if (goog.isArray(container)) {
container.push(polygon);
} else {
container.geometry = polygon;
}
},
'LinearRing': function(node, container) {
var coordinates = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, coordinates, container]);
this.readChildNodes(node, coordinates);
if (goog.isArray(container)) {
container.push(coordinates);
} else {
container.geometry = {
type: ol.geom.GeometryType.LINEARRING,
coordinates: coordinates[0]
};
}
},
'coordinates': function(node, coordinates) {
var str = this.getChildValue(node).replace(
this.regExes.trimSpace, '');
str = str.replace(this.regExes.trimComma, ',');
var coords;
var cs = node.getAttribute('cs') || ',';
var ts = node.getAttribute('ts') || this.regExes.splitSpace;
var pointList = str.split(ts);
var numPoints = pointList.length;
var points = new Array(numPoints);
for (var i = 0; i < numPoints; ++i) {
coords = pointList[i].split(cs).map(parseFloat);
if (this.xy) {
points[i] = coords;
} else {
if (coords.length === 2) {
points[i] = coords.reverse();
} else if (coords.length === 3) {
points[i] = [coords[1], coords[0], coords[2]];
}
}
}
coordinates.push(points);
},
'coord': function(node, coordinates) {
var coord = {};
if (coordinates.length === 0) {
coordinates.push([]);
}
this.readChildNodes(node, coord);
if (goog.isDef(coord.z)) {
coordinates.push([coord.x, coord.y, coord.z]);
} else {
coordinates[0].push([coord.x, coord.y]);
}
},
'X': function(node, coord) {
coord.x = parseFloat(this.getChildValue(node));
},
'Y': function(node, coord) {
coord.y = parseFloat(this.getChildValue(node));
},
'Z': function(node, coord) {
coord.z = parseFloat(this.getChildValue(node));
}
}
};
this.featureNSReaders_ = {
'*': function(node, obj) {
// The node can either be named like the featureType, or it
// can be a child of the feature:featureType. Children can be
// geometry or attributes.
var name;
var local = node.localName || node.nodeName.split(':').pop();
// Since an attribute can have the same name as the feature type
// we only want to read the node as a feature if the parent
// node can have feature nodes as children. In this case, the
// obj.features property is set.
if (obj.features) {
if (!this.singleFeatureType &&
(goog.array.indexOf(this.featureType, local) !== -1)) {
name = '_typeName';
} else if (local === this.featureType) {
name = '_typeName';
}
} else {
// Assume attribute elements have one child node and that the child
// is a text node. Otherwise assume it is a geometry node.
if (node.childNodes.length === 0 ||
(node.childNodes.length === 1 &&
node.firstChild.nodeType === 3)) {
if (this.extractAttributes) {
name = '_attribute';
}
} else {
name = '_geometry';
}
}
if (name) {
this.readers[this.featureNS][name].apply(this, [node, obj]);
}
},
'_typeName': function(node, obj) {
var container = {properties: {}};
this.readChildNodes(node, container);
// look for common gml namespaced elements
if (container.name) {
container.properties.name = container.name;
}
var feature = new ol.Feature(container.properties);
var geom = container.geometry;
if (geom) {
var sharedVertices = undefined;
if (this.readFeaturesOptions_) {
var callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, geom.type);
}
}
var geometry = this.createGeometry_({geometry: geom},
sharedVertices);
if (goog.isDef(geometry)) {
feature.setGeometry(geometry);
}
}
// TODO set feature.type and feature.namespace
// TODO set fid
obj.features.push(feature);
},
'_geometry': function(node, obj) {
if (!this.geometryName) {
this.geometryName = node.nodeName.split(':').pop();
}
this.readChildNodes(node, obj);
},
'_attribute': function(node, obj) {
var local = node.localName || node.nodeName.split(':').pop();
var value = this.getChildValue(node);
obj.properties[local] = value;
}
};
if (goog.isDef(this.featureNS)) {
this.readers[this.featureNS] = this.featureNSReaders_;
}
this.writers = {
'http://www.opengis.net/gml': {
'featureMember': function(feature) {
var node = this.createElementNS('gml:featureMember');
this.writeNode('_typeName', feature, this.featureNS, node);
return node;
},
'MultiPoint': function(geometry) {
var node = this.createElementNS('gml:MultiPoint');
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
this.writeNode('pointMember', geometry.components[i], null, node);
}
return node;
},
'pointMember': function(geometry) {
var node = this.createElementNS('gml:pointMember');
this.writeNode('Point', geometry, null, node);
return node;
},
'MultiLineString': function(geometry) {
var node = this.createElementNS('gml:MultiLineString');
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
this.writeNode('lineStringMember', geometry.components[i], null,
node);
}
return node;
},
'lineStringMember': function(geometry) {
var node = this.createElementNS('gml:lineStringMember');
this.writeNode('LineString', geometry, null, node);
return node;
},
'MultiPolygon': function(geometry) {
var node = this.createElementNS('gml:MultiPolygon');
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
this.writeNode('polygonMember', geometry.components[i], null, node);
}
return node;
},
'polygonMember': function(geometry) {
var node = this.createElementNS('gml:polygonMember');
this.writeNode('Polygon', geometry, null, node);
return node;
},
'GeometryCollection': function(geometry) {
var node = this.createElementNS('gml:GeometryCollection');
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
this.writeNode('geometryMember', geometry.components[i], null, node);
}
return node;
},
'geometryMember': function(geometry) {
var node = this.createElementNS('gml:geometryMember');
var child = this.writeNode('_geometry', geometry, this.featureNS);
node.appendChild(child.firstChild);
return node;
}
},
'http://www.opengis.net/wfs': {
'FeatureCollection': function(features) {
/**
* This is only here because GML2 only describes abstract
* feature collections. Typically, you would not be using
* the GML format to write wfs elements. This just provides
* some way to write out lists of features. GML3 defines the
* featureMembers element, so that is used by default instead.
*/
var node = this.createElementNS('wfs:FeatureCollection',
'http://www.opengis.net/wfs');
for (var i = 0, ii = features.length; i < ii; ++i) {
this.writeNode('featureMember', features[i], null, node);
}
return node;
}
}
};
this.featureNSWiters_ = {
'_typeName': function(feature) {
var node = this.createElementNS('feature:' + this.featureType,
this.featureNS);
// TODO: https://github.com/openlayers/ol3/issues/558
// this.setAttributeNS(node, null, 'fid', feature.fid);
if (feature.getGeometry() !== null) {
this.writeNode('_geometry', feature.getGeometry(), this.featureNS,
node);
}
var attributes = feature.getAttributes();
for (var name in attributes) {
var value = attributes[name];
if (goog.isDefAndNotNull(value) && !(value instanceof
ol.geom.Geometry)) {
this.writeNode('_attribute', {name: name, value: value},
this.featureNS, node);
}
}
return node;
},
'_geometry': function(geometry) {
var node = this.createElementNS('feature:' + this.geometryName,
this.featureNS);
var type = geometry.getType(), child;
if (type === ol.geom.GeometryType.POINT) {
child = this.writeNode('Point', geometry, null, node);
} else if (type === ol.geom.GeometryType.MULTIPOINT) {
child = this.writeNode('MultiPoint', geometry, null, node);
} else if (type === ol.geom.GeometryType.LINEARRING) {
child = this.writeNode('LinearRing', geometry.getCoordinates(), null,
node);
} else if (type === ol.geom.GeometryType.LINESTRING) {
child = this.writeNode('LineString', geometry, null, node);
} else if (type === ol.geom.GeometryType.MULTILINESTRING) {
child = this.writeNode('MultiLineString', geometry, null, node);
} else if (type === ol.geom.GeometryType.POLYGON) {
child = this.writeNode('Polygon', geometry, null, node);
} else if (type === ol.geom.GeometryType.MULTIPOLYGON) {
child = this.writeNode('MultiPolygon', geometry, null, node);
} else if (type === ol.geom.GeometryType.GEOMETRYCOLLECTION) {
child = this.writeNode('GeometryCollection', geometry, null, node);
}
if (goog.isDef(this.srsName)) {
this.setAttributeNS(child, null, 'srsName', this.srsName);
}
return node;
},
'_attribute': function(obj) {
var node = this.createElementNS('feature:' + obj.name, this.featureNS);
node.appendChild(this.createTextNode(obj.value));
return node;
}
};
this.writers[this.featureNS] = this.featureNSWiters_;
goog.base(this);
};
goog.inherits(ol.parser.ogc.GML, ol.parser.XML);
/**
* @param {string|Document|Element|Object} data Data to read.
* @return {Object} An object representing the document.
*/
ol.parser.ogc.GML.prototype.read = function(data) {
if (typeof data == 'string') {
data = goog.dom.xml.loadXml(data);
}
if (data && data.nodeType == 9) {
data = data.documentElement;
}
var obj = {features: []};
this.readNode(data, obj, true);
return obj;
};
/**
* @param {Element|Document} node The node to be read.
* @param {Object} obj The object to be modified.
* @param {boolean=} opt_first Should be set to true for the first node read.
* This is usually the readNode call in the read method. Without this being
* set, auto-configured properties will stick on subsequent reads.
* @return {Object} The input object, modified (or a new one if none was
* provided).
*/
ol.parser.ogc.GML.prototype.readNode = function(node, obj, opt_first) {
// on subsequent calls of this.read(), we want to reset auto-
// configured properties and auto-configure again.
if (opt_first === true && this.autoConfig === true) {
this.featureType = null;
delete this.readers[this.featureNS];
delete this.writers[this.featureNS];
this.featureNS = null;
}
// featureType auto-configuration
if (!this.featureNS && (!(node.namespaceURI in this.readers) &&
node.parentNode.namespaceURI == this.defaultNamespaceURI &&
(/^(.*:)?featureMembers?$/).test(node.parentNode.nodeName))) {
this.featureType = node.nodeName.split(':').pop();
this.readers[node.namespaceURI] = this.featureNSReaders_;
this.writers[node.namespaceURI] = this.featureNSWiters_;
this.featureNS = node.namespaceURI;
this.autoConfig = true;
}
return ol.parser.XML.prototype.readNode.apply(this, [node, obj]);
};
/**
* @private
* @param {Object} container Geometry container.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Geometry} The geometry created.
*/
// TODO use a mixin since this is also used in the KML parser
ol.parser.ogc.GML.prototype.createGeometry_ = function(container,
opt_vertices) {
var geometry = null, coordinates, i, ii;
switch (container.geometry.type) {
case ol.geom.GeometryType.POINT:
geometry = new ol.geom.Point(container.geometry.coordinates,
opt_vertices);
break;
case ol.geom.GeometryType.LINEARRING:
geometry = new ol.geom.LinearRing(container.geometry.coordinates,
opt_vertices);
break;
case ol.geom.GeometryType.LINESTRING:
geometry = new ol.geom.LineString(container.geometry.coordinates,
opt_vertices);
break;
case ol.geom.GeometryType.POLYGON:
geometry = new ol.geom.Polygon(container.geometry.coordinates,
opt_vertices);
break;
case ol.geom.GeometryType.MULTIPOINT:
coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates);
}
geometry = new ol.geom.MultiPoint(coordinates, opt_vertices);
break;
case ol.geom.GeometryType.MULTILINESTRING:
coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates);
}
geometry = new ol.geom.MultiLineString(coordinates, opt_vertices);
break;
case ol.geom.GeometryType.MULTIPOLYGON:
coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates);
}
geometry = new ol.geom.MultiPolygon(coordinates, opt_vertices);
break;
case ol.geom.GeometryType.GEOMETRYCOLLECTION:
var geometries = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
geometries.push(this.createGeometry_({
geometry: container.geometry.parts[i]
}, opt_vertices));
}
geometry = new ol.geom.GeometryCollection(geometries);
break;
default:
break;
}
return geometry;
};
/**
* Parse a GML document provided as a string.
* @param {string} str GML document.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {Array.<ol.Feature>} Array of features.
*/
ol.parser.ogc.GML.prototype.readFeaturesFromString =
function(str, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(str).features;
};

122
src/ol/parser/ogc/gml_v2.js Normal file
View File

@@ -0,0 +1,122 @@
goog.provide('ol.parser.ogc.GML_v2');
goog.require('goog.dom.xml');
goog.require('goog.object');
goog.require('ol.parser.ogc.GML');
/**
* @constructor
* @param {ol.parser.GML2Options=} opt_options Optional configuration object.
* @extends {ol.parser.ogc.GML}
*/
ol.parser.ogc.GML_v2 = function(opt_options) {
this.schemaLocation = 'http://www.opengis.net/gml ' +
'http://schemas.opengis.net/gml/2.1.2/feature.xsd';
goog.base(this, opt_options);
goog.object.extend(this.readers['http://www.opengis.net/gml'], {
'outerBoundaryIs': function(node, container) {
var coordinates = [];
this.readChildNodes(node, coordinates);
container['outer'] = coordinates[0][0];
},
'innerBoundaryIs': function(node, container) {
var coordinates = [];
this.readChildNodes(node, coordinates);
container.inner.push(coordinates[0][0]);
},
'Box': function(node, container) {
var coordinates = [];
this.readChildNodes(node, coordinates);
container.bounds = [coordinates[0][0][0], coordinates[0][1][0],
coordinates[0][0][1], coordinates[0][1][1]];
}
});
goog.object.extend(this.writers['http://www.opengis.net/gml'], {
'Point': function(geometry) {
var node = this.createElementNS('gml:Point');
this.writeNode('coordinates', [geometry.getCoordinates()], null, node);
return node;
},
'coordinates': function(coordinates) {
var numCoordinates = coordinates.length;
var parts = new Array(numCoordinates);
for (var i = 0; i < numCoordinates; ++i) {
var coord = coordinates[i];
var str = '';
if (this.xy) {
str += coord[0] + ',' + coord[1];
} else {
str += coord[1] + ',' + coord[0];
}
if (coord.length === 3) {
str += ',' + coord[2];
}
parts[i] = str;
}
var value = (numCoordinates === 1) ? parts[0] : parts.join(' ');
var node = this.createElementNS('gml:coordinates');
this.setAttributeNS(node, null, 'decimal', '.');
this.setAttributeNS(node, null, 'cs', ',');
this.setAttributeNS(node, null, 'ts', ' ');
node.appendChild(this.createTextNode(value));
return node;
},
'LineString': function(geometry) {
var node = this.createElementNS('gml:LineString');
this.writeNode('coordinates', geometry.getCoordinates(), null, node);
return node;
},
'Polygon': function(geometry) {
var node = this.createElementNS('gml:Polygon');
var coordinates = geometry.getCoordinates();
this.writeNode('outerBoundaryIs', coordinates[0], null, node);
for (var i = 1; i < coordinates.length; ++i) {
this.writeNode('innerBoundaryIs', coordinates[i], null, node);
}
return node;
},
'outerBoundaryIs': function(ring) {
var node = this.createElementNS('gml:outerBoundaryIs');
this.writeNode('LinearRing', ring, null, node);
return node;
},
'innerBoundaryIs': function(ring) {
var node = this.createElementNS('gml:innerBoundaryIs');
this.writeNode('LinearRing', ring, null, node);
return node;
},
'LinearRing': function(ring) {
var node = this.createElementNS('gml:LinearRing');
this.writeNode('coordinates', ring, null, node);
return node;
},
'Box': function(extent) {
var node = this.createElementNS('gml:Box');
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)) {
// TODO setAttribute or this.setAttributeNS
node.setAttribute('srsName', this.srsName);
}
return node;
}
});
};
goog.inherits(ol.parser.ogc.GML_v2, ol.parser.ogc.GML);
/**
* @param {Object} obj Object structure to write out as XML.
* @return {string} An string representing the XML document.
*/
ol.parser.ogc.GML_v2.prototype.write = function(obj) {
var root = this.writeNode('FeatureCollection', obj.features,
'http://www.opengis.net/wfs');
this.setAttributeNS(
root, 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation', this.schemaLocation);
return this.serialize(root);
};

422
src/ol/parser/ogc/gml_v3.js Normal file
View File

@@ -0,0 +1,422 @@
goog.provide('ol.parser.ogc.GML_v3');
goog.require('goog.dom.xml');
goog.require('goog.object');
goog.require('ol.geom.GeometryType');
goog.require('ol.parser.ogc.GML');
/**
* @constructor
* @param {ol.parser.GML3Options=} opt_options Optional configuration object.
* @extends {ol.parser.ogc.GML}
*/
ol.parser.ogc.GML_v3 = function(opt_options) {
this.schemaLocation = 'http://www.opengis.net/gml ' +
'http://schemas.opengis.net/gml/3.1.1/profiles/gmlsfProfile/' +
'1.0.0/gmlsf.xsd';
goog.base(this, opt_options);
if (!goog.isDef(this.surface)) {
this.surface = false;
}
if (!goog.isDef(this.curve)) {
this.curve = false;
}
if (!goog.isDef(this.multiCurve)) {
this.multiCurve = true;
}
if (!goog.isDef(this.multiSurface)) {
this.multiSurface = true;
}
this.featureNSWiters_['_geometry'] = function(geometry) {
var node = this.createElementNS('feature:' + this.geometryName,
this.featureNS);
var type = geometry.getType(), child;
if (type === ol.geom.GeometryType.POINT) {
child = this.writeNode('Point', geometry, null, node);
} else if (type === ol.geom.GeometryType.MULTIPOINT) {
child = this.writeNode('MultiPoint', geometry, null, node);
} else if (type === ol.geom.GeometryType.LINESTRING) {
if (this.curve === true) {
child = this.writeNode('Curve', geometry, null, node);
} else {
child = this.writeNode('LineString', geometry, null, node);
}
} else if (type === ol.geom.GeometryType.LINEARRING) {
child = this.writeNode('LinearRing', geometry.getCoordinates(), null,
node);
} else if (type === ol.geom.GeometryType.MULTILINESTRING) {
if (this.multiCurve === false) {
child = this.writeNode('MultiLineString', geometry, null, node);
} else {
child = this.writeNode('MultiCurve', geometry, null, node);
}
} else if (type === ol.geom.GeometryType.POLYGON) {
if (this.surface === true) {
child = this.writeNode('Surface', geometry, null, node);
} else {
child = this.writeNode('Polygon', geometry, null, node);
}
} else if (type === ol.geom.GeometryType.MULTIPOLYGON) {
if (this.multiSurface === false) {
child = this.writeNode('MultiPolygon', geometry, null, node);
} else {
child = this.writeNode('MultiSurface', geometry, null, node);
}
} 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);
}
return node;
};
goog.object.extend(this.readers['http://www.opengis.net/gml'], {
'_inherit': function(node, obj, container) {
// SRSReferenceGroup attributes
var dim = parseInt(node.getAttribute('srsDimension'), 10) ||
(container && container.srsDimension);
if (dim) {
obj.srsDimension = dim;
}
},
'featureMembers': function(node, obj) {
this.readChildNodes(node, obj);
},
'Curve': function(node, container) {
var coordinates = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, coordinates, container]);
this.readChildNodes(node, coordinates);
var linestring = {
type: ol.geom.GeometryType.LINESTRING,
coordinates: coordinates[0]
};
// in the case of a multi geometry this is parts
if (goog.isArray(container)) {
container.push(linestring);
} else {
container.geometry = linestring;
}
},
'segments': function(node, obj) {
this.readChildNodes(node, obj);
},
'LineStringSegment': function(node, container) {
var coordinates = [];
this.readChildNodes(node, coordinates);
container.push(coordinates[0]);
},
'pos': function(node, obj) {
var str = this.getChildValue(node).replace(
this.regExes.trimSpace, '');
var coords = str.split(this.regExes.splitSpace).map(parseFloat);
if (this.xy) {
obj.push([coords]);
} else {
if (coords.length === 2) {
obj.push([coords.reverse()]);
} else if (coords.length === 3) {
obj.push([coords[1], coords[0], coords[2]]);
}
}
},
'posList': function(node, obj) {
var str = this.getChildValue(node).replace(
this.regExes.trimSpace, '');
var coords = str.split(this.regExes.splitSpace);
// The "dimension" attribute is from the GML 3.0.1 spec.
var dim = obj.srsDimension ||
parseInt(node.getAttribute('srsDimension') ||
node.getAttribute('dimension'), 10) || 2;
var j, x, y, z;
var numPoints = coords.length / dim;
var points = new Array(numPoints);
for (var i = 0, ii = coords.length; i < ii; i += dim) {
x = parseFloat(coords[i]);
y = parseFloat(coords[i + 1]);
if (dim === 3) {
if (this.xy) {
points[i / dim] = [x, y, parseFloat(coords[i + 2])];
} else {
points[i / dim] = [y, x, parseFloat(coords[i + 2])];
}
} else if (dim === 2) {
if (this.xy) {
points[i / dim] = [x, y];
} else {
points[i / dim] = [y, x];
}
}
}
obj.push(points);
},
'Surface': function(node, obj) {
this.readChildNodes(node, obj);
},
'patches': function(node, obj) {
this.readChildNodes(node, obj);
},
'PolygonPatch': function(node, obj) {
this.readers[this.defaultNamespaceURI]['Polygon'].apply(this,
[node, obj]);
},
'exterior': function(node, container) {
var coordinates = [];
this.readChildNodes(node, coordinates);
container.outer = coordinates[0][0];
},
'interior': function(node, container) {
var coordinates = [];
this.readChildNodes(node, coordinates);
container.inner.push(coordinates[0][0]);
},
'MultiCurve': function(node, container) {
var parts = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, parts, container]);
this.readChildNodes(node, parts);
container.geometry = {
type: ol.geom.GeometryType.MULTILINESTRING,
parts: parts
};
},
'curveMember': function(node, obj) {
this.readChildNodes(node, obj);
},
'MultiSurface': function(node, container) {
var parts = [];
this.readers[this.defaultNamespaceURI]['_inherit'].apply(this,
[node, parts, container]);
this.readChildNodes(node, parts);
container.geometry = {
type: ol.geom.GeometryType.MULTIPOLYGON,
parts: parts
};
},
'surfaceMember': function(node, obj) {
this.readChildNodes(node, obj);
},
'surfaceMembers': function(node, obj) {
this.readChildNodes(node, obj);
},
'pointMembers': function(node, obj) {
this.readChildNodes(node, obj);
},
'lineStringMembers': function(node, obj) {
this.readChildNodes(node, obj);
},
'polygonMembers': function(node, obj) {
this.readChildNodes(node, obj);
},
'geometryMembers': function(node, obj) {
this.readChildNodes(node, obj);
},
'Envelope': function(node, container) {
var coordinates = [];
this.readChildNodes(node, coordinates);
container.bounds = [coordinates[0][0][0][0], coordinates[1][0][0][0],
coordinates[0][0][0][1], coordinates[1][0][0][1]];
},
'lowerCorner': function(node, envelope) {
var coordinates = [];
this.readers[this.defaultNamespaceURI]['pos'].apply(this,
[node, coordinates]);
envelope.push(coordinates);
},
'upperCorner': function(node, envelope) {
var coordinates = [];
this.readers[this.defaultNamespaceURI]['pos'].apply(this,
[node, coordinates]);
envelope.push(coordinates);
}
});
goog.object.extend(this.writers['http://www.opengis.net/gml'], {
'featureMembers': function(features) {
var node = this.createElementNS('gml:featureMembers');
for (var i = 0, ii = features.length; i < ii; ++i) {
this.writeNode('_typeName', features[i], this.featureNS, node);
}
return node;
},
'Point': function(geometry) {
var node = this.createElementNS('gml:Point');
this.writeNode('pos', geometry.getCoordinates(), null, node);
return node;
},
'pos': function(point) {
// only 2d for simple features profile
var pos;
if (this.xy) {
pos = (point[0] + ' ' + point[1]);
} else {
pos = (point[1] + ' ' + point[0]);
}
var node = this.createElementNS('gml:pos');
node.appendChild(this.createTextNode(pos));
return node;
},
'LineString': function(geometry) {
var node = this.createElementNS('gml:LineString');
this.writeNode('posList', geometry.getCoordinates(), null, node);
return node;
},
'Curve': function(geometry) {
var node = this.createElementNS('gml:Curve');
this.writeNode('segments', geometry, null, node);
return node;
},
'segments': function(geometry) {
var node = this.createElementNS('gml:segments');
this.writeNode('LineStringSegment', geometry, null, node);
return node;
},
'LineStringSegment': function(geometry) {
var node = this.createElementNS('gml:LineStringSegment');
this.writeNode('posList', geometry.getCoordinates(), null, node);
return node;
},
'posList': function(points) {
// only 2d for simple features profile
var len = points.length;
var parts = new Array(len);
var point;
for (var i = 0; i < len; ++i) {
point = points[i];
if (this.xy) {
parts[i] = point[0] + ' ' + point[1];
} else {
parts[i] = point[1] + ' ' + point[0];
}
}
var node = this.createElementNS('gml:posList');
node.appendChild(this.createTextNode(parts.join(' ')));
return node;
},
'Surface': function(geometry) {
var node = this.createElementNS('gml:Surface');
this.writeNode('patches', geometry, null, node);
return node;
},
'patches': function(geometry) {
var node = this.createElementNS('gml:patches');
this.writeNode('PolygonPatch', geometry, null, node);
return node;
},
'PolygonPatch': function(geometry) {
var node = this.createElementNS('gml:PolygonPatch');
node.setAttribute('interpolation', 'planar');
var coordinates = geometry.getCoordinates();
this.writeNode('exterior', coordinates[0], null, node);
for (var i = 1, len = coordinates.length; i < len; ++i) {
this.writeNode('interior', coordinates[i], null, node);
}
return node;
},
'Polygon': function(geometry) {
var node = this.createElementNS('gml:Polygon');
var coordinates = geometry.getCoordinates();
this.writeNode('exterior', coordinates[0], null, node);
for (var i = 1, len = coordinates.length; i < len; ++i) {
this.writeNode('interior', coordinates[i], null, node);
}
return node;
},
'exterior': function(ring) {
var node = this.createElementNS('gml:exterior');
this.writeNode('LinearRing', ring, null, node);
return node;
},
'interior': function(ring) {
var node = this.createElementNS('gml:interior');
this.writeNode('LinearRing', ring, null, node);
return node;
},
'LinearRing': function(ring) {
var node = this.createElementNS('gml:LinearRing');
this.writeNode('posList', ring, null, node);
return node;
},
'MultiCurve': function(geometry) {
var node = this.createElementNS('gml:MultiCurve');
for (var i = 0, len = geometry.components.length; i < len; ++i) {
this.writeNode('curveMember', geometry.components[i], null, node);
}
return node;
},
'curveMember': function(geometry) {
var node = this.createElementNS('gml:curveMember');
if (this.curve) {
this.writeNode('Curve', geometry, null, node);
} else {
this.writeNode('LineString', geometry, null, node);
}
return node;
},
'MultiSurface': function(geometry) {
var node = this.createElementNS('gml:MultiSurface');
for (var i = 0, len = geometry.components.length; i < len; ++i) {
this.writeNode('surfaceMember', geometry.components[i], null, node);
}
return node;
},
'surfaceMember': function(polygon) {
var node = this.createElementNS('gml:surfaceMember');
if (this.surface) {
this.writeNode('Surface', polygon, null, node);
} else {
this.writeNode('Polygon', polygon, null, node);
}
return node;
},
'Envelope': function(bounds) {
var node = this.createElementNS('gml:Envelope');
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);
}
return node;
},
'lowerCorner': function(bounds) {
// only 2d for simple features profile
var pos;
if (this.xy) {
pos = (bounds.left + ' ' + bounds.bottom);
} else {
pos = (bounds.bottom + ' ' + bounds.left);
}
var node = this.createElementNS('gml:lowerCorner');
node.appendChild(this.createTextNode(pos));
return node;
},
'upperCorner': function(bounds) {
// only 2d for simple features profile
var pos;
if (this.xy) {
pos = (bounds.right + ' ' + bounds.top);
} else {
pos = (bounds.top + ' ' + bounds.right);
}
var node = this.createElementNS('gml:upperCorner');
node.appendChild(this.createTextNode(pos));
return node;
}
});
};
goog.inherits(ol.parser.ogc.GML_v3, ol.parser.ogc.GML);
/**
* @param {Object} obj Object structure to write out as XML.
* @return {string} An string representing the XML document.
*/
ol.parser.ogc.GML_v3.prototype.write = function(obj) {
var root = this.writeNode('featureMembers', obj.features);
this.setAttributeNS(
root, 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation', this.schemaLocation);
return this.serialize(root);
};

View File

@@ -1,5 +1,6 @@
goog.provide('ol.parser.XML');
goog.require('goog.dom.xml');
goog.require('ol.parser.Parser');
@@ -246,3 +247,27 @@ ol.parser.XML.prototype.setAttributeNS = function(node, uri, name, value) {
}
}
};
/**
* Serializes a node.
*
* @param {Element} node Element node to serialize.
* @return {string} The serialized XML string.
*/
ol.parser.XML.prototype.serialize = function(node) {
if (node.nodeType == 1) {
// Add nodes to a document before serializing. Everything else
// is serialized as is. This is also needed to get all namespaces
// defined in some browsers such as Chrome (xmlns attributes).
var doc = document.implementation.createDocument('', '', null);
if (doc.importNode) {
doc.appendChild(doc.importNode(node, true));
} else {
doc.appendChild(node);
}
return goog.dom.xml.serialize(doc);
} else {
return goog.dom.xml.serialize(node);
}
};