diff --git a/examples/gml.js b/examples/gml.js index a1dc12929c..6c4aef9c9c 100644 --- a/examples/gml.js +++ b/examples/gml.js @@ -41,7 +41,7 @@ var map = new ol.Map({ }) }); -var gml = new ol.parser.ogc.GML_v3({xy: false}); +var gml = new ol.parser.ogc.GML_v3(); var url = 'data/gml/topp-states-wfs.xml'; var xhr = new XMLHttpRequest(); diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index 1402090553..349e3ca1a9 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -314,8 +314,8 @@ * @property {Array.|string|undefined} featureType The local * (without prefix) feature typeName(s). * @property {string|undefined} geometryName Geometry element name. - * @property {boolean|undefined} xy Order of the GML coordinate true:(x,y) or - * false:(y,x). Default is `true`. + * @property {string|undefined} axisOrientation The axis orientation as + * specified in Proj4. The default is 'enu'. */ /** @@ -327,8 +327,8 @@ * @property {Array.|string|undefined} featureType The local * (without prefix) feature typeName(s). * @property {string|undefined} geometryName Geometry element name. - * @property {boolean|undefined} xy Order of the GML coordinate true:(x,y) or - * false:(y,x). Default is `true`. + * @property {string|undefined} axisOrientation The axis orientation as + * specified in Proj4. The default is 'enu'. * @property {boolean|undefined} surface Write gml:Surface instead of * gml:Polygon elements. This also affects the elements in multi-part * geometries. Default is `false“. diff --git a/src/ol/layer/vectorlayer.js b/src/ol/layer/vectorlayer.js index fd5f022f46..39ce1bf209 100644 --- a/src/ol/layer/vectorlayer.js +++ b/src/ol/layer/vectorlayer.js @@ -349,24 +349,26 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) { this.addFeatures(features); }; + var options = {callback: callback, projection: + this.getSource().getProjection()}; if (goog.isString(data)) { if (goog.isFunction(parser.readFeaturesFromStringAsync)) { parser.readFeaturesFromStringAsync(data, goog.bind(addFeatures, this), - {callback: callback}); + options); } else { goog.asserts.assert(goog.isFunction(parser.readFeaturesFromString), 'Expected a parser with readFeaturesFromString method.'); - features = parser.readFeaturesFromString(data, {callback: callback}); + features = parser.readFeaturesFromString(data, options); addFeatures.call(this, features); } } else if (goog.isObject(data)) { if (goog.isFunction(parser.readFeaturesFromObjectAsync)) { parser.readFeaturesFromObjectAsync(data, goog.bind(addFeatures, this), - {callback: callback}); + options); } else { goog.asserts.assert(goog.isFunction(parser.readFeaturesFromObject), 'Expected a parser with a readFeaturesFromObject method.'); - features = parser.readFeaturesFromObject(data, {callback: callback}); + features = parser.readFeaturesFromObject(data, options); addFeatures.call(this, features); } } else { diff --git a/src/ol/parser/featureparser.js b/src/ol/parser/featureparser.js index 9313e23288..e6d2e85584 100644 --- a/src/ol/parser/featureparser.js +++ b/src/ol/parser/featureparser.js @@ -97,6 +97,7 @@ ol.parser.ReadFeaturesCallback; /** - * @typedef {{callback: ol.parser.ReadFeaturesCallback}} + * @typedef {{callback: ol.parser.ReadFeaturesCallback, + * projection: ol.Projection}} */ ol.parser.ReadFeaturesOptions; diff --git a/src/ol/parser/ogc/gml.js b/src/ol/parser/ogc/gml.js index 8537943df2..a9fa765c79 100644 --- a/src/ol/parser/ogc/gml.js +++ b/src/ol/parser/ogc/gml.js @@ -29,8 +29,8 @@ 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.axisOrientation)) { + this.axisOrientation = 'enu'; } if (!goog.isDef(this.extractAttributes)) { this.extractAttributes = true; @@ -182,7 +182,7 @@ ol.parser.ogc.GML = function(opt_options) { var points = new Array(numPoints); for (var i = 0; i < numPoints; ++i) { coords = pointList[i].split(cs).map(parseFloat); - if (this.xy) { + if (this.axisOrientation.substr(0, 2) === 'en') { points[i] = coords; } else { if (coords.length === 2) { @@ -552,5 +552,8 @@ ol.parser.ogc.GML.prototype.createGeometry_ = function(container, ol.parser.ogc.GML.prototype.readFeaturesFromString = function(str, opt_options) { this.readFeaturesOptions_ = opt_options; + if (goog.isDef(opt_options) && goog.isDef(opt_options.projection)) { + this.axisOrientation = opt_options.projection.getAxisOrientation(); + } return this.read(str).features; }; diff --git a/src/ol/parser/ogc/gml_v2.js b/src/ol/parser/ogc/gml_v2.js index b4f76f9179..aae2a0238d 100644 --- a/src/ol/parser/ogc/gml_v2.js +++ b/src/ol/parser/ogc/gml_v2.js @@ -1,6 +1,6 @@ goog.provide('ol.parser.ogc.GML_v2'); -goog.require('goog.dom.xml'); +goog.require('goog.array'); goog.require('goog.object'); goog.require('ol.parser.ogc.GML'); @@ -44,18 +44,14 @@ ol.parser.ogc.GML_v2 = function(opt_options) { 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]; + var part = goog.array.concat(coord); + if (this.axisOrientation.substr(0, 2) !== 'en') { + part[0] = coord[1]; + part[1] = coord[0]; } - if (coord.length === 3) { - str += ',' + coord[2]; - } - parts[i] = str; + parts[i] = part.join(','); } - var value = (numCoordinates === 1) ? parts[0] : parts.join(' '); + var value = parts.join(' '); var node = this.createElementNS('gml:coordinates'); this.setAttributeNS(node, null, 'decimal', '.'); this.setAttributeNS(node, null, 'cs', ','); diff --git a/src/ol/parser/ogc/gml_v3.js b/src/ol/parser/ogc/gml_v3.js index 2b389628b8..86a10eca20 100644 --- a/src/ol/parser/ogc/gml_v3.js +++ b/src/ol/parser/ogc/gml_v3.js @@ -112,7 +112,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) { var str = this.getChildValue(node).replace( this.regExes.trimSpace, ''); var coords = str.split(this.regExes.splitSpace).map(parseFloat); - if (this.xy) { + if (this.axisOrientation.substr(0, 2) === 'en') { obj.push([coords]); } else { if (coords.length === 2) { @@ -136,14 +136,15 @@ 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'; if (dim === 3) { - if (this.xy) { + if (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) { + if (xy) { points[i / dim] = [x, y]; } else { points[i / dim] = [y, x]; @@ -248,7 +249,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) { 'pos': function(point) { // only 2d for simple features profile var pos; - if (this.xy) { + if (this.axisOrientation.substr(0, 2) === 'en') { pos = (point[0] + ' ' + point[1]); } else { pos = (point[1] + ' ' + point[0]); @@ -284,7 +285,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) { var point; for (var i = 0; i < len; ++i) { point = points[i]; - if (this.xy) { + if (this.axisOrientation.substr(0, 2) === 'en') { parts[i] = point[0] + ' ' + point[1]; } else { parts[i] = point[1] + ' ' + point[0]; @@ -383,7 +384,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) { 'lowerCorner': function(bounds) { // only 2d for simple features profile var pos; - if (this.xy) { + if (this.axisOrientation.substr(0, 2) === 'en') { pos = (bounds.left + ' ' + bounds.bottom); } else { pos = (bounds.bottom + ' ' + bounds.left); @@ -395,7 +396,7 @@ ol.parser.ogc.GML_v3 = function(opt_options) { 'upperCorner': function(bounds) { // only 2d for simple features profile var pos; - if (this.xy) { + if (this.axisOrientation.substr(0, 2) === 'en') { pos = (bounds.right + ' ' + bounds.top); } else { pos = (bounds.top + ' ' + bounds.right);