Accept flat coordinates in LineString constructor

This commit is contained in:
ahocevar
2018-07-07 16:04:43 +02:00
parent c0d04ea077
commit 160f1bc286
14 changed files with 57 additions and 67 deletions

View File

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

View File

@@ -272,8 +272,7 @@ GML3.prototype.readCurve_ = function(node, objectStack) {
const flatCoordinates = pushParseAndPop([null], const flatCoordinates = pushParseAndPop([null],
this.CURVE_PARSERS_, node, objectStack, this); this.CURVE_PARSERS_, node, objectStack, this);
if (flatCoordinates) { if (flatCoordinates) {
const lineString = new LineString(null); const lineString = new LineString(flatCoordinates, GeometryLayout.XYZ);
lineString.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates);
return lineString; return lineString;
} else { } else {
return undefined; return undefined;

View File

@@ -388,8 +388,7 @@ GMLBase.prototype.polygonMemberParser_ = function(node, objectStack) {
GMLBase.prototype.readLineString = function(node, objectStack) { GMLBase.prototype.readLineString = function(node, objectStack) {
const flatCoordinates = this.readFlatCoordinatesFromNode_(node, objectStack); const flatCoordinates = this.readFlatCoordinatesFromNode_(node, objectStack);
if (flatCoordinates) { if (flatCoordinates) {
const lineString = new LineString(null); const lineString = new LineString(flatCoordinates, GeometryLayout.XYZ);
lineString.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates);
return lineString; return lineString;
} else { } else {
return undefined; return undefined;

View File

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

View File

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

View File

@@ -942,9 +942,7 @@ function readGxTrack(node, objectStack) {
for (let i = 0, ii = Math.min(flatCoordinates.length, whens.length); i < ii; ++i) { for (let i = 0, ii = Math.min(flatCoordinates.length, whens.length); i < ii; ++i) {
flatCoordinates[4 * i + 3] = whens[i]; flatCoordinates[4 * i + 3] = whens[i];
} }
const lineString = new LineString(null); return new LineString(flatCoordinates, GeometryLayout.XYZM);
lineString.setFlatCoordinates(GeometryLayout.XYZM, flatCoordinates);
return lineString;
} }
@@ -1025,8 +1023,7 @@ function readLineString(node, objectStack) {
const flatCoordinates = const flatCoordinates =
readFlatCoordinatesFromNode(node, objectStack); readFlatCoordinatesFromNode(node, objectStack);
if (flatCoordinates) { if (flatCoordinates) {
const lineString = new LineString(null); const lineString = new LineString(flatCoordinates, GeometryLayout.XYZ);
lineString.setFlatCoordinates(GeometryLayout.XYZ, flatCoordinates);
lineString.setProperties(properties); lineString.setProperties(properties);
return lineString; return lineString;
} else { } else {

View File

@@ -340,13 +340,13 @@ MVT.prototype.createFeature_ = function(pbf, rawFeature, opt_options) {
} }
} else { } else {
geom = geometryType === GeometryType.POINT ? new Point(flatCoordinates, GeometryLayout.XY) : 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.POLYGON ? new Polygon(flatCoordinates, GeometryLayout.XY, ends) :
geometryType === GeometryType.MULTI_POINT ? new MultiPoint (null) : geometryType === GeometryType.MULTI_POINT ? new MultiPoint (null) :
geometryType === GeometryType.MULTI_LINE_STRING ? new MultiLineString(null) : geometryType === GeometryType.MULTI_LINE_STRING ? new MultiLineString(null) :
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); geom.setFlatCoordinates(GeometryLayout.XY, flatCoordinates, ends);
} }
feature = new this.featureClass_(); feature = new this.featureClass_();

View File

@@ -177,8 +177,7 @@ OSMXML.prototype.readFeaturesFromNode = function(node, opt_options) {
// closed way // closed way
geometry = new Polygon(flatCoordinates, GeometryLayout.XY, [flatCoordinates.length]); geometry = new Polygon(flatCoordinates, GeometryLayout.XY, [flatCoordinates.length]);
} else { } else {
geometry = new LineString(null); geometry = new LineString(flatCoordinates, GeometryLayout.XY);
geometry.setFlatCoordinates(GeometryLayout.XY, flatCoordinates);
} }
transformWithOptions(geometry, false, options); transformWithOptions(geometry, false, options);
const feature = new Feature(geometry); const feature = new Feature(geometry);

View File

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

View File

@@ -167,10 +167,8 @@ MultiLineString.prototype.getLineString = function(index) {
if (index < 0 || this.ends_.length <= index) { if (index < 0 || this.ends_.length <= index) {
return null; return null;
} }
const lineString = new LineString(null); return new LineString(this.flatCoordinates.slice(
lineString.setFlatCoordinates(this.layout, this.flatCoordinates.slice( index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]), this.layout);
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]));
return lineString;
}; };
@@ -188,8 +186,7 @@ MultiLineString.prototype.getLineStrings = function() {
let offset = 0; let offset = 0;
for (let i = 0, ii = ends.length; i < ii; ++i) { for (let i = 0, ii = ends.length; i < ii; ++i) {
const end = ends[i]; const end = ends[i];
const lineString = new LineString(null); const lineString = new LineString(flatCoordinates.slice(offset, end), layout);
lineString.setFlatCoordinates(layout, flatCoordinates.slice(offset, end));
lineStrings.push(lineString); lineStrings.push(lineString);
offset = end; offset = end;
} }

View File

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

View File

@@ -733,12 +733,18 @@ Draw.prototype.modifyDrawing_ = function(event) {
if (geometry instanceof Polygon && if (geometry instanceof Polygon &&
this.mode_ !== Mode.POLYGON) { this.mode_ !== Mode.POLYGON) {
if (!this.sketchLine_) { if (!this.sketchLine_) {
this.sketchLine_ = new Feature(new LineString(null)); this.sketchLine_ = new Feature();
} }
const ring = geometry.getLinearRing(0); const ring = geometry.getLinearRing(0);
sketchLineGeom = /** @type {module:ol/geom/LineString} */ (this.sketchLine_.getGeometry()); sketchLineGeom = /** @type {module:ol/geom/LineString} */ (this.sketchLine_.getGeometry());
sketchLineGeom.setFlatCoordinates( if (!sketchLineGeom) {
ring.getLayout(), ring.getFlatCoordinates()); sketchLineGeom = new LineString(ring.getFlatCoordinates(), ring.getLayout());
this.sketchLine_.setGeometry(sketchLineGeom);
} else {
sketchLineGeom.setFlatCoordinatesInternal(
ring.getLayout(), ring.getFlatCoordinates());
sketchLineGeom.changed();
}
} else if (this.sketchLineCoords_) { } else if (this.sketchLineCoords_) {
sketchLineGeom = /** @type {module:ol/geom/LineString} */ (this.sketchLine_.getGeometry()); sketchLineGeom = /** @type {module:ol/geom/LineString} */ (this.sketchLine_.getGeometry());
sketchLineGeom.setCoordinates(this.sketchLineCoords_); sketchLineGeom.setCoordinates(this.sketchLineCoords_);

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]; const polygon = [151, 17, 163, 22, 159, 30, 150, 30, 143, 24, 151, 17];
function createLineString(coords, textAlign, maxAngle, strokeColor, strokeWidth, scale) { function createLineString(coords, textAlign, maxAngle, strokeColor, strokeWidth, scale) {
let geom = new LineString(); let geom = new LineString(coords, 'XY');
geom.setFlatCoordinates('XY', coords);
let style = new Style({ let style = new Style({
stroke: new Stroke({ stroke: new Stroke({
color: 'red' color: 'red'
@@ -262,8 +261,7 @@ describe('ol.rendering.style.Text', function() {
it('renders text along a MultiLineString', function(done) { it('renders text along a MultiLineString', function(done) {
createMap('canvas'); createMap('canvas');
let line = new LineString(); let line = new LineString(nicePath, 'XY');
line.setFlatCoordinates('XY', nicePath);
const geom = new MultiLineString(null); const geom = new MultiLineString(null);
geom.appendLineString(line); geom.appendLineString(line);
line = line.clone(); line = line.clone();

View File

@@ -4,10 +4,10 @@ import LineString from '../../../../src/ol/geom/LineString.js';
describe('ol.geom.LineString', function() { 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() { expect(function() {
return new LineString(null); return new LineString(null);
}).not.to.throwException(); }).to.throwException();
}); });
describe('construct empty', function() { describe('construct empty', function() {