move srsName and axisOrientation from the instance to the read and write functions as discussed with @ahocevar

This commit is contained in:
Bart van den Eijnden
2013-07-30 14:44:50 +02:00
committed by ahocevar
parent 6f3fa14c53
commit 2f4f508d1a
28 changed files with 122 additions and 98 deletions

View File

@@ -356,10 +356,6 @@
/**
* @typedef {Object} ol.parser.GMLOptions
* @property {string|undefined} axisOrientation The axis orientation as
* specified in Proj4. If this is not provided, the projection's axis
* orientation will be used if `projection` is not overridden (i.e. the
* projection defined in the data is used). Otherwise the default is 'enu'.
* @property {boolean|undefined} curve Write gml:Curve instead of
* gml:LineString elements. This also affects the elements in multi-part
* geometries. Default is `false´. This only applies to GML version 3.
@@ -380,11 +376,6 @@
* default is `true´. This only applies to GML version 3.
* @property {string|undefined} schemaLocation Optional schemaLocation to use
* when writing out the GML, this will override the default provided.
* @property {string|undefined} srsName URI for spatial reference system.
* This is optional for single part geometries and mandatory for
* collections and multis. If set, the srsName attribute will be written
* for all geometries. Default is null. For reading only, no `srsName` needs
* to be provided.
* @property {boolean|undefined} surface Write gml:Surface instead of
* gml:Polygon elements. This also affects the elements in multi-part
* geometries. Default is `false´. This only applies to GML version 3.

View File

@@ -28,8 +28,6 @@ goog.require('ol.proj');
ol.parser.ogc.GML = function(opt_options) {
var options = /** @type {ol.parser.GMLOptions} */
(goog.isDef(opt_options) ? opt_options : {});
this.axisOrientation = goog.isDef(options.axisOrientation) ?
options.axisOrientation : null;
this.extractAttributes = goog.isDef(options.extractAttributes) ?
options.extractAttributes : true;
this.surface = goog.isDef(options.surface) ?
@@ -40,20 +38,18 @@ ol.parser.ogc.GML = function(opt_options) {
options.multiCurve : true;
this.multiSurface = goog.isDef(options.multiSurface) ?
options.multiSurface : true;
this.srsName = goog.isDef(options.srsName) ?
options.srsName : null;
/**
* @private
* @protected
* @type {string|undefined}
*/
this.srsName_;
this.srsName;
/**
* @private
* @protected
* @type {string|undefined}
*/
this.axisOrientation_;
this.axisOrientation;
if (goog.isDef(options.schemaLocation)) {
this.schemaLocation = options.schemaLocation;
@@ -319,21 +315,21 @@ ol.parser.ogc.GML = function(opt_options) {
// TODO: Deal with GML documents that do not have the same SRS for all
// geometries.
var srsName;
if (!goog.isDef(this.srsName_)) {
if (!goog.isDef(this.srsName)) {
for (var i = node.childNodes.length - 1; i >= 0; --i) {
var child = node.childNodes[i];
if (child.nodeType == 1) {
srsName = child.getAttribute('srsName');
if (goog.isDef(srsName)) {
this.srsName_ = srsName;
this.srsName = srsName;
}
break;
}
}
}
if (!goog.isDef(this.axisOrientation_)) {
if (!goog.isDef(this.axisOrientation)) {
if (goog.isDef(srsName)) {
this.axisOrientation_ = ol.proj.get(srsName).getAxisOrientation();
this.axisOrientation = ol.proj.get(srsName).getAxisOrientation();
}
}
this.readChildNodes(node, obj);
@@ -349,9 +345,9 @@ ol.parser.ogc.GML = function(opt_options) {
}
this.writers = {
'http://www.opengis.net/gml': {
'featureMember': function(feature) {
'featureMember': function(obj) {
var node = this.createElementNS('gml:featureMember');
this.writeNode('_typeName', feature, this.featureNS, node);
this.writeNode('_typeName', obj, this.featureNS, node);
return node;
},
'MultiPoint': function(geometry) {
@@ -468,8 +464,9 @@ ol.parser.ogc.GML = function(opt_options) {
} 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);
if (goog.isDef(this.getSrsName())) {
this.setAttributeNS(child, null, 'srsName',
ol.proj.get(this.getSrsName()).getCode());
}
return node;
},
@@ -491,16 +488,16 @@ goog.inherits(ol.parser.ogc.GML, ol.parser.XML);
* @return {string?} Axis orientation.
*/
ol.parser.ogc.GML.prototype.getAxisOrientation = function() {
return goog.isDef(this.axisOrientation_) ? this.axisOrientation_ : 'enu';
return goog.isDef(this.axisOrientation) ? this.axisOrientation : 'enu';
};
/**
* @return {string?} SRS name.
* @return {string|undefined} SRS name.
*/
ol.parser.ogc.GML.prototype.getSrsName = function() {
return this.srsName_;
}
return this.srsName;
};
/**
@@ -514,13 +511,10 @@ ol.parser.ogc.GML.prototype.read = function(data) {
if (data && data.nodeType == 9) {
data = data.documentElement;
}
this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName;
this.axisOrientation_ = goog.isNull(this.axisOrientation) ?
undefined : this.axisOrientation;
var obj = /** @type {ol.parser.ReadFeaturesResult} */
({features: [], metadata: {}});
this.readNode(data, obj, true);
obj.metadata.projection = this.srsName_;
obj.metadata.projection = this.srsName;
return obj;
};

View File

@@ -3,6 +3,7 @@ goog.provide('ol.parser.ogc.GML_v2');
goog.require('goog.array');
goog.require('goog.object');
goog.require('ol.parser.ogc.GML');
goog.require('ol.proj');
@@ -112,17 +113,21 @@ 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.
* @param {ol.parser.ReadFeaturesResult} obj Object structure to write out as
* GML.
* @return {string} A string representing the GML document.
*/
ol.parser.ogc.GML_v2.prototype.write = function(obj) {
if (goog.isDef(obj.metadata)) {
this.srsName = goog.isDef(obj.metadata.projection) ?
ol.proj.get(obj.metadata.projection).getCode() : undefined;
}
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);
this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName;
this.axisOrientation_ = goog.isNull(this.axisOrientation) ?
undefined : this.axisOrientation;
return this.serialize(root);
var gml = this.serialize(root);
delete this.srsName;
return gml;
};

View File

@@ -4,6 +4,7 @@ goog.require('goog.array');
goog.require('goog.object');
goog.require('ol.geom.GeometryType');
goog.require('ol.parser.ogc.GML');
goog.require('ol.proj');
@@ -56,7 +57,8 @@ ol.parser.ogc.GML_v3 = function(opt_options) {
child = this.writeNode('MultiGeometry', geometry, null, node);
}
if (goog.isDef(this.getSrsName())) {
this.setAttributeNS(child, null, 'srsName', this.getSrsName());
this.setAttributeNS(child, null, 'srsName',
ol.proj.get(this.getSrsName()).getCode());
}
return node;
};
@@ -412,12 +414,15 @@ goog.inherits(ol.parser.ogc.GML_v3, ol.parser.ogc.GML);
* @return {string} An string representing the XML document.
*/
ol.parser.ogc.GML_v3.prototype.write = function(obj) {
if (goog.isDef(obj.metadata)) {
this.srsName = goog.isDef(obj.metadata.projection) ?
ol.proj.get(obj.metadata.projection).getCode() : undefined;
}
var root = this.writeNode('featureMembers', obj.features);
this.setAttributeNS(
root, 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation', this.schemaLocation);
this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName;
this.axisOrientation_ = goog.isNull(this.axisOrientation) ?
undefined : this.axisOrientation;
return this.serialize(root);
var gml = this.serialize(root);
delete this.srsName;
return gml;
};