Accept linestrings and flat coordinates in MultiLineString constructor
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -323,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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -584,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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1107,8 +1105,7 @@ function readMultiGeometry(node, objectStack) {
|
||||
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);
|
||||
|
||||
@@ -343,13 +343,9 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) {
|
||||
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(null) :
|
||||
geometryType === GeometryType.MULTI_LINE_STRING ? new MultiLineString(flatCoordinates, GeometryLayout.XY, ends) :
|
||||
null;
|
||||
}
|
||||
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_();
|
||||
if (this.geometryName_) {
|
||||
feature.setGeometryName(this.geometryName_);
|
||||
|
||||
@@ -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;
|
||||
|
||||
if (Array.isArray(coordinates[0])) {
|
||||
this.setCoordinates(coordinates, opt_layout);
|
||||
} else if (opt_layout !== undefined && opt_ends) {
|
||||
this.setFlatCoordinatesInternal(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.setFlatCoordinatesInternal(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());
|
||||
};
|
||||
|
||||
|
||||
@@ -223,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);
|
||||
};
|
||||
|
||||
|
||||
@@ -251,15 +267,12 @@ 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 = [];
|
||||
@@ -268,37 +281,5 @@ MultiLineString.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.
|
||||
*/
|
||||
MultiLineString.prototype.setFlatCoordinates = function(layout, flatCoordinates, ends) {
|
||||
this.setFlatCoordinatesInternal(layout, flatCoordinates);
|
||||
this.ends_ = ends;
|
||||
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;
|
||||
|
||||
@@ -262,8 +262,7 @@ describe('ol.rendering.style.Text', function() {
|
||||
it('renders text along a MultiLineString', function(done) {
|
||||
createMap('canvas');
|
||||
let line = new LineString(nicePath, 'XY');
|
||||
const geom = new MultiLineString(null);
|
||||
geom.appendLineString(line);
|
||||
const geom = new MultiLineString([line]);
|
||||
line = line.clone();
|
||||
line.translate(0, 50);
|
||||
geom.appendLineString(line);
|
||||
|
||||
@@ -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]);
|
||||
|
||||
Reference in New Issue
Block a user