Do not accept null coordinates in Point constructor

This commit is contained in:
ahocevar
2018-07-07 14:48:46 +02:00
parent da0838dde2
commit c0d04ea077
8 changed files with 31 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

@@ -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.<module:ol/geom/Point>} */
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;

View File

@@ -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.<number>} 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;

View File

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

View File

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