diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index c4776c565e..2c921ec046 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -163,3 +163,46 @@ ol.geom.RawMultiLineString; * @typedef {Array.} */ ol.geom.RawMultiPolygon; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} coordinates Coordinates. + * @param {number} stride Stride. + * @return {number} offset Offset. + */ +ol.geom.deflateCoordinates = + function(flatCoordinates, offset, coordinates, stride) { + var i, ii; + for (i = 0, ii = coordinates.length; i < ii; ++i) { + var coordinate = coordinates[i]; + goog.asserts.assert(coordinate.length == stride); + var j; + for (j = 0; j < stride; ++j) { + flatCoordinates[offset++] = coordinate[j]; + } + } + return offset; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {Array.=} opt_coordinates Coordinates. + * @return {Array.} Coordinates. + */ +ol.geom.inflateCoordinates = + function(flatCoordinates, offset, end, stride, opt_coordinates) { + var coordinates = goog.isDef(opt_coordinates) ? opt_coordinates : []; + var i = 0; + var j; + for (j = offset; j < end; j += stride) { + coordinates[i++] = flatCoordinates.slice(j, j + stride); + } + coordinates.length = i; + return coordinates; +}; diff --git a/test/spec/ol/geom/geom.test.js b/test/spec/ol/geom/geom.test.js new file mode 100644 index 0000000000..c64a21ee83 --- /dev/null +++ b/test/spec/ol/geom/geom.test.js @@ -0,0 +1,31 @@ +goog.provide('ol.test.geom'); + + +describe('ol.geom', function() { + + describe('ol.geom.deflateCoordinates', function() { + + var flatCoordinates; + beforeEach(function() { + flatCoordinates = []; + }); + + it('flattens coordinates', function() { + var offset = ol.geom.deflateCoordinates( + flatCoordinates, 0, [[1, 2], [3, 4]], 2); + expect(offset).to.be(4); + expect(flatCoordinates).to.eql([1, 2, 3, 4]); + }); + + }); + + describe('ol.geom.inflateCoordinates', function() { + + it('inflates coordinates', function() { + var coordinates = ol.geom.inflateCoordinates([1, 2, 3, 4], 0, 4, 2); + expect(coordinates).to.eql([[1, 2], [3, 4]]); + }); + + }); + +});