Accept flat coordinates for MultiPoint constructor

This commit is contained in:
ahocevar
2018-07-07 16:26:24 +02:00
parent 160f1bc286
commit 3871f7785a
5 changed files with 22 additions and 35 deletions

View File

@@ -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);

View File

@@ -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_();

View File

@@ -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.<module:ol/coordinate~Coordinate>} coordinates Coordinates.
* @param {Array.<module:ol/coordinate~Coordinate>|Array.<number>} 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.<module:ol/coordinate~Coordinate>} coordinates Coordinates.
* @param {!Array.<module:ol/coordinate~Coordinate>} 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.<number>} 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;

View File

@@ -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);
};

View File

@@ -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() {