diff --git a/src/ol/Graticule.js b/src/ol/Graticule.js index 431e9c12df..3235b67183 100644 --- a/src/ol/Graticule.js +++ b/src/ol/Graticule.js @@ -360,9 +360,13 @@ Graticule.prototype.getMeridianPoint_ = function(lineString, extent, index) { extent[1] + Math.abs(extent[1] - extent[3]) * this.lonLabelPosition_, clampedBottom, clampedTop); const coordinate = [flatCoordinates[0], lat]; - const point = this.meridiansLabels_[index] !== undefined ? - this.meridiansLabels_[index].geom : new Point(null); - point.setCoordinates(coordinate); + let point; + if (index in this.meridiansLabels_) { + point = this.meridiansLabels_[index]; + point.setCoordinates(coordinate); + } else { + point = new Point(coordinate); + } return point; }; @@ -408,9 +412,13 @@ Graticule.prototype.getParallelPoint_ = function(lineString, extent, index) { extent[0] + Math.abs(extent[0] - extent[2]) * this.latLabelPosition_, clampedLeft, clampedRight); const coordinate = [lon, flatCoordinates[1]]; - const point = this.parallelsLabels_[index] !== undefined ? - this.parallelsLabels_[index].geom : new Point(null); - point.setCoordinates(coordinate); + let point; + if (index in this.parallelsLabels_) { + point = this.parallelsLabels_[index]; + point.setCoordinates(coordinate); + } else { + point = new Point(coordinate); + } return point; }; diff --git a/src/ol/format/GMLBase.js b/src/ol/format/GMLBase.js index cf4858d093..03597cb395 100644 --- a/src/ol/format/GMLBase.js +++ b/src/ol/format/GMLBase.js @@ -291,9 +291,7 @@ GMLBase.prototype.readFeatureElement = function(node, objectStack) { GMLBase.prototype.readPoint = function(node, objectStack) { const flatCoordinates = this.readFlatCoordinatesFromNode_(node, objectStack); if (flatCoordinates) { - const point = new Point(null); - point.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates); - return point; + return new Point(flatCoordinates, GeometryLayout.XYZ); } }; diff --git a/src/ol/format/KML.js b/src/ol/format/KML.js index 4e0c3185fe..242fe65297 100644 --- a/src/ol/format/KML.js +++ b/src/ol/format/KML.js @@ -1144,8 +1144,7 @@ function readPoint(node, objectStack) { const flatCoordinates = readFlatCoordinatesFromNode(node, objectStack); if (flatCoordinates) { - const point = new Point(null); - point.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates); + const point = new Point(flatCoordinates, GeometryLayout.XYZ); point.setProperties(properties); return point; } else { diff --git a/src/ol/format/MVT.js b/src/ol/format/MVT.js index c111de9376..a4ff3766d3 100644 --- a/src/ol/format/MVT.js +++ b/src/ol/format/MVT.js @@ -339,14 +339,14 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) { geom = new Polygon(flatCoordinates, GeometryLayout.XY, ends); } } else { - geom = geometryType === GeometryType.POINT ? new Point(null) : + geom = geometryType === GeometryType.POINT ? new Point(flatCoordinates, GeometryLayout.XY) : geometryType === GeometryType.LINE_STRING ? new LineString(null) : geometryType === GeometryType.POLYGON ? new Polygon(flatCoordinates, GeometryLayout.XY, ends) : geometryType === GeometryType.MULTI_POINT ? new MultiPoint (null) : geometryType === GeometryType.MULTI_LINE_STRING ? new MultiLineString(null) : null; } - if (geometryType !== GeometryType.POLYGON) { + if (geometryType !== GeometryType.POLYGON && geometryType !== GeometryType.POINT) { geom.setFlatCoordinates(GeometryLayout.XY, flatCoordinates, ends); } feature = new this.featureClass_(); diff --git a/src/ol/geom/MultiPoint.js b/src/ol/geom/MultiPoint.js index c69e250c31..61fdab1f1f 100644 --- a/src/ol/geom/MultiPoint.js +++ b/src/ol/geom/MultiPoint.js @@ -105,10 +105,8 @@ MultiPoint.prototype.getPoint = function(index) { if (index < 0 || n <= index) { return null; } - const point = new Point(null); - point.setFlatCoordinates(this.layout, this.flatCoordinates.slice( - index * this.stride, (index + 1) * this.stride)); - return point; + return new Point(this.flatCoordinates.slice( + index * this.stride, (index + 1) * this.stride), this.layout); }; @@ -124,8 +122,7 @@ MultiPoint.prototype.getPoints = function() { /** @type {Array.} */ const points = []; for (let i = 0, ii = flatCoordinates.length; i < ii; i += stride) { - const point = new Point(null); - point.setFlatCoordinates(layout, flatCoordinates.slice(i, i + stride)); + const point = new Point(flatCoordinates.slice(i, i + stride), layout); points.push(point); } return points; diff --git a/src/ol/geom/Point.js b/src/ol/geom/Point.js index 88ac1f9907..dd1548189e 100644 --- a/src/ol/geom/Point.js +++ b/src/ol/geom/Point.js @@ -3,7 +3,6 @@ */ import {inherits} from '../util.js'; import {createOrUpdateFromCoordinate, containsXY} from '../extent.js'; -import GeometryLayout from '../geom/GeometryLayout.js'; import GeometryType from '../geom/GeometryType.js'; import SimpleGeometry from '../geom/SimpleGeometry.js'; import {deflateCoordinate} from '../geom/flat/deflate.js'; @@ -34,8 +33,7 @@ inherits(Point, SimpleGeometry); * @api */ Point.prototype.clone = function() { - const point = new Point(null); - point.setFlatCoordinates(this.layout, this.flatCoordinates.slice()); + const point = new Point(this.flatCoordinates.slice(), this.layout); return point; }; @@ -101,26 +99,13 @@ Point.prototype.intersectsExtent = function(extent) { * @api */ Point.prototype.setCoordinates = function(coordinates, opt_layout) { - if (!coordinates) { - this.setFlatCoordinates(GeometryLayout.XY, null); - } else { - this.setLayout(opt_layout, coordinates, 0); - if (!this.flatCoordinates) { - this.flatCoordinates = []; - } - this.flatCoordinates.length = deflateCoordinate( - this.flatCoordinates, 0, coordinates, this.stride); - this.changed(); + this.setLayout(opt_layout, coordinates, 0); + if (!this.flatCoordinates) { + this.flatCoordinates = []; } -}; - - -/** - * @param {module:ol/geom/GeometryLayout} layout Layout. - * @param {Array.} flatCoordinates Flat coordinates. - */ -Point.prototype.setFlatCoordinates = function(layout, flatCoordinates) { - this.setFlatCoordinatesInternal(layout, flatCoordinates); + this.flatCoordinates.length = deflateCoordinate( + this.flatCoordinates, 0, coordinates, this.stride); this.changed(); }; + export default Point; diff --git a/src/ol/geom/SimpleGeometry.js b/src/ol/geom/SimpleGeometry.js index d8849f057e..41ed58def6 100644 --- a/src/ol/geom/SimpleGeometry.js +++ b/src/ol/geom/SimpleGeometry.js @@ -214,7 +214,7 @@ SimpleGeometry.prototype.setFlatCoordinatesInternal = function(layout, flatCoord /** * @abstract - * @param {Array} coordinates Coordinates. + * @param {!Array} coordinates Coordinates. * @param {module:ol/geom/GeometryLayout=} opt_layout Layout. */ SimpleGeometry.prototype.setCoordinates = function(coordinates, opt_layout) {}; diff --git a/test/spec/ol/geom/point.test.js b/test/spec/ol/geom/point.test.js index 57b1971846..0f9e4e9aa6 100644 --- a/test/spec/ol/geom/point.test.js +++ b/test/spec/ol/geom/point.test.js @@ -3,10 +3,10 @@ import Point from '../../../../src/ol/geom/Point.js'; describe('ol.geom.Point', function() { - it('can be constructed with a null geometry', function() { + it('cannot be constructed with a null geometry', function() { expect(function() { return new Point(null); - }).not.to.throwException(); + }).to.throwException(); }); describe('construct with 2D coordinates', function() {