Merge pull request #8362 from ahocevar/smart-flat-coordinates

Smart flat coordinates
This commit is contained in:
Andreas Hocevar
2018-07-09 10:27:11 +02:00
committed by GitHub
28 changed files with 321 additions and 478 deletions

View File

@@ -1,5 +1,11 @@
## Upgrade notes
### Next release
#### Geometry constructor and `setCoordinates` no longer accept `null` coordinates
Geometries (`ol/geom/*`) now need to be constructed with valid coordinates (center for `ol/geom/Circle`) as first constructor argument. The same applies to the `setCoordinates()` (`setCenter() for `ol/geom/Circle`) method.
### v5.0.0
#### Renamed `ol/source/TileUTFGrid` to `ol/source/UTFGrid`
@@ -170,7 +176,7 @@ The optional this (i.e. opt_this) arguments were removed from the following meth
#### `Map#forEachLayerAtPixel` parameters have changed
If you are using the layer filter, please note that you now have to pass in the layer filter via an `AtPixelOptions` object. If you are not using the layer filter the usage has not changed.
Old syntax:

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;
};
@@ -563,8 +571,13 @@ Graticule.prototype.getMap = function() {
*/
Graticule.prototype.getMeridian_ = function(lon, minLat, maxLat, squaredTolerance, index) {
const flatCoordinates = meridian(lon, minLat, maxLat, this.projection_, squaredTolerance);
const lineString = this.meridians_[index] !== undefined ? this.meridians_[index] : new LineString(null);
lineString.setFlatCoordinates(GeometryLayout.XY, flatCoordinates);
let lineString = this.meridians_[index];
if (!lineString) {
lineString = this.meridians_[index] = new LineString(flatCoordinates, GeometryLayout.XY);
} else {
lineString.setFlatCoordinates(GeometryLayout.XY, flatCoordinates);
lineString.changed();
}
return lineString;
};
@@ -590,8 +603,13 @@ Graticule.prototype.getMeridians = function() {
*/
Graticule.prototype.getParallel_ = function(lat, minLon, maxLon, squaredTolerance, index) {
const flatCoordinates = parallel(lat, minLon, maxLon, this.projection_, squaredTolerance);
const lineString = this.parallels_[index] !== undefined ? this.parallels_[index] : new LineString(null);
lineString.setFlatCoordinates(GeometryLayout.XY, flatCoordinates);
let lineString = this.parallels_[index];
if (!lineString) {
lineString = new LineString(flatCoordinates, GeometryLayout.XY);
} else {
lineString.setFlatCoordinates(GeometryLayout.XY, flatCoordinates);
lineString.changed();
}
return lineString;
};

View File

@@ -103,8 +103,7 @@ GML3.prototype.readMultiCurve_ = function(node, objectStack) {
const lineStrings = pushParseAndPop([],
this.MULTICURVE_PARSERS_, node, objectStack, this);
if (lineStrings) {
const multiLineString = new MultiLineString(null);
multiLineString.setLineStrings(lineStrings);
const multiLineString = new MultiLineString(lineStrings);
return multiLineString;
} else {
return undefined;
@@ -123,11 +122,7 @@ GML3.prototype.readMultiSurface_ = function(node, objectStack) {
const polygons = pushParseAndPop([],
this.MULTISURFACE_PARSERS_, node, objectStack, this);
if (polygons) {
const multiPolygon = new MultiPolygon(null);
multiPolygon.setPolygons(polygons);
return multiPolygon;
} else {
return undefined;
return new MultiPolygon(polygons);
}
};
@@ -247,7 +242,6 @@ GML3.prototype.readSurface_ = function(node, objectStack) {
const flatLinearRings = pushParseAndPop([null],
this.SURFACE_PARSERS_, node, objectStack, this);
if (flatLinearRings && flatLinearRings[0]) {
const polygon = new Polygon(null);
const flatCoordinates = flatLinearRings[0];
const ends = [flatCoordinates.length];
let i, ii;
@@ -255,9 +249,7 @@ GML3.prototype.readSurface_ = function(node, objectStack) {
extend(flatCoordinates, flatLinearRings[i]);
ends.push(flatCoordinates.length);
}
polygon.setFlatCoordinates(
GeometryLayout.XYZ, flatCoordinates, ends);
return polygon;
return new Polygon(flatCoordinates, GeometryLayout.XYZ, ends);
} else {
return undefined;
}
@@ -275,8 +267,7 @@ GML3.prototype.readCurve_ = function(node, objectStack) {
const flatCoordinates = pushParseAndPop([null],
this.CURVE_PARSERS_, node, objectStack, this);
if (flatCoordinates) {
const lineString = new LineString(null);
lineString.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates);
const lineString = new LineString(flatCoordinates, GeometryLayout.XYZ);
return lineString;
} else {
return undefined;

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);
}
};
@@ -325,11 +323,7 @@ GMLBase.prototype.readMultiLineString = function(node, objectStack) {
const lineStrings = pushParseAndPop([],
this.MULTILINESTRING_PARSERS_, node, objectStack, this);
if (lineStrings) {
const multiLineString = new MultiLineString(null);
multiLineString.setLineStrings(lineStrings);
return multiLineString;
} else {
return undefined;
return new MultiLineString(lineStrings);
}
};
@@ -343,11 +337,7 @@ GMLBase.prototype.readMultiPolygon = function(node, objectStack) {
/** @type {Array.<module:ol/geom/Polygon>} */
const polygons = pushParseAndPop([], this.MULTIPOLYGON_PARSERS_, node, objectStack, this);
if (polygons) {
const multiPolygon = new MultiPolygon(null);
multiPolygon.setPolygons(polygons);
return multiPolygon;
} else {
return undefined;
return new MultiPolygon(polygons);
}
};
@@ -390,8 +380,7 @@ GMLBase.prototype.polygonMemberParser_ = function(node, objectStack) {
GMLBase.prototype.readLineString = function(node, objectStack) {
const flatCoordinates = this.readFlatCoordinatesFromNode_(node, objectStack);
if (flatCoordinates) {
const lineString = new LineString(null);
lineString.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates);
const lineString = new LineString(flatCoordinates, GeometryLayout.XYZ);
return lineString;
} else {
return undefined;
@@ -425,11 +414,7 @@ GMLBase.prototype.readFlatLinearRing_ = function(node, objectStack) {
GMLBase.prototype.readLinearRing = function(node, objectStack) {
const flatCoordinates = this.readFlatCoordinatesFromNode_(node, objectStack);
if (flatCoordinates) {
const ring = new LinearRing(null);
ring.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates);
return ring;
} else {
return undefined;
return new LinearRing(flatCoordinates, GeometryLayout.XYZ);
}
};
@@ -444,7 +429,6 @@ GMLBase.prototype.readPolygon = function(node, objectStack) {
const flatLinearRings = pushParseAndPop([null],
this.FLAT_LINEAR_RINGS_PARSERS_, node, objectStack, this);
if (flatLinearRings && flatLinearRings[0]) {
const polygon = new Polygon(null);
const flatCoordinates = flatLinearRings[0];
const ends = [flatCoordinates.length];
let i, ii;
@@ -452,8 +436,7 @@ GMLBase.prototype.readPolygon = function(node, objectStack) {
extend(flatCoordinates, flatLinearRings[i]);
ends.push(flatCoordinates.length);
}
polygon.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates, ends);
return polygon;
return new Polygon(flatCoordinates, GeometryLayout.XYZ, ends);
} else {
return undefined;
}

View File

@@ -553,8 +553,7 @@ function readRte(node, objectStack) {
const layoutOptions = /** @type {module:ol/format/GPX~LayoutOptions} */ (values['layoutOptions']);
delete values['layoutOptions'];
const layout = applyLayoutOptions(layoutOptions, flatCoordinates);
const geometry = new LineString(null);
geometry.setFlatCoordinates(layout, flatCoordinates);
const geometry = new LineString(flatCoordinates, layout);
transformWithOptions(geometry, false, options);
const feature = new Feature(geometry);
feature.setProperties(values);
@@ -585,8 +584,7 @@ function readTrk(node, objectStack) {
const layoutOptions = /** @type {module:ol/format/GPX~LayoutOptions} */ (values['layoutOptions']);
delete values['layoutOptions'];
const layout = applyLayoutOptions(layoutOptions, flatCoordinates, ends);
const geometry = new MultiLineString(null);
geometry.setFlatCoordinates(layout, flatCoordinates, ends);
const geometry = new MultiLineString(flatCoordinates, layout, ends);
transformWithOptions(geometry, false, options);
const feature = new Feature(geometry);
feature.setProperties(values);

View File

@@ -168,9 +168,8 @@ IGC.prototype.readFeatureFromText = function(text, opt_options) {
if (flatCoordinates.length === 0) {
return null;
}
const lineString = new LineString(null);
const layout = altitudeMode == IGCZ.NONE ? GeometryLayout.XYM : GeometryLayout.XYZM;
lineString.setFlatCoordinates(layout, flatCoordinates);
const lineString = new LineString(flatCoordinates, layout);
const feature = new Feature(transformWithOptions(lineString, false, opt_options));
feature.setProperties(properties);
return feature;

View File

@@ -904,9 +904,7 @@ function readGxMultiTrack(node, objectStack) {
if (!lineStrings) {
return undefined;
}
const multiLineString = new MultiLineString(null);
multiLineString.setLineStrings(lineStrings);
return multiLineString;
return new MultiLineString(lineStrings);
}
@@ -942,9 +940,7 @@ function readGxTrack(node, objectStack) {
for (let i = 0, ii = Math.min(flatCoordinates.length, whens.length); i < ii; ++i) {
flatCoordinates[4 * i + 3] = whens[i];
}
const lineString = new LineString(null);
lineString.setFlatCoordinates(GeometryLayout.XYZM, flatCoordinates);
return lineString;
return new LineString(flatCoordinates, GeometryLayout.XYZM);
}
@@ -1025,8 +1021,7 @@ function readLineString(node, objectStack) {
const flatCoordinates =
readFlatCoordinatesFromNode(node, objectStack);
if (flatCoordinates) {
const lineString = new LineString(null);
lineString.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates);
const lineString = new LineString(flatCoordinates, GeometryLayout.XYZ);
lineString.setProperties(properties);
return lineString;
} else {
@@ -1047,9 +1042,7 @@ function readLinearRing(node, objectStack) {
const flatCoordinates =
readFlatCoordinatesFromNode(node, objectStack);
if (flatCoordinates) {
const polygon = new Polygon(null);
polygon.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates,
[flatCoordinates.length]);
const polygon = new Polygon(flatCoordinates, GeometryLayout.XYZ, [flatCoordinates.length]);
polygon.setProperties(properties);
return polygon;
} else {
@@ -1109,16 +1102,13 @@ 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);
multiGeometry.setLineStrings(geometries);
multiGeometry = new MultiLineString(geometries);
setCommonGeometryProperties(multiGeometry, geometries);
} else if (type == GeometryType.POLYGON) {
multiGeometry = new MultiPolygon(null);
multiGeometry.setPolygons(geometries);
multiGeometry = new MultiPolygon(geometries);
setCommonGeometryProperties(multiGeometry, geometries);
} else if (type == GeometryType.GEOMETRY_COLLECTION) {
multiGeometry = new GeometryCollection(geometries);
@@ -1146,8 +1136,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 {
@@ -1179,14 +1168,13 @@ function readPolygon(node, objectStack) {
const flatLinearRings = pushParseAndPop([null],
FLAT_LINEAR_RINGS_PARSERS, node, objectStack);
if (flatLinearRings && flatLinearRings[0]) {
const polygon = new Polygon(null);
const flatCoordinates = flatLinearRings[0];
const ends = [flatCoordinates.length];
for (let i = 1, ii = flatLinearRings.length; i < ii; ++i) {
extend(flatCoordinates, flatLinearRings[i]);
ends.push(flatCoordinates.length);
}
polygon.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates, ends);
const polygon = new Polygon(flatCoordinates, GeometryLayout.XYZ, ends);
polygon.setProperties(properties);
return polygon;
} else {

View File

@@ -311,7 +311,7 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) {
values[this.layerName_] = rawFeature.layer.name;
const flatCoordinates = [];
let ends = [];
const ends = [];
this.readRawGeometry_(pbf, rawFeature, flatCoordinates, ends);
const geometryType = getGeometryType(type, ends.length);
@@ -333,20 +333,18 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) {
offset = end;
}
if (endss.length > 1) {
ends = endss;
geom = new MultiPolygon(null);
geom = new MultiPolygon(flatCoordinates, GeometryLayout.XY, endss);
} else {
geom = new Polygon(null);
geom = new Polygon(flatCoordinates, GeometryLayout.XY, ends);
}
} else {
geom = geometryType === GeometryType.POINT ? new Point(null) :
geometryType === GeometryType.LINE_STRING ? new LineString(null) :
geometryType === GeometryType.POLYGON ? new Polygon(null) :
geometryType === GeometryType.MULTI_POINT ? new MultiPoint (null) :
geometryType === GeometryType.MULTI_LINE_STRING ? new MultiLineString(null) :
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(flatCoordinates, GeometryLayout.XY) :
geometryType === GeometryType.MULTI_LINE_STRING ? new MultiLineString(flatCoordinates, GeometryLayout.XY, ends) :
null;
}
geom.setFlatCoordinates(GeometryLayout.XY, flatCoordinates, ends);
feature = new this.featureClass_();
if (this.geometryName_) {
feature.setGeometryName(this.geometryName_);

View File

@@ -175,12 +175,9 @@ OSMXML.prototype.readFeaturesFromNode = function(node, opt_options) {
let geometry;
if (values.ndrefs[0] == values.ndrefs[values.ndrefs.length - 1]) {
// closed way
geometry = new Polygon(null);
geometry.setFlatCoordinates(GeometryLayout.XY, flatCoordinates,
[flatCoordinates.length]);
geometry = new Polygon(flatCoordinates, GeometryLayout.XY, [flatCoordinates.length]);
} else {
geometry = new LineString(null);
geometry.setFlatCoordinates(GeometryLayout.XY, flatCoordinates);
geometry = new LineString(flatCoordinates, GeometryLayout.XY);
}
transformWithOptions(geometry, false, options);
const feature = new Feature(geometry);

View File

@@ -858,7 +858,14 @@ Parser.prototype.parseGeometry_ = function() {
if (!parser || !ctor) {
throw new Error('Invalid geometry type: ' + geomType);
}
const coordinates = parser.call(this);
let coordinates = parser.call(this);
if (!coordinates) {
if (ctor === GeometryConstructor[GeometryType.POINT]) {
coordinates = [NaN, NaN];
} else {
coordinates = [];
}
}
return new ctor(coordinates, this.layout_);
}
}

View File

@@ -3,7 +3,6 @@
*/
import {inherits} from '../util.js';
import {createOrUpdate, forEachCorner, intersects} 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';
@@ -13,16 +12,22 @@ import {deflateCoordinate} from '../geom/flat/deflate.js';
* Circle geometry.
*
* @constructor
* @extends {module:ol/geom/SimpleGeometry}
* @param {module:ol/coordinate~Coordinate} center Center.
* @extends {!module:ol/geom/SimpleGeometry}
* @param {!module:ol/coordinate~Coordinate} center Center. (For internal use,
* flat coordinates in combination with `opt_layout` and no `opt_radius` are
* also accepted.)
* @param {number=} opt_radius Radius.
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
* @api
*/
const Circle = function(center, opt_radius, opt_layout) {
SimpleGeometry.call(this);
const radius = opt_radius ? opt_radius : 0;
this.setCenterAndRadius(center, radius, opt_layout);
if (opt_layout !== undefined && opt_radius === undefined) {
this.setFlatCoordinates(opt_layout, center);
} else {
const radius = opt_radius ? opt_radius : 0;
this.setCenterAndRadius(center, radius, opt_layout);
}
};
inherits(Circle, SimpleGeometry);
@@ -35,9 +40,7 @@ inherits(Circle, SimpleGeometry);
* @api
*/
Circle.prototype.clone = function() {
const circle = new Circle(null);
circle.setFlatCoordinates(this.layout, this.flatCoordinates.slice());
return circle;
return new Circle(this.flatCoordinates.slice(), undefined, this.layout);
};
@@ -171,36 +174,33 @@ Circle.prototype.setCenter = function(center) {
flatCoordinates[stride + i] = center[i];
}
this.setFlatCoordinates(this.layout, flatCoordinates);
this.changed();
};
/**
* Set the center (as {@link module:ol/coordinate~Coordinate coordinate}) and the radius (as
* number) of the circle.
* @param {module:ol/coordinate~Coordinate} center Center.
* @param {!module:ol/coordinate~Coordinate} center Center.
* @param {number} radius Radius.
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
* @api
*/
Circle.prototype.setCenterAndRadius = function(center, radius, opt_layout) {
if (!center) {
this.setFlatCoordinates(GeometryLayout.XY, null);
} else {
this.setLayout(opt_layout, center, 0);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
/** @type {Array.<number>} */
const flatCoordinates = this.flatCoordinates;
let offset = deflateCoordinate(
flatCoordinates, 0, center, this.stride);
flatCoordinates[offset++] = flatCoordinates[0] + radius;
for (let i = 1, ii = this.stride; i < ii; ++i) {
flatCoordinates[offset++] = flatCoordinates[i];
}
flatCoordinates.length = offset;
this.changed();
this.setLayout(opt_layout, center, 0);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
/** @type {Array.<number>} */
const flatCoordinates = this.flatCoordinates;
let offset = deflateCoordinate(
flatCoordinates, 0, center, this.stride);
flatCoordinates[offset++] = flatCoordinates[0] + radius;
for (let i = 1, ii = this.stride; i < ii; ++i) {
flatCoordinates[offset++] = flatCoordinates[i];
}
flatCoordinates.length = offset;
this.changed();
};
@@ -216,16 +216,6 @@ Circle.prototype.getCoordinates = function() {};
Circle.prototype.setCoordinates = function(coordinates, opt_layout) {};
/**
* @param {module:ol/geom/GeometryLayout} layout Layout.
* @param {Array.<number>} flatCoordinates Flat coordinates.
*/
Circle.prototype.setFlatCoordinates = function(layout, flatCoordinates) {
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.changed();
};
/**
* Set the radius of the circle. The radius is in the units of the projection.
* @param {number} radius Radius.

View File

@@ -22,7 +22,9 @@ import {douglasPeucker} from '../geom/flat/simplify.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
*/
@@ -54,7 +56,11 @@ const LineString = function(coordinates, opt_layout) {
*/
this.maxDeltaRevision_ = -1;
this.setCoordinates(coordinates, opt_layout);
if (opt_layout !== undefined && !Array.isArray(coordinates[0])) {
this.setFlatCoordinates(opt_layout, coordinates);
} else {
this.setCoordinates(coordinates, opt_layout);
}
};
@@ -83,9 +89,7 @@ LineString.prototype.appendCoordinate = function(coordinate) {
* @api
*/
LineString.prototype.clone = function() {
const lineString = new LineString(null);
lineString.setFlatCoordinates(this.layout, this.flatCoordinates.slice());
return lineString;
return new LineString(this.flatCoordinates.slice(), this.layout);
};
@@ -208,10 +212,7 @@ LineString.prototype.getSimplifiedGeometryInternal = function(squaredTolerance)
simplifiedFlatCoordinates.length = douglasPeucker(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride,
squaredTolerance, simplifiedFlatCoordinates, 0);
const simplifiedLineString = new LineString(null);
simplifiedLineString.setFlatCoordinates(
GeometryLayout.XY, simplifiedFlatCoordinates);
return simplifiedLineString;
return new LineString(simplifiedFlatCoordinates, GeometryLayout.XY);
};
@@ -237,32 +238,19 @@ LineString.prototype.intersectsExtent = function(extent) {
/**
* Set the coordinates of the linestring.
* @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
*/
LineString.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.
*/
LineString.prototype.setFlatCoordinates = function(layout, flatCoordinates) {
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.flatCoordinates.length = deflateCoordinates(
this.flatCoordinates, 0, coordinates, this.stride);
this.changed();
};
export default LineString;

View File

@@ -19,7 +19,9 @@ import {douglasPeucker} from '../geom/flat/simplify.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
*/
@@ -39,7 +41,11 @@ const LinearRing = function(coordinates, opt_layout) {
*/
this.maxDeltaRevision_ = -1;
this.setCoordinates(coordinates, opt_layout);
if (opt_layout !== undefined && !Array.isArray(coordinates[0])) {
this.setFlatCoordinates(opt_layout, coordinates);
} else {
this.setCoordinates(coordinates, opt_layout);
}
};
@@ -53,9 +59,7 @@ inherits(LinearRing, SimpleGeometry);
* @api
*/
LinearRing.prototype.clone = function() {
const linearRing = new LinearRing(null);
linearRing.setFlatCoordinates(this.layout, this.flatCoordinates.slice());
return linearRing;
return new LinearRing(this.flatCoordinates.slice(), this.layout);
};
@@ -107,10 +111,7 @@ LinearRing.prototype.getSimplifiedGeometryInternal = function(squaredTolerance)
simplifiedFlatCoordinates.length = douglasPeucker(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride,
squaredTolerance, simplifiedFlatCoordinates, 0);
const simplifiedLinearRing = new LinearRing(null);
simplifiedLinearRing.setFlatCoordinates(
GeometryLayout.XY, simplifiedFlatCoordinates);
return simplifiedLinearRing;
return new LinearRing(simplifiedFlatCoordinates, GeometryLayout.XY);
};
@@ -131,32 +132,18 @@ LinearRing.prototype.intersectsExtent = function(extent) {};
/**
* Set the coordinates of the linear ring.
* @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
*/
LinearRing.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.
*/
LinearRing.prototype.setFlatCoordinates = function(layout, flatCoordinates) {
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.flatCoordinates.length = deflateCoordinates(
this.flatCoordinates, 0, coordinates, this.stride);
this.changed();
};
export default LinearRing;

View File

@@ -21,11 +21,14 @@ import {douglasPeuckerArray} from '../geom/flat/simplify.js';
*
* @constructor
* @extends {module:ol/geom/SimpleGeometry}
* @param {Array.<Array.<module:ol/coordinate~Coordinate>>} coordinates Coordinates.
* @param {Array.<Array.<module:ol/coordinate~Coordinate>|module:ol/geom~MultiLineString>|Array.<number>} coordinates
* Coordinates or LineString geometries. (For internal use, flat coordinates in
* combination with `opt_layout` and `opt_ends` are also accepted.)
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
* @param {Array.<number>} opt_ends Flat coordinate ends for internal use.
* @api
*/
const MultiLineString = function(coordinates, opt_layout) {
const MultiLineString = function(coordinates, opt_layout, opt_ends) {
SimpleGeometry.call(this);
@@ -47,7 +50,26 @@ const MultiLineString = function(coordinates, opt_layout) {
*/
this.maxDeltaRevision_ = -1;
this.setCoordinates(coordinates, opt_layout);
if (Array.isArray(coordinates[0])) {
this.setCoordinates(coordinates, opt_layout);
} else if (opt_layout !== undefined && opt_ends) {
this.setFlatCoordinates(opt_layout, coordinates);
this.ends_ = opt_ends;
} else {
let layout = this.getLayout();
const flatCoordinates = [];
const ends = [];
for (let i = 0, ii = coordinates.length; i < ii; ++i) {
const lineString = coordinates[i];
if (i === 0) {
layout = lineString.getLayout();
}
extend(flatCoordinates, lineString.getFlatCoordinates());
ends.push(flatCoordinates.length);
}
this.setFlatCoordinates(layout, flatCoordinates);
this.ends_ = ends;
}
};
@@ -77,10 +99,7 @@ MultiLineString.prototype.appendLineString = function(lineString) {
* @api
*/
MultiLineString.prototype.clone = function() {
const multiLineString = new MultiLineString(null);
multiLineString.setFlatCoordinates(
this.layout, this.flatCoordinates.slice(), this.ends_.slice());
return multiLineString;
return new MultiLineString(this.flatCoordinates.slice(), this.layout, this.ends_.slice());
};
@@ -167,10 +186,8 @@ MultiLineString.prototype.getLineString = function(index) {
if (index < 0 || this.ends_.length <= index) {
return null;
}
const lineString = new LineString(null);
lineString.setFlatCoordinates(this.layout, this.flatCoordinates.slice(
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]));
return lineString;
return new LineString(this.flatCoordinates.slice(
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]), this.layout);
};
@@ -188,8 +205,7 @@ MultiLineString.prototype.getLineStrings = function() {
let offset = 0;
for (let i = 0, ii = ends.length; i < ii; ++i) {
const end = ends[i];
const lineString = new LineString(null);
lineString.setFlatCoordinates(layout, flatCoordinates.slice(offset, end));
const lineString = new LineString(flatCoordinates.slice(offset, end), layout);
lineStrings.push(lineString);
offset = end;
}
@@ -226,10 +242,7 @@ MultiLineString.prototype.getSimplifiedGeometryInternal = function(squaredTolera
simplifiedFlatCoordinates.length = douglasPeuckerArray(
this.flatCoordinates, 0, this.ends_, this.stride, squaredTolerance,
simplifiedFlatCoordinates, 0, simplifiedEnds);
const simplifiedMultiLineString = new MultiLineString(null);
simplifiedMultiLineString.setFlatCoordinates(
GeometryLayout.XY, simplifiedFlatCoordinates, simplifiedEnds);
return simplifiedMultiLineString;
return new MultiLineString(simplifiedFlatCoordinates, GeometryLayout.XY, simplifiedEnds);
};
@@ -254,54 +267,19 @@ MultiLineString.prototype.intersectsExtent = function(extent) {
/**
* Set the coordinates of the multilinestring.
* @param {Array.<Array.<module:ol/coordinate~Coordinate>>} coordinates Coordinates.
* @param {!Array.<Array.<module:ol/coordinate~Coordinate>>} coordinates Coordinates.
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
* @override
* @api
*/
MultiLineString.prototype.setCoordinates = function(coordinates, opt_layout) {
if (!coordinates) {
this.setFlatCoordinates(GeometryLayout.XY, null, this.ends_);
} else {
this.setLayout(opt_layout, coordinates, 2);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
const ends = deflateCoordinatesArray(
this.flatCoordinates, 0, coordinates, this.stride, this.ends_);
this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1];
this.changed();
this.setLayout(opt_layout, coordinates, 2);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
};
/**
* @param {module:ol/geom/GeometryLayout} layout Layout.
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {Array.<number>} ends Ends.
*/
MultiLineString.prototype.setFlatCoordinates = function(layout, flatCoordinates, ends) {
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.ends_ = ends;
const ends = deflateCoordinatesArray(
this.flatCoordinates, 0, coordinates, this.stride, this.ends_);
this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1];
this.changed();
};
/**
* @param {Array.<module:ol/geom/LineString>} lineStrings LineStrings.
*/
MultiLineString.prototype.setLineStrings = function(lineStrings) {
let layout = this.getLayout();
const flatCoordinates = [];
const ends = [];
for (let i = 0, ii = lineStrings.length; i < ii; ++i) {
const lineString = lineStrings[i];
if (i === 0) {
layout = lineString.getLayout();
}
extend(flatCoordinates, lineString.getFlatCoordinates());
ends.push(flatCoordinates.length);
}
this.setFlatCoordinates(layout, flatCoordinates, ends);
};
export default MultiLineString;

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.setFlatCoordinates(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;
};
@@ -105,10 +109,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 +126,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;
@@ -161,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

@@ -26,11 +26,15 @@ import {quantizeMultiArray} from '../geom/flat/simplify.js';
*
* @constructor
* @extends {module:ol/geom/SimpleGeometry}
* @param {Array.<Array.<Array.<module:ol/coordinate~Coordinate>>>} coordinates Coordinates.
* @param {Array.<Array.<Array.<module:ol/coordinate~Coordinate>>>|Array.<number>} coordinates
* Coordinates. (For internal use, flat coordinats in combination with
* `opt_layout` and `opt_endss` are also accepted).
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
* @param {Array.<number>} opt_endss Array of ends for internal use with flat
* coordinates.
* @api
*/
const MultiPolygon = function(coordinates, opt_layout) {
const MultiPolygon = function(coordinates, opt_layout, opt_endss) {
SimpleGeometry.call(this);
@@ -76,7 +80,33 @@ const MultiPolygon = function(coordinates, opt_layout) {
*/
this.orientedFlatCoordinates_ = null;
this.setCoordinates(coordinates, opt_layout);
if (!opt_endss && !Array.isArray(coordinates[0])) {
let layout = this.getLayout();
const flatCoordinates = [];
const endss = [];
for (let i = 0, ii = coordinates.length; i < ii; ++i) {
const polygon = coordinates[i];
if (i === 0) {
layout = polygon.getLayout();
}
const offset = flatCoordinates.length;
const ends = polygon.getEnds();
for (let j = 0, jj = ends.length; j < jj; ++j) {
ends[j] += offset;
}
extend(flatCoordinates, polygon.getFlatCoordinates());
endss.push(ends);
}
opt_layout = layout;
coordinates = flatCoordinates;
opt_endss = endss;
}
if (opt_layout !== undefined && opt_endss) {
this.setFlatCoordinates(opt_layout, coordinates);
this.endss_ = opt_endss;
} else {
this.setCoordinates(coordinates, opt_layout);
}
};
@@ -115,17 +145,14 @@ MultiPolygon.prototype.appendPolygon = function(polygon) {
* @api
*/
MultiPolygon.prototype.clone = function() {
const multiPolygon = new MultiPolygon(null);
const len = this.endss_.length;
const newEndss = new Array(len);
for (let i = 0; i < len; ++i) {
newEndss[i] = this.endss_[i].slice();
}
multiPolygon.setFlatCoordinates(
this.layout, this.flatCoordinates.slice(), newEndss);
return multiPolygon;
return new MultiPolygon(
this.flatCoordinates.slice(), this.layout, newEndss);
};
@@ -225,10 +252,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);
};
@@ -263,10 +287,7 @@ MultiPolygon.prototype.getSimplifiedGeometryInternal = function(squaredTolerance
this.flatCoordinates, 0, this.endss_, this.stride,
Math.sqrt(squaredTolerance),
simplifiedFlatCoordinates, 0, simplifiedEndss);
const simplifiedMultiPolygon = new MultiPolygon(null);
simplifiedMultiPolygon.setFlatCoordinates(
GeometryLayout.XY, simplifiedFlatCoordinates, simplifiedEndss);
return simplifiedMultiPolygon;
return new MultiPolygon(simplifiedFlatCoordinates, GeometryLayout.XY, simplifiedEndss);
};
@@ -294,10 +315,7 @@ MultiPolygon.prototype.getPolygon = function(index) {
ends[i] -= offset;
}
}
const polygon = new Polygon(null);
polygon.setFlatCoordinates(
this.layout, this.flatCoordinates.slice(offset, end), ends);
return polygon;
return new Polygon(this.flatCoordinates.slice(offset, end), this.layout, ends);
};
@@ -320,9 +338,7 @@ MultiPolygon.prototype.getPolygons = function() {
ends[j] -= offset;
}
}
const polygon = new Polygon(null);
polygon.setFlatCoordinates(
layout, flatCoordinates.slice(offset, end), ends);
const polygon = new Polygon(flatCoordinates.slice(offset, end), layout, ends);
polygons.push(polygon);
offset = end;
}
@@ -351,66 +367,27 @@ MultiPolygon.prototype.intersectsExtent = function(extent) {
/**
* Set the coordinates of the multipolygon.
* @param {Array.<Array.<Array.<module:ol/coordinate~Coordinate>>>} coordinates Coordinates.
* @param {!Array.<Array.<Array.<module:ol/coordinate~Coordinate>>>} coordinates Coordinates.
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
* @override
* @api
*/
MultiPolygon.prototype.setCoordinates = function(coordinates, opt_layout) {
if (!coordinates) {
this.setFlatCoordinates(GeometryLayout.XY, null, this.endss_);
} else {
this.setLayout(opt_layout, coordinates, 3);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
const endss = deflateMultiCoordinatesArray(
this.flatCoordinates, 0, coordinates, this.stride, this.endss_);
if (endss.length === 0) {
this.flatCoordinates.length = 0;
} else {
const lastEnds = endss[endss.length - 1];
this.flatCoordinates.length = lastEnds.length === 0 ?
0 : lastEnds[lastEnds.length - 1];
}
this.changed();
this.setLayout(opt_layout, coordinates, 3);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
const endss = deflateMultiCoordinatesArray(
this.flatCoordinates, 0, coordinates, this.stride, this.endss_);
if (endss.length === 0) {
this.flatCoordinates.length = 0;
} else {
const lastEnds = endss[endss.length - 1];
this.flatCoordinates.length = lastEnds.length === 0 ?
0 : lastEnds[lastEnds.length - 1];
}
};
/**
* @param {module:ol/geom/GeometryLayout} layout Layout.
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {Array.<Array.<number>>} endss Endss.
*/
MultiPolygon.prototype.setFlatCoordinates = function(layout, flatCoordinates, endss) {
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.endss_ = endss;
this.changed();
};
/**
* @param {Array.<module:ol/geom/Polygon>} polygons Polygons.
*/
MultiPolygon.prototype.setPolygons = function(polygons) {
let layout = this.getLayout();
const flatCoordinates = [];
const endss = [];
for (let i = 0, ii = polygons.length; i < ii; ++i) {
const polygon = polygons[i];
if (i === 0) {
layout = polygon.getLayout();
}
const offset = flatCoordinates.length;
const ends = polygon.getEnds();
for (let j = 0, jj = ends.length; j < jj; ++j) {
ends[j] += offset;
}
extend(flatCoordinates, polygon.getFlatCoordinates());
endss.push(ends);
}
this.setFlatCoordinates(layout, flatCoordinates, endss);
};
export default MultiPolygon;

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

@@ -27,16 +27,19 @@ import {modulo} from '../math.js';
*
* @constructor
* @extends {module:ol/geom/SimpleGeometry}
* @param {Array.<Array.<module:ol/coordinate~Coordinate>>} coordinates Array of linear
* rings that define the polygon. The first linear ring of the array
* defines the outer-boundary or surface of the polygon. Each subsequent
* linear ring defines a hole in the surface of the polygon. A linear ring
* is an array of vertices' coordinates where the first coordinate and the
* last are equivalent.
* @param {!Array.<Array.<module:ol/coordinate~Coordinate>>|!Array.<number>} coordinates
* Array of linear rings that define the polygon. The first linear ring of the
* array defines the outer-boundary or surface of the polygon. Each subsequent
* linear ring defines a hole in the surface of the polygon. A linear ring is
* an array of vertices' coordinates where the first coordinate and the last are
* equivalent. (For internal use, flat coordinates in combination with
* `opt_layout` and `opt_ends` are also accepted.)
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
* @param {Array.<number>=} opt_ends Ends (for internal use with flat
* coordinates).
* @api
*/
const Polygon = function(coordinates, opt_layout) {
const Polygon = function(coordinates, opt_layout, opt_ends) {
SimpleGeometry.call(this);
@@ -82,7 +85,12 @@ const Polygon = function(coordinates, opt_layout) {
*/
this.orientedFlatCoordinates_ = null;
this.setCoordinates(coordinates, opt_layout);
if (opt_layout !== undefined && opt_ends) {
this.setFlatCoordinates(opt_layout, coordinates);
this.ends_ = opt_ends;
} else {
this.setCoordinates(coordinates, opt_layout);
}
};
@@ -112,10 +120,7 @@ Polygon.prototype.appendLinearRing = function(linearRing) {
* @api
*/
Polygon.prototype.clone = function() {
const polygon = new Polygon(null);
polygon.setFlatCoordinates(
this.layout, this.flatCoordinates.slice(), this.ends_.slice());
return polygon;
return new Polygon(this.flatCoordinates.slice(), this.layout, this.ends_.slice());
};
@@ -244,10 +249,8 @@ Polygon.prototype.getLinearRing = function(index) {
if (index < 0 || this.ends_.length <= index) {
return null;
}
const linearRing = new LinearRing(null);
linearRing.setFlatCoordinates(this.layout, this.flatCoordinates.slice(
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]));
return linearRing;
return new LinearRing(this.flatCoordinates.slice(
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]), this.layout);
};
@@ -264,8 +267,7 @@ Polygon.prototype.getLinearRings = function() {
let offset = 0;
for (let i = 0, ii = ends.length; i < ii; ++i) {
const end = ends[i];
const linearRing = new LinearRing(null);
linearRing.setFlatCoordinates(layout, flatCoordinates.slice(offset, end));
const linearRing = new LinearRing(flatCoordinates.slice(offset, end), layout);
linearRings.push(linearRing);
offset = end;
}
@@ -304,10 +306,7 @@ Polygon.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) {
this.flatCoordinates, 0, this.ends_, this.stride,
Math.sqrt(squaredTolerance),
simplifiedFlatCoordinates, 0, simplifiedEnds);
const simplifiedPolygon = new Polygon(null);
simplifiedPolygon.setFlatCoordinates(
GeometryLayout.XY, simplifiedFlatCoordinates, simplifiedEnds);
return simplifiedPolygon;
return new Polygon(simplifiedFlatCoordinates, GeometryLayout.XY, simplifiedEnds);
};
@@ -332,35 +331,19 @@ Polygon.prototype.intersectsExtent = function(extent) {
/**
* Set the coordinates of the polygon.
* @param {Array.<Array.<module:ol/coordinate~Coordinate>>} coordinates Coordinates.
* @param {!Array.<Array.<module:ol/coordinate~Coordinate>>} coordinates Coordinates.
* @param {module:ol/geom/GeometryLayout=} opt_layout Layout.
* @override
* @api
*/
Polygon.prototype.setCoordinates = function(coordinates, opt_layout) {
if (!coordinates) {
this.setFlatCoordinates(GeometryLayout.XY, null, this.ends_);
} else {
this.setLayout(opt_layout, coordinates, 2);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
const ends = deflateCoordinatesArray(
this.flatCoordinates, 0, coordinates, this.stride, this.ends_);
this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1];
this.changed();
this.setLayout(opt_layout, coordinates, 2);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
};
/**
* @param {module:ol/geom/GeometryLayout} layout Layout.
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {Array.<number>} ends Ends.
*/
Polygon.prototype.setFlatCoordinates = function(layout, flatCoordinates, ends) {
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.ends_ = ends;
const ends = deflateCoordinatesArray(
this.flatCoordinates, 0, coordinates, this.stride, this.ends_);
this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1];
this.changed();
};
@@ -387,9 +370,7 @@ export function circular(center, radius, opt_n, opt_sphereRadius) {
extend(flatCoordinates, sphereOffset(center, radius, 2 * Math.PI * i / n, opt_sphereRadius));
}
flatCoordinates.push(flatCoordinates[0], flatCoordinates[1]);
const polygon = new Polygon(null);
polygon.setFlatCoordinates(GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]);
return polygon;
return new Polygon(flatCoordinates, GeometryLayout.XY, [flatCoordinates.length]);
}
@@ -406,10 +387,7 @@ export function fromExtent(extent) {
const maxY = extent[3];
const flatCoordinates =
[minX, minY, minX, maxY, maxX, maxY, maxX, minY, minX, minY];
const polygon = new Polygon(null);
polygon.setFlatCoordinates(
GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]);
return polygon;
return new Polygon(flatCoordinates, GeometryLayout.XY, [flatCoordinates.length]);
}
@@ -426,14 +404,13 @@ export function fromCircle(circle, opt_sides, opt_angle) {
const sides = opt_sides ? opt_sides : 32;
const stride = circle.getStride();
const layout = circle.getLayout();
const polygon = new Polygon(null, layout);
const arrayLength = stride * (sides + 1);
const flatCoordinates = new Array(arrayLength);
for (let i = 0; i < arrayLength; i++) {
flatCoordinates[i] = 0;
}
const ends = [flatCoordinates.length];
polygon.setFlatCoordinates(layout, flatCoordinates, ends);
const polygon = new Polygon(flatCoordinates, layout, ends);
makeRegular(polygon, circle.getCenter(), circle.getRadius(), opt_angle);
return polygon;
}
@@ -449,9 +426,7 @@ export function fromCircle(circle, opt_sides, opt_angle) {
*/
export function makeRegular(polygon, center, radius, opt_angle) {
const flatCoordinates = polygon.getFlatCoordinates();
const layout = polygon.getLayout();
const stride = polygon.getStride();
const ends = polygon.getEnds();
const sides = flatCoordinates.length / stride - 1;
const startAngle = opt_angle ? opt_angle : 0;
for (let i = 0; i <= sides; ++i) {
@@ -460,5 +435,5 @@ export function makeRegular(polygon, center, radius, opt_angle) {
flatCoordinates[offset] = center[0] + (radius * Math.cos(angle));
flatCoordinates[offset + 1] = center[1] + (radius * Math.sin(angle));
}
polygon.setFlatCoordinates(layout, flatCoordinates, ends);
polygon.changed();
}

View File

@@ -203,9 +203,8 @@ SimpleGeometry.prototype.getStride = function() {
/**
* @param {module:ol/geom/GeometryLayout} layout Layout.
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @protected
*/
SimpleGeometry.prototype.setFlatCoordinatesInternal = function(layout, flatCoordinates) {
*/
SimpleGeometry.prototype.setFlatCoordinates = function(layout, flatCoordinates) {
this.stride = getStrideForLayout(layout);
this.layout = layout;
this.flatCoordinates = flatCoordinates;
@@ -214,7 +213,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

@@ -733,12 +733,18 @@ Draw.prototype.modifyDrawing_ = function(event) {
if (geometry instanceof Polygon &&
this.mode_ !== Mode.POLYGON) {
if (!this.sketchLine_) {
this.sketchLine_ = new Feature(new LineString(null));
this.sketchLine_ = new Feature();
}
const ring = geometry.getLinearRing(0);
sketchLineGeom = /** @type {module:ol/geom/LineString} */ (this.sketchLine_.getGeometry());
sketchLineGeom.setFlatCoordinates(
ring.getLayout(), ring.getFlatCoordinates());
if (!sketchLineGeom) {
sketchLineGeom = new LineString(ring.getFlatCoordinates(), ring.getLayout());
this.sketchLine_.setGeometry(sketchLineGeom);
} else {
sketchLineGeom.setFlatCoordinates(
ring.getLayout(), ring.getFlatCoordinates());
sketchLineGeom.changed();
}
} else if (this.sketchLineCoords_) {
sketchLineGeom = /** @type {module:ol/geom/LineString} */ (this.sketchLine_.getGeometry());
sketchLineGeom.setCoordinates(this.sketchLineCoords_);
@@ -988,14 +994,19 @@ export function createBox() {
return (
function(coordinates, opt_geometry) {
const extent = boundingExtent(coordinates);
const geometry = opt_geometry || new Polygon(null);
geometry.setCoordinates([[
const boxCoordinates = [[
getBottomLeft(extent),
getBottomRight(extent),
getTopRight(extent),
getTopLeft(extent),
getBottomLeft(extent)
]]);
]];
let geometry = opt_geometry;
if (geometry) {
geometry.setCoordinates(boxCoordinates);
} else {
geometry = new Polygon(boxCoordinates);
}
return geometry;
}
);

View File

@@ -105,8 +105,7 @@ describe('ol.rendering.style.Text', function() {
const polygon = [151, 17, 163, 22, 159, 30, 150, 30, 143, 24, 151, 17];
function createLineString(coords, textAlign, maxAngle, strokeColor, strokeWidth, scale) {
let geom = new LineString();
geom.setFlatCoordinates('XY', coords);
let geom = new LineString(coords, 'XY');
let style = new Style({
stroke: new Stroke({
color: 'red'
@@ -262,10 +261,8 @@ describe('ol.rendering.style.Text', function() {
it('renders text along a MultiLineString', function(done) {
createMap('canvas');
let line = new LineString();
line.setFlatCoordinates('XY', nicePath);
const geom = new MultiLineString(null);
geom.appendLineString(line);
let line = new LineString(nicePath, 'XY');
const geom = new MultiLineString([line]);
line = line.clone();
line.translate(0, 50);
geom.appendLineString(line);
@@ -287,8 +284,7 @@ describe('ol.rendering.style.Text', function() {
it('renders text along a Polygon', function(done) {
createMap('canvas');
const geom = new Polygon(null);
geom.setFlatCoordinates('XY', polygon, [polygon.length]);
const geom = new Polygon(polygon, 'XY', [polygon.length]);
const feature = new Feature(geom);
feature.setStyle(new Style({
text: new Text({
@@ -305,10 +301,8 @@ describe('ol.rendering.style.Text', function() {
it('renders text along a MultiPolygon', function(done) {
createMap('canvas');
let geom = new Polygon(null);
geom.setFlatCoordinates('XY', polygon, [polygon.length]);
const multiPolygon = new MultiPolygon(null);
multiPolygon.appendPolygon(geom);
let geom = new Polygon(polygon, 'XY', [polygon.length]);
const multiPolygon = new MultiPolygon([geom]);
geom = geom.clone();
geom.translate(0, 30);
multiPolygon.appendPolygon(geom);

View File

@@ -178,13 +178,6 @@ describe('ol.geom.Circle', function() {
expect(circle.getRadius()).to.be(3);
});
it('fires a single change event', function() {
const spy = sinon.spy();
circle.on('change', spy);
circle.setFlatCoordinates('XY', [1, 2, 4, 2]);
expect(spy.calledOnce).to.be(true);
});
});
describe('#setRadius', function() {

View File

@@ -4,10 +4,10 @@ import LineString from '../../../../src/ol/geom/LineString.js';
describe('ol.geom.LineString', function() {
it('can be constructed with a null geometry', function() {
it('cannot be constructed with a null geometry', function() {
expect(function() {
return new LineString(null);
}).not.to.throwException();
}).to.throwException();
});
describe('construct empty', function() {

View File

@@ -5,10 +5,10 @@ import MultiLineString from '../../../../src/ol/geom/MultiLineString.js';
describe('ol.geom.MultiLineString', function() {
it('can be constructed with a null geometry', function() {
it('cannot be constructed with a null geometry', function() {
expect(function() {
return new MultiLineString(null);
}).not.to.throwException();
}).to.throwException();
});
describe('construct empty', function() {
@@ -343,10 +343,9 @@ describe('ol.geom.MultiLineString', function() {
describe('#setLineStrings', function() {
it('sets the line strings', function() {
const multiLineString = new MultiLineString(null);
const lineString1 = new LineString([[1, 2], [3, 4]]);
const lineString2 = new LineString([[5, 6], [7, 8]]);
multiLineString.setLineStrings([lineString1, lineString2]);
const multiLineString = new MultiLineString([lineString1, lineString2]);
expect(multiLineString.getFlatCoordinates()).to.eql(
[1, 2, 3, 4, 5, 6, 7, 8]);
expect(multiLineString.getEnds()).to.eql([4, 8]);

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

View File

@@ -4,22 +4,17 @@ import Polygon from '../../../../src/ol/geom/Polygon.js';
describe('ol.geom.MultiPolygon', function() {
it('can be constructed with a null geometry', function() {
it('cannot be constructed with a null geometry', function() {
expect(function() {
return new MultiPolygon(null);
}).not.to.throwException();
}).to.throwException();
});
describe('with a null MultiPolygon', function() {
let multiPolygon;
beforeEach(function() {
multiPolygon = new MultiPolygon(null);
});
it('can append polygons', function() {
multiPolygon.appendPolygon(
new Polygon([[[0, 0], [0, 2], [1, 1], [2, 0]]]));
const multiPolygon = new MultiPolygon([
new Polygon([[[0, 0], [0, 2], [1, 1], [2, 0]]])]);
expect(multiPolygon.getCoordinates()).to.eql(
[[[[0, 0], [0, 2], [1, 1], [2, 0]]]]);
multiPolygon.appendPolygon(

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

View File

@@ -6,10 +6,10 @@ import Polygon, {fromCircle, fromExtent} from '../../../../src/ol/geom/Polygon.j
describe('ol/geom/Polygon', function() {
it('can be constructed with a null geometry', function() {
it('cannot be constructed with a null geometry', function() {
expect(function() {
return new Polygon(null);
}).not.to.throwException();
}).to.throwException();
});
describe('construct empty', function() {