Get projection and axis order from GML data

To make tests pass, the winding order of the coordinates in the
test data had to be reversed to conform to common gml practice.
This commit is contained in:
ahocevar
2013-07-24 22:42:49 +02:00
parent 109ec71877
commit 56e8b1cfb4
8 changed files with 102 additions and 28 deletions

View File

@@ -357,7 +357,9 @@
/**
* @typedef {Object} ol.parser.GMLOptions
* @property {string|undefined} axisOrientation The axis orientation as
* specified in Proj4. The default is 'enu'.
* specified in Proj4. If this is not provided, 'enu' will be used for GML2,
* and the projection's axis orientation for GML3, with a fallback to 'enu'
* if `projection` was not configured and is not provided by the data.
* @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.
@@ -381,7 +383,8 @@
* @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 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

@@ -14,6 +14,7 @@ goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.parser.StringFeatureParser');
goog.require('ol.parser.XML');
goog.require('ol.proj');
@@ -41,6 +42,20 @@ ol.parser.ogc.GML = function(opt_options) {
options.multiSurface : true;
this.srsName = goog.isDef(options.srsName) ?
options.srsName : null;
/**
* @private
* @type {string|undefined}
*/
this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName;
/**
* @private
* @type {string|undefined}
*/
this.axisOrientation_ = goog.isNull(this.srsName) ? undefined :
ol.proj.get(this.srsName).getAxisOrientation();
if (goog.isDef(options.schemaLocation)) {
this.schemaLocation = options.schemaLocation;
}
@@ -197,7 +212,7 @@ ol.parser.ogc.GML = function(opt_options) {
var points = new Array(numPoints);
for (var i = 0; i < numPoints; ++i) {
coords = goog.array.map(pointList[i].split(cs), parseFloat);
if (this.axisOrientation.substr(0, 2) === 'en') {
if (this.getAxisOrientation().substr(0, 2) === 'en') {
points[i] = coords;
} else {
if (coords.length === 2) {
@@ -302,6 +317,27 @@ ol.parser.ogc.GML = function(opt_options) {
if (!this.geometryName) {
this.geometryName = node.nodeName.split(':').pop();
}
// TODO: Deal with GML documents that do not have the same SRS for all
// geometries.
if (!goog.isDef(this.srsName_)) {
for (var i = node.childNodes.length - 1; i >= 0; --i) {
var child = node.childNodes[i];
if (child.nodeType == 1) {
var srsName = child.getAttribute('srsName');
if (goog.isDef(srsName)) {
this.srsName_ = srsName.replace(ol.parser.ogc.GML.regExes.epsg,
'EPSG:');
if (!goog.isDef(this.axisOrientation_)) {
var projection = ol.proj.get(this.srsName_);
if (!goog.isNull(projection)) {
this.axisOrientation_ = projection.getAxisOrientation();
}
}
}
break;
}
}
}
this.readChildNodes(node, obj);
},
'_attribute': function(node, obj) {
@@ -453,6 +489,26 @@ ol.parser.ogc.GML = function(opt_options) {
goog.inherits(ol.parser.ogc.GML, ol.parser.XML);
/**
* Constants for regExes.
* @enum {RegExp}
*/
ol.parser.ogc.GML.regExes = {
epsg: new RegExp('(http:\/\/www\.opengis\.net\/gml\/srs\/epsg.xml#|' +
'urn:x-ogc:def:crs:EPSG:)', 'i')
};
/**
* @return {string?} Axis orientation of the data that is currently being
* parsed.
*/
ol.parser.ogc.GML.prototype.getAxisOrientation = function() {
return goog.isDef(this.axisOrientation_) ?
this.axisOrientation_ : this.axisOrientation;
};
/**
* @param {string|Document|Element|Object} data Data to read.
* @return {ol.parser.ReadFeaturesResult} An object representing the document.
@@ -464,8 +520,12 @@ ol.parser.ogc.GML.prototype.read = function(data) {
if (data && data.nodeType == 9) {
data = data.documentElement;
}
var obj = {features: [], metadata: {projection: null}};
var obj = /** @type {ol.parser.ReadFeaturesResult} */
({features: [], metadata: {}});
this.readNode(data, obj, true);
obj.metadata.projection = this.srsName_;
this.srsName_ = goog.isNull(this.srsName) ? undefined : this.srsName;
delete this.axisOrientation_;
return obj;
};

View File

@@ -111,6 +111,14 @@ ol.parser.ogc.GML_v2 = function(opt_options) {
goog.inherits(ol.parser.ogc.GML_v2, ol.parser.ogc.GML);
/**
* @return {string?} Axis orientation that was configured with this instance.
*/
ol.parser.ogc.GML_v2.prototype.getAxisOrientation = function() {
return this.axisOrientation;
};
/**
* @param {Object} obj Object structure to write out as XML.
* @return {string} An string representing the XML document.

View File

@@ -101,7 +101,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) {
this.regExes.trimSpace, '');
var coords = goog.array.map(str.split(this.regExes.splitSpace),
parseFloat);
if (this.axisOrientation.substr(0, 2) === 'en') {
if (this.getAxisOrientation().substr(0, 2) === 'en') {
obj.push([coords]);
} else {
if (coords.length === 2) {
@@ -125,7 +125,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) {
for (var i = 0, ii = coords.length; i < ii; i += dim) {
x = parseFloat(coords[i]);
y = parseFloat(coords[i + 1]);
var xy = this.axisOrientation.substr(0, 2) === 'en';
var xy = this.getAxisOrientation().substr(0, 2) === 'en';
if (dim === 3) {
if (xy) {
points[i / dim] = [x, y, parseFloat(coords[i + 2])];
@@ -393,7 +393,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) {
'upperCorner': function(bounds) {
// only 2d for simple features profile
var pos;
if (this.axisOrientation.substr(0, 2) === 'en') {
if (this.axisOrientation().substr(0, 2) === 'en') {
pos = (bounds.right + ' ' + bounds.top);
} else {
pos = (bounds.top + ' ' + bounds.right);