diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index 7be66ae95a..04a6bd3518 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -16,6 +16,7 @@ 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'); @@ -649,7 +650,7 @@ ol.format.GML.readEnvelope_ = function(node, objectStack) { ol.format.GML.readFlatCoordinatesFromNode_ = function(node, objectStack) { goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); return /** @type {Array.} */ (ol.xml.pushParseAndPop( - node.getAttribute('srsDimension'), + null, ol.format.GML.GEOMETRY_FLAT_COORDINATES_PARSERS_, node, objectStack)); }; @@ -662,7 +663,15 @@ ol.format.GML.readFlatCoordinatesFromNode_ = function(node, objectStack) { ol.format.GML.readFlatPos_ = function(node) { var s = ol.xml.getAllTextContent(node, false).replace(/^\s*|\s*$/g, ''); var flatCoordinates = goog.array.map(s.split(/\s+/), parseFloat); - // TODO handle axis order + var containerSrs = node.parentNode.getAttribute('srsName'); + var axisOrientation = 'enu'; + if (containerSrs !== null) { + var proj = ol.proj.get(containerSrs); + axisOrientation = proj.getAxisOrientation(); + } + if (axisOrientation === 'neu') { + flatCoordinates = flatCoordinates.reverse(); + } var len = flatCoordinates.length; if (len == 2) { flatCoordinates.push(0); @@ -681,13 +690,19 @@ ol.format.GML.readFlatPos_ = function(node) { * @return {Array.|undefined} Flat coordinates. */ ol.format.GML.readFlatPosList_ = function(node, objectStack) { - var containerDimension = objectStack[objectStack.length - 1]; var s = ol.xml.getAllTextContent(node, false).replace(/^\s*|\s*$/g, ''); + var containerDimension = node.parentNode.getAttribute('srsDimension'); + var containerSrs = node.parentNode.getAttribute('srsName'); + var axisOrientation = 'enu'; + if (containerSrs !== null) { + 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 = parseInt(node.getAttribute('srsDimension') || node.getAttribute('dimension'), 10) || - (goog.isString(containerDimension)) ? + (containerDimension !== null) ? parseInt(containerDimension, 10) : 2; var x, y, z; var flatCoordinates = []; @@ -695,8 +710,11 @@ ol.format.GML.readFlatPosList_ = function(node, objectStack) { x = parseFloat(coords[i]); y = parseFloat(coords[i + 1]); z = (dim === 3) ? parseFloat(coords[i + 2]) : 0; - // TODO axis orientation - flatCoordinates.push(x, y, z); + if (axisOrientation === 'enu') { + flatCoordinates.push(x, y, z); + } else { + flatCoordinates.push(y, x, z); + } } return flatCoordinates; }; diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index 9daad10943..2d21e9da7d 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -13,7 +13,8 @@ describe('ol.format.GML', function() { it('can read a point geometry', function() { var text = - '' + + '' + ' 1 2' + ''; var g = format.readGeometry(text); @@ -28,7 +29,7 @@ describe('ol.format.GML', function() { it('can read a linestring geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' 1 2 3 4' + ''; var g = format.readGeometry(text); @@ -38,12 +39,38 @@ describe('ol.format.GML', function() { }); + describe('axis order', function() { + + it('can read a linestring geometry with correct axis order', function() { + var text = + '' + + ' -90 -180 90 180' + + ''; + var g = format.readGeometry(text); + expect(g).to.be.an(ol.geom.LineString); + expect(g.getCoordinates()).to.eql([[-180, -90, 0], [180, 90, 0]]); + }); + + it('can read a point geometry with correct axis order', function() { + var text = + '' + + ' -90 -180' + + ''; + var g = format.readGeometry(text); + expect(g).to.be.an(ol.geom.Point); + expect(g.getCoordinates()).to.eql([-180, -90, 0]); + }); + + }); + describe('linestring 3D', function() { it('can read a linestring 3D geometry', function() { var text = '' + + ' srsName="CRS:84" srsDimension="3">' + ' 1 2 3 4 5 6' + ''; var g = format.readGeometry(text); @@ -58,7 +85,7 @@ describe('ol.format.GML', function() { it('can read a linearring geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' 1 2 3 4 5 6 1 2' + ''; var g = format.readGeometry(text); @@ -74,7 +101,7 @@ describe('ol.format.GML', function() { it('can read a polygon geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' 1 2 3 2 3 4 1 2' + @@ -105,7 +132,7 @@ describe('ol.format.GML', function() { it('can read a surface geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' ' + @@ -140,7 +167,7 @@ describe('ol.format.GML', function() { it('can read a curve geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' 1 2 3 4' + @@ -159,7 +186,7 @@ describe('ol.format.GML', function() { it('can read an envelope geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' 1 2' + ' 3 4' + ''; @@ -174,7 +201,7 @@ describe('ol.format.GML', function() { it('can read a singular multipoint geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' 1 2' + @@ -199,7 +226,7 @@ describe('ol.format.GML', function() { it('can read a plural multipoint geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' 1 2' + @@ -224,7 +251,7 @@ describe('ol.format.GML', function() { it('can read a singular multilinestring geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' 1 2 2 3' + @@ -245,7 +272,7 @@ describe('ol.format.GML', function() { it('can read a plural multilinestring geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' 1 2 2 3' + @@ -268,7 +295,7 @@ describe('ol.format.GML', function() { it('can read a singular multipolygon geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' ' + @@ -310,7 +337,7 @@ describe('ol.format.GML', function() { it('can read a plural multipolygon geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' ' + @@ -354,7 +381,7 @@ describe('ol.format.GML', function() { it('can read a singular multicurve-linestring geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' 1 2 2 3' + @@ -375,7 +402,7 @@ describe('ol.format.GML', function() { it('can read a singular multicurve-curve geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' ' + @@ -408,7 +435,7 @@ describe('ol.format.GML', function() { it('can read a singular multisurface geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' ' + @@ -450,7 +477,7 @@ describe('ol.format.GML', function() { it('can read a plural multisurface geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' ' + @@ -492,7 +519,7 @@ describe('ol.format.GML', function() { it('can read a multisurface-surface geometry', function() { var text = '' + + ' srsName="CRS:84">' + ' ' + ' ' + ' ' +