From 3871f7785a66f6fe067bec1b969fbcae3249d258 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Sat, 7 Jul 2018 16:26:24 +0200 Subject: [PATCH] Accept flat coordinates for MultiPoint constructor --- src/ol/format/KML.js | 3 +-- src/ol/format/MVT.js | 5 ++-- src/ol/geom/MultiPoint.js | 40 +++++++++++----------------- src/ol/geom/MultiPolygon.js | 5 +--- test/spec/ol/geom/multipoint.test.js | 4 +-- 5 files changed, 22 insertions(+), 35 deletions(-) diff --git a/src/ol/format/KML.js b/src/ol/format/KML.js index ead216af48..3e682105d0 100644 --- a/src/ol/format/KML.js +++ b/src/ol/format/KML.js @@ -1104,8 +1104,7 @@ function readMultiGeometry(node, objectStack) { geometry = geometries[i]; extend(flatCoordinates, geometry.getFlatCoordinates()); } - multiGeometry = new MultiPoint(null); - multiGeometry.setFlatCoordinates(layout, flatCoordinates); + multiGeometry = new MultiPoint(flatCoordinates, layout); setCommonGeometryProperties(multiGeometry, geometries); } else if (type == GeometryType.LINE_STRING) { multiGeometry = new MultiLineString(null); diff --git a/src/ol/format/MVT.js b/src/ol/format/MVT.js index 701f4675f3..3b7c02badf 100644 --- a/src/ol/format/MVT.js +++ b/src/ol/format/MVT.js @@ -342,11 +342,12 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) { geom = geometryType === GeometryType.POINT ? new Point(flatCoordinates, GeometryLayout.XY) : geometryType === GeometryType.LINE_STRING ? new LineString(flatCoordinates, GeometryLayout.XY) : geometryType === GeometryType.POLYGON ? new Polygon(flatCoordinates, GeometryLayout.XY, ends) : - geometryType === GeometryType.MULTI_POINT ? new MultiPoint (null) : + geometryType === GeometryType.MULTI_POINT ? new MultiPoint(flatCoordinates, GeometryLayout.XY) : geometryType === GeometryType.MULTI_LINE_STRING ? new MultiLineString(null) : null; } - if (geometryType !== GeometryType.POLYGON && geometryType !== GeometryType.LINE_STRING && geometryType !== GeometryType.POINT) { + if (geometryType !== GeometryType.POLYGON && geometryType !== GeometryType.LINE_STRING && + geometryType !== GeometryType.MULTI_POINT && 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 61fdab1f1f..44f7010f89 100644 --- a/src/ol/geom/MultiPoint.js +++ b/src/ol/geom/MultiPoint.js @@ -4,7 +4,6 @@ import {inherits} from '../util.js'; import {extend} from '../array.js'; import {closestSquaredDistanceXY, containsXY} from '../extent.js'; -import GeometryLayout from '../geom/GeometryLayout.js'; import GeometryType from '../geom/GeometryType.js'; import Point from '../geom/Point.js'; import SimpleGeometry from '../geom/SimpleGeometry.js'; @@ -18,13 +17,19 @@ import {squaredDistance as squaredDx} from '../math.js'; * * @constructor * @extends {module:ol/geom/SimpleGeometry} - * @param {Array.} coordinates Coordinates. + * @param {Array.|Array.} coordinates + * Coordinates. (For internal use, flat coordinates in combination with + * `opt_layout` are also accepted) * @param {module:ol/geom/GeometryLayout=} opt_layout Layout. * @api */ const MultiPoint = function(coordinates, opt_layout) { SimpleGeometry.call(this); - this.setCoordinates(coordinates, opt_layout); + if (opt_layout && !Array.isArray(coordinates[0])) { + this.setFlatCoordinatesInternal(opt_layout, coordinates); + } else { + this.setCoordinates(coordinates, opt_layout); + } }; inherits(MultiPoint, SimpleGeometry); @@ -52,8 +57,7 @@ MultiPoint.prototype.appendPoint = function(point) { * @api */ MultiPoint.prototype.clone = function() { - const multiPoint = new MultiPoint(null); - multiPoint.setFlatCoordinates(this.layout, this.flatCoordinates.slice()); + const multiPoint = new MultiPoint(this.flatCoordinates.slice(), this.layout); return multiPoint; }; @@ -158,32 +162,18 @@ MultiPoint.prototype.intersectsExtent = function(extent) { /** * Set the coordinates of the multipoint. - * @param {Array.} coordinates Coordinates. + * @param {!Array.} coordinates Coordinates. * @param {module:ol/geom/GeometryLayout=} opt_layout Layout. * @override * @api */ MultiPoint.prototype.setCoordinates = function(coordinates, opt_layout) { - if (!coordinates) { - this.setFlatCoordinates(GeometryLayout.XY, null); - } else { - this.setLayout(opt_layout, coordinates, 1); - if (!this.flatCoordinates) { - this.flatCoordinates = []; - } - this.flatCoordinates.length = deflateCoordinates( - this.flatCoordinates, 0, coordinates, this.stride); - this.changed(); + this.setLayout(opt_layout, coordinates, 1); + if (!this.flatCoordinates) { + this.flatCoordinates = []; } -}; - - -/** - * @param {module:ol/geom/GeometryLayout} layout Layout. - * @param {Array.} flatCoordinates Flat coordinates. - */ -MultiPoint.prototype.setFlatCoordinates = function(layout, flatCoordinates) { - this.setFlatCoordinatesInternal(layout, flatCoordinates); + this.flatCoordinates.length = deflateCoordinates( + this.flatCoordinates, 0, coordinates, this.stride); this.changed(); }; export default MultiPoint; diff --git a/src/ol/geom/MultiPolygon.js b/src/ol/geom/MultiPolygon.js index 9cde0ed0c3..365b2e5e4e 100644 --- a/src/ol/geom/MultiPolygon.js +++ b/src/ol/geom/MultiPolygon.js @@ -225,10 +225,7 @@ MultiPolygon.prototype.getFlatInteriorPoints = function() { * @api */ MultiPolygon.prototype.getInteriorPoints = function() { - const interiorPoints = new MultiPoint(null); - interiorPoints.setFlatCoordinates(GeometryLayout.XYM, - this.getFlatInteriorPoints().slice()); - return interiorPoints; + return new MultiPoint(this.getFlatInteriorPoints().slice(), GeometryLayout.XYM); }; diff --git a/test/spec/ol/geom/multipoint.test.js b/test/spec/ol/geom/multipoint.test.js index 308f7c3ac9..bdbb32533c 100644 --- a/test/spec/ol/geom/multipoint.test.js +++ b/test/spec/ol/geom/multipoint.test.js @@ -5,10 +5,10 @@ import Point from '../../../../src/ol/geom/Point.js'; describe('ol.geom.MultiPoint', function() { - it('can be constructed with a null geometry', function() { + it('cannot be constructed with a null geometry', function() { expect(function() { return new MultiPoint(null); - }).not.to.throwException(); + }).to.throwException(); }); describe('construct empty', function() {