Allow flat coordinates for Polygon constructor
This commit is contained in:
@@ -247,7 +247,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 +254,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;
|
||||
}
|
||||
|
||||
@@ -444,7 +444,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 +451,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;
|
||||
}
|
||||
|
||||
@@ -1047,9 +1047,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 {
|
||||
@@ -1179,14 +1177,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 {
|
||||
|
||||
@@ -336,17 +336,19 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) {
|
||||
ends = endss;
|
||||
geom = new MultiPolygon(null);
|
||||
} 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.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) {
|
||||
geom.setFlatCoordinates(GeometryLayout.XY, flatCoordinates, ends);
|
||||
}
|
||||
feature = new this.featureClass_();
|
||||
if (this.geometryName_) {
|
||||
feature.setGeometryName(this.geometryName_);
|
||||
|
||||
@@ -175,9 +175,7 @@ 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);
|
||||
|
||||
@@ -294,10 +294,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 +317,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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
if (opt_layout !== undefined && opt_ends) {
|
||||
this.setFlatCoordinatesInternal(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());
|
||||
};
|
||||
|
||||
|
||||
@@ -304,10 +309,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,15 +334,12 @@ 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 = [];
|
||||
@@ -349,19 +348,6 @@ Polygon.prototype.setCoordinates = function(coordinates, opt_layout) {
|
||||
this.flatCoordinates, 0, coordinates, this.stride, this.ends_);
|
||||
this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1];
|
||||
this.changed();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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;
|
||||
this.changed();
|
||||
};
|
||||
|
||||
export default Polygon;
|
||||
@@ -387,9 +373,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 +390,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 +407,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 +429,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 +438,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();
|
||||
}
|
||||
|
||||
@@ -988,14 +988,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;
|
||||
}
|
||||
);
|
||||
|
||||
@@ -287,8 +287,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,8 +304,7 @@ 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]);
|
||||
let geom = new Polygon(polygon, 'XY', [polygon.length]);
|
||||
const multiPolygon = new MultiPolygon(null);
|
||||
multiPolygon.appendPolygon(geom);
|
||||
geom = geom.clone();
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user