diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index fb5280c7cf..d66fc9d95d 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -60,7 +60,7 @@ ol.geom.Geometry = function() { * @protected * @type {Array.} */ - this.flatCoordinates = []; + this.flatCoordinates = null; /** * @protected @@ -201,6 +201,19 @@ ol.geom.Geometry.prototype.getStride = function() { ol.geom.Geometry.prototype.getType = goog.abstractMethod; +/** + * @param {ol.geom.Layout} layout Layout. + * @param {Array.} flatCoordinates Flat coordinates. + * @protected + */ +ol.geom.Geometry.prototype.setFlatCoordinatesInternal = + function(layout, flatCoordinates) { + this.stride = ol.geom.Geometry.getStrideForLayout_(layout); + this.layout = layout; + this.flatCoordinates = flatCoordinates; +}; + + /** * @param {ol.geom.Layout|undefined} layout Layout. * @param {Array} coordinates Coordinates. @@ -236,7 +249,9 @@ ol.geom.Geometry.prototype.setLayout = * @param {ol.TransformFunction} transformFn Transform. */ ol.geom.Geometry.prototype.transform = function(transformFn) { - transformFn(this.flatCoordinates, this.flatCoordinates, this.stride); + if (!goog.isNull(this.flatCoordinates)) { + transformFn(this.flatCoordinates, this.flatCoordinates, this.stride); + } }; @@ -291,7 +306,11 @@ ol.geom.RawMultiPolygon; */ ol.geom.transformGeometry2D = function(geometry, transform, opt_dest) { var flatCoordinates = geometry.getFlatCoordinates(); - var stride = geometry.getStride(); - return ol.geom.flat.transform2D( - flatCoordinates, stride, transform, opt_dest); + if (goog.isNull(flatCoordinates)) { + return null; + } else { + var stride = geometry.getStride(); + return ol.geom.flat.transform2D( + flatCoordinates, stride, transform, opt_dest); + } }; diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 0a76be1014..40bfd7c109 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -50,8 +50,26 @@ ol.geom.LineString.prototype.getType = function() { */ ol.geom.LineString.prototype.setCoordinates = function(coordinates, opt_layout) { - this.setLayout(opt_layout, coordinates, 1); - ol.geom.flat.deflateCoordinates( - this.flatCoordinates, 0, coordinates, this.stride); + if (goog.isNull(coordinates)) { + this.setFlatCoordinates(ol.geom.Layout.XY, null); + } else { + this.setLayout(opt_layout, coordinates, 1); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = []; + } + this.flatCoordinates.length = ol.geom.flat.deflateCoordinates( + this.flatCoordinates, 0, coordinates, this.stride); + this.dispatchChangeEvent(); + } +}; + + +/** + * @param {ol.geom.Layout} layout Layout. + * @param {Array.} flatCoordinates Flat coordinates. + */ +ol.geom.LineString.prototype.setFlatCoordinates = + function(layout, flatCoordinates) { + this.setFlatCoordinatesInternal(layout, flatCoordinates); this.dispatchChangeEvent(); }; diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index 8611641d02..da4a7c2505 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -74,8 +74,29 @@ ol.geom.MultiLineString.prototype.getType = function() { */ ol.geom.MultiLineString.prototype.setCoordinates = function(coordinates, opt_layout) { - this.setLayout(opt_layout, coordinates, 2); - ol.geom.flat.deflateCoordinatess( - this.flatCoordinates, 0, coordinates, this.stride, this.ends_); + if (goog.isNull(coordinates)) { + this.setFlatCoordinates(ol.geom.Layout.XY, null, this.ends_); + } else { + this.setLayout(opt_layout, coordinates, 2); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = []; + } + var ends = ol.geom.flat.deflateCoordinatess( + this.flatCoordinates, 0, coordinates, this.stride, this.ends_); + this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1]; + this.dispatchChangeEvent(); + } +}; + + +/** + * @param {ol.geom.Layout} layout Layout. + * @param {Array.} flatCoordinates Flat coordinates. + * @param {Array.} ends Ends. + */ +ol.geom.MultiLineString.prototype.setFlatCoordinates = + function(layout, flatCoordinates, ends) { + this.setFlatCoordinatesInternal(layout, flatCoordinates); + this.ends_ = ends; this.dispatchChangeEvent(); }; diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index 930ae9041d..525f720720 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -57,8 +57,26 @@ ol.geom.MultiPoint.prototype.getType = function() { */ ol.geom.MultiPoint.prototype.setCoordinates = function(coordinates, opt_layout) { - this.setLayout(opt_layout, coordinates, 1); - ol.geom.flat.deflateCoordinates( - this.flatCoordinates, 0, coordinates, this.stride); + if (goog.isNull(coordinates)) { + this.setFlatCoordinates(ol.geom.Layout.XY, null); + } else { + this.setLayout(opt_layout, coordinates, 1); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = []; + } + this.flatCoordinates.length = ol.geom.flat.deflateCoordinates( + this.flatCoordinates, 0, coordinates, this.stride); + this.dispatchChangeEvent(); + } +}; + + +/** + * @param {ol.geom.Layout} layout Layout. + * @param {Array.} flatCoordinates Flat coordinates. + */ +ol.geom.MultiPoint.prototype.setFlatCoordinates = + function(layout, flatCoordinates) { + this.setFlatCoordinatesInternal(layout, flatCoordinates); this.dispatchChangeEvent(); }; diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 55b7bc2052..e4fbdb3d87 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -119,10 +119,33 @@ ol.geom.MultiPolygon.prototype.getType = function() { */ ol.geom.MultiPolygon.prototype.setCoordinates = function(coordinates, opt_layout) { - this.setLayout(opt_layout, coordinates, 3); - ol.geom.flat.deflateCoordinatesss( - this.flatCoordinates, 0, coordinates, this.stride, this.endss_); - ol.geom.flat.orientLinearRingss( - this.flatCoordinates, 0, this.endss_, this.stride); + if (goog.isNull(coordinates)) { + this.setFlatCoordinates(ol.geom.Layout.XY, null, this.endss_); + } else { + this.setLayout(opt_layout, coordinates, 3); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = []; + } + var endss = ol.geom.flat.deflateCoordinatesss( + this.flatCoordinates, 0, coordinates, this.stride, this.endss_); + var lastEnds = endss[endss.length - 1]; + this.flatCoordinates.length = lastEnds.length === 0 ? + 0 : lastEnds[lastEnds.length - 1]; + ol.geom.flat.orientLinearRingss( + this.flatCoordinates, 0, this.endss_, this.stride); + this.dispatchChangeEvent(); + } +}; + + +/** + * @param {ol.geom.Layout} layout Layout. + * @param {Array.} flatCoordinates Flat coordinates. + * @param {Array.>} endss Endss. + */ +ol.geom.MultiPolygon.prototype.setFlatCoordinates = + function(layout, flatCoordinates, endss) { + this.setFlatCoordinatesInternal(layout, flatCoordinates); + this.endss_ = endss; this.dispatchChangeEvent(); }; diff --git a/src/ol/geom/point.js b/src/ol/geom/point.js index 7d660b92b6..a458ce2160 100644 --- a/src/ol/geom/point.js +++ b/src/ol/geom/point.js @@ -3,6 +3,7 @@ goog.provide('ol.geom.Point'); goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.Geometry'); +goog.require('ol.geom.flat'); @@ -54,7 +55,25 @@ ol.geom.Point.prototype.getType = function() { * @param {ol.geom.Layout=} opt_layout Layout. */ ol.geom.Point.prototype.setCoordinates = function(coordinates, opt_layout) { - this.setLayout(opt_layout, coordinates, 0); - this.flatCoordinates = coordinates; + if (goog.isNull(coordinates)) { + this.setFlatCoordinates(ol.geom.Layout.XY, null); + } else { + this.setLayout(opt_layout, coordinates, 0); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = []; + } + this.flatCoordinates.length = ol.geom.flat.deflateCoordinate( + this.flatCoordinates, 0, coordinates, this.stride); + this.dispatchChangeEvent(); + } +}; + + +/** + * @param {ol.geom.Layout} layout Layout. + * @param {Array.} flatCoordinates Flat coordinates. + */ +ol.geom.Point.prototype.setFlatCoordinates = function(layout, flatCoordinates) { + this.setFlatCoordinatesInternal(layout, flatCoordinates); this.dispatchChangeEvent(); }; diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 2c4a732f4b..039384ac07 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -117,10 +117,31 @@ ol.geom.Polygon.prototype.getType = function() { * @param {ol.geom.Layout=} opt_layout Layout. */ ol.geom.Polygon.prototype.setCoordinates = function(coordinates, opt_layout) { - this.setLayout(opt_layout, coordinates, 2); - ol.geom.flat.deflateCoordinatess( - this.flatCoordinates, 0, coordinates, this.stride, this.ends_); - ol.geom.flat.orientLinearRings( - this.flatCoordinates, 0, this.ends_, this.stride); + if (goog.isNull(coordinates)) { + this.setFlatCoordinates(ol.geom.Layout.XY, null, this.ends_); + } else { + this.setLayout(opt_layout, coordinates, 2); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = []; + } + var ends = ol.geom.flat.deflateCoordinatess( + this.flatCoordinates, 0, coordinates, this.stride, this.ends_); + this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1]; + ol.geom.flat.orientLinearRings( + this.flatCoordinates, 0, this.ends_, this.stride); + this.dispatchChangeEvent(); + } +}; + + +/** + * @param {ol.geom.Layout} layout Layout. + * @param {Array.} flatCoordinates Flat coordinates. + * @param {Array.} ends Ends. + */ +ol.geom.Polygon.prototype.setFlatCoordinates = + function(layout, flatCoordinates, ends) { + this.setFlatCoordinatesInternal(layout, flatCoordinates); + this.ends_ = ends; this.dispatchChangeEvent(); }; diff --git a/test/spec/ol/geom/linestring.test.js b/test/spec/ol/geom/linestring.test.js index b16b628a57..0c0b510580 100644 --- a/test/spec/ol/geom/linestring.test.js +++ b/test/spec/ol/geom/linestring.test.js @@ -3,6 +3,13 @@ goog.provide('ol.test.geom.LineString'); describe('ol.geom.LineString', function() { + it('can be constructed with a null geometry', function() { + expect(function() { + var lineString = new ol.geom.LineString(null); + lineString = lineString; // suppress gjslint warning + }).not.to.throwException(); + }); + describe('construct empty', function() { var lineString; diff --git a/test/spec/ol/geom/multilinestring.test.js b/test/spec/ol/geom/multilinestring.test.js index e6021aea9d..2fa422db75 100644 --- a/test/spec/ol/geom/multilinestring.test.js +++ b/test/spec/ol/geom/multilinestring.test.js @@ -3,6 +3,13 @@ goog.provide('ol.test.geom.MultiLineString'); describe('ol.geom.MultiLineString', function() { + it('can be constructed with a null geometry', function() { + expect(function() { + var multiLineString = new ol.geom.MultiLineString(null); + multiLineString = multiLineString; // suppress gjslint warning + }).not.to.throwException(); + }); + describe('construct empty', function() { var multiLineString; diff --git a/test/spec/ol/geom/multipoint.test.js b/test/spec/ol/geom/multipoint.test.js index 18065bf18f..304e834159 100644 --- a/test/spec/ol/geom/multipoint.test.js +++ b/test/spec/ol/geom/multipoint.test.js @@ -3,6 +3,13 @@ goog.provide('ol.test.geom.MultiPoint'); describe('ol.geom.MultiPoint', function() { + it('can be constructed with a null geometry', function() { + expect(function() { + var multiPoint = new ol.geom.MultiPoint(null); + multiPoint = multiPoint; // suppress gjslint warning + }).not.to.throwException(); + }); + describe('construct empty', function() { var multiPoint; diff --git a/test/spec/ol/geom/multipolygon.test.js b/test/spec/ol/geom/multipolygon.test.js new file mode 100644 index 0000000000..92badfa88a --- /dev/null +++ b/test/spec/ol/geom/multipolygon.test.js @@ -0,0 +1,16 @@ +goog.provide('ol.test.geom.MultiPolygon'); + + +describe('ol.geom.MultiPolygon', function() { + + it('can be constructed with a null geometry', function() { + expect(function() { + var multiPolygon = new ol.geom.MultiPolygon(null); + multiPolygon = multiPolygon; // suppress gjslint warning + }).not.to.throwException(); + }); + +}); + + +goog.require('ol.geom.MultiPolygon'); diff --git a/test/spec/ol/geom/point.test.js b/test/spec/ol/geom/point.test.js index cea7644fe7..74ff2afad0 100644 --- a/test/spec/ol/geom/point.test.js +++ b/test/spec/ol/geom/point.test.js @@ -3,6 +3,13 @@ goog.provide('ol.test.geom.Point'); describe('ol.geom.Point', function() { + it('can be constructed with a null geometry', function() { + expect(function() { + var point = new ol.geom.Point(null); + point = point; // suppress gjslint warning + }).not.to.throwException(); + }); + describe('construct with 2D coordinates', function() { var point; diff --git a/test/spec/ol/geom/polygon.test.js b/test/spec/ol/geom/polygon.test.js index 0be8b0734a..bd53d9f2a0 100644 --- a/test/spec/ol/geom/polygon.test.js +++ b/test/spec/ol/geom/polygon.test.js @@ -5,6 +5,13 @@ goog.provide('ol.test.geom.Polygon'); describe('ol.geom.Polygon', function() { + it('can be constructed with a null geometry', function() { + expect(function() { + var polygon = new ol.geom.Polygon(null); + polygon = polygon; // suppress gjslint warning + }).not.to.throwException(); + }); + describe('construct empty', function() { var polygon;