Adding original properties to cloned geometry

This commit is contained in:
Michał Zielański
2020-08-26 15:11:11 +02:00
parent 62842d5508
commit 8e0a61ac5f
12 changed files with 45 additions and 5 deletions

View File

@@ -37,7 +37,13 @@ class Circle extends SimpleGeometry {
* @api
*/
clone() {
return new Circle(this.flatCoordinates.slice(), undefined, this.layout);
const circle = new Circle(
this.flatCoordinates.slice(),
undefined,
this.layout
);
circle.importPropertiesFrom(this);
return circle;
}
/**

View File

@@ -109,6 +109,17 @@ class Geometry extends BaseObject {
return abstract();
}
/**
* Import properties from another geometry.
* @param {Geometry} geometry Source geometry.
* @protected
*/
importPropertiesFrom(geometry) {
if (geometry.hasProperties()) {
this.setProperties(geometry.getProperties(), true);
}
}
/**
* @abstract
* @param {number} x X.

View File

@@ -69,6 +69,7 @@ class GeometryCollection extends Geometry {
clone() {
const geometryCollection = new GeometryCollection(null);
geometryCollection.setGeometries(this.geometries_);
geometryCollection.importPropertiesFrom(this);
return geometryCollection;
}

View File

@@ -87,7 +87,12 @@ class LineString extends SimpleGeometry {
* @api
*/
clone() {
return new LineString(this.flatCoordinates.slice(), this.layout);
const lineString = new LineString(
this.flatCoordinates.slice(),
this.layout
);
lineString.importPropertiesFrom(this);
return lineString;
}
/**

View File

@@ -102,11 +102,13 @@ class MultiLineString extends SimpleGeometry {
* @api
*/
clone() {
return new MultiLineString(
const multiLineString = new MultiLineString(
this.flatCoordinates.slice(),
this.layout,
this.ends_.slice()
);
multiLineString.importPropertiesFrom(this);
return multiLineString;
}
/**

View File

@@ -61,6 +61,7 @@ class MultiPoint extends SimpleGeometry {
this.flatCoordinates.slice(),
this.layout
);
multiPoint.importPropertiesFrom(this);
return multiPoint;
}

View File

@@ -155,11 +155,14 @@ class MultiPolygon extends SimpleGeometry {
newEndss[i] = this.endss_[i].slice();
}
return new MultiPolygon(
const multiPolygon = new MultiPolygon(
this.flatCoordinates.slice(),
this.layout,
newEndss
);
multiPolygon.importPropertiesFrom(this);
return multiPolygon;
}
/**

View File

@@ -30,6 +30,7 @@ class Point extends SimpleGeometry {
*/
clone() {
const point = new Point(this.flatCoordinates.slice(), this.layout);
point.importPropertiesFrom(this);
return point;
}

View File

@@ -118,11 +118,13 @@ class Polygon extends SimpleGeometry {
* @api
*/
clone() {
return new Polygon(
const polygon = new Polygon(
this.flatCoordinates.slice(),
this.layout,
this.ends_.slice()
);
polygon.importPropertiesFrom(this);
return polygon;
}
/**

View File

@@ -9,11 +9,14 @@ describe('ol.geom.Circle', function () {
describe('#clone', function () {
it('returns a clone', function () {
circle.setProperties({foo: 'bar', baz: null});
const clone = circle.clone();
expect(clone).to.be.an(Circle);
expect(clone.getCenter()).to.eql(circle.getCenter());
expect(clone.getCenter()).not.to.be(circle.getCenter());
expect(clone.getRadius()).to.be(circle.getRadius());
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
});
});

View File

@@ -97,6 +97,7 @@ describe('ol.geom.GeometryCollection', function () {
]);
const poly = new Polygon([outer, inner1, inner2]);
const multi = new GeometryCollection([point, line, poly]);
multi.setProperties({foo: 'bar', baz: null});
const clone = multi.clone();
expect(clone).to.not.be(multi);
const geometries = clone.getGeometries();
@@ -106,6 +107,7 @@ describe('ol.geom.GeometryCollection', function () {
[30, 40],
]);
expect(geometries[2].getCoordinates()).to.eql([outer, inner1, inner2]);
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
});
it('does a deep clone', function () {

View File

@@ -276,8 +276,11 @@ describe('ol.geom.MultiPolygon', function () {
describe('#clone()', function () {
it('has the expected endss_', function () {
multiPolygon.setProperties({foo: 'bar', baz: null});
const clone = multiPolygon.clone();
expect(multiPolygon.endss_).to.eql(clone.endss_);
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
});
});