Accept flat coordinates in LineString constructor
This commit is contained in:
@@ -571,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.setFlatCoordinatesInternal(GeometryLayout.XY, flatCoordinates);
|
||||
lineString.changed();
|
||||
}
|
||||
return lineString;
|
||||
};
|
||||
|
||||
@@ -598,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.setFlatCoordinatesInternal(GeometryLayout.XY, flatCoordinates);
|
||||
lineString.changed();
|
||||
}
|
||||
return lineString;
|
||||
};
|
||||
|
||||
|
||||
@@ -272,8 +272,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;
|
||||
|
||||
@@ -388,8 +388,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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -942,9 +942,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 +1023,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 {
|
||||
|
||||
@@ -340,13 +340,13 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) {
|
||||
}
|
||||
} else {
|
||||
geom = geometryType === GeometryType.POINT ? new Point(flatCoordinates, GeometryLayout.XY) :
|
||||
geometryType === GeometryType.LINE_STRING ? new LineString(null) :
|
||||
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_LINE_STRING ? new MultiLineString(null) :
|
||||
null;
|
||||
}
|
||||
if (geometryType !== GeometryType.POLYGON && geometryType !== GeometryType.POINT) {
|
||||
if (geometryType !== GeometryType.POLYGON && geometryType !== GeometryType.LINE_STRING && geometryType !== GeometryType.POINT) {
|
||||
geom.setFlatCoordinates(GeometryLayout.XY, flatCoordinates, ends);
|
||||
}
|
||||
feature = new this.featureClass_();
|
||||
|
||||
@@ -177,8 +177,7 @@ OSMXML.prototype.readFeaturesFromNode = function(node, opt_options) {
|
||||
// closed way
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
if (opt_layout !== undefined && !Array.isArray(coordinates[0])) {
|
||||
this.setFlatCoordinatesInternal(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,15 +238,12 @@ 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 = [];
|
||||
@@ -253,16 +251,6 @@ LineString.prototype.setCoordinates = function(coordinates, opt_layout) {
|
||||
this.flatCoordinates.length = deflateCoordinates(
|
||||
this.flatCoordinates, 0, coordinates, this.stride);
|
||||
this.changed();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {module:ol/geom/GeometryLayout} layout Layout.
|
||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||
*/
|
||||
LineString.prototype.setFlatCoordinates = function(layout, flatCoordinates) {
|
||||
this.setFlatCoordinatesInternal(layout, flatCoordinates);
|
||||
this.changed();
|
||||
};
|
||||
export default LineString;
|
||||
|
||||
@@ -167,10 +167,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 +186,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;
|
||||
}
|
||||
|
||||
@@ -203,7 +203,6 @@ SimpleGeometry.prototype.getStride = function() {
|
||||
/**
|
||||
* @param {module:ol/geom/GeometryLayout} layout Layout.
|
||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||
* @protected
|
||||
*/
|
||||
SimpleGeometry.prototype.setFlatCoordinatesInternal = function(layout, flatCoordinates) {
|
||||
this.stride = getStrideForLayout(layout);
|
||||
|
||||
@@ -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(
|
||||
if (!sketchLineGeom) {
|
||||
sketchLineGeom = new LineString(ring.getFlatCoordinates(), ring.getLayout());
|
||||
this.sketchLine_.setGeometry(sketchLineGeom);
|
||||
} else {
|
||||
sketchLineGeom.setFlatCoordinatesInternal(
|
||||
ring.getLayout(), ring.getFlatCoordinates());
|
||||
sketchLineGeom.changed();
|
||||
}
|
||||
} else if (this.sketchLineCoords_) {
|
||||
sketchLineGeom = /** @type {module:ol/geom/LineString} */ (this.sketchLine_.getGeometry());
|
||||
sketchLineGeom.setCoordinates(this.sketchLineCoords_);
|
||||
|
||||
@@ -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,8 +261,7 @@ describe('ol.rendering.style.Text', function() {
|
||||
|
||||
it('renders text along a MultiLineString', function(done) {
|
||||
createMap('canvas');
|
||||
let line = new LineString();
|
||||
line.setFlatCoordinates('XY', nicePath);
|
||||
let line = new LineString(nicePath, 'XY');
|
||||
const geom = new MultiLineString(null);
|
||||
geom.appendLineString(line);
|
||||
line = line.clone();
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user