allow hydrating clustered features

This commit is contained in:
jkonieczny
2021-07-08 20:06:18 +02:00
parent e791bf4e33
commit 9fd25dddbf
2 changed files with 28 additions and 0 deletions
+11
View File
@@ -38,6 +38,8 @@ import {getUid} from '../util.js';
* return feature.getGeometry(); * return feature.getGeometry();
* } * }
* ``` * ```
* @property {function(Feature, Array<Feature>):any} [hydrate] Function called after creating
* cluster feature
* See {@link module:ol/geom/Polygon~Polygon#getInteriorPoint} for a way to get a cluster * See {@link module:ol/geom/Polygon~Polygon#getInteriorPoint} for a way to get a cluster
* calculation point for polygons. * calculation point for polygons.
* @property {VectorSource} [source] Source. * @property {VectorSource} [source] Source.
@@ -108,6 +110,12 @@ class Cluster extends VectorSource {
return geometry; return geometry;
}; };
/**
* @type {function(Feature, Array<Feature>):any}
* @protected
*/
this.hydrate = options.hydrate;
/** /**
* @type {VectorSource} * @type {VectorSource}
* @protected * @protected
@@ -295,6 +303,9 @@ class Cluster extends VectorSource {
centroid[1] * (1 - ratio) + searchCenter[1] * ratio, centroid[1] * (1 - ratio) + searchCenter[1] * ratio,
]); ]);
const cluster = new Feature(geometry); const cluster = new Feature(geometry);
if (this.hydrate) {
this.hydrate(cluster, features);
}
cluster.set('features', features, true); cluster.set('features', features, true);
return cluster; return cluster;
} }
@@ -75,6 +75,23 @@ describe('ol.source.Cluster', function () {
expect(source.getFeatures().length).to.be(1); expect(source.getFeatures().length).to.be(1);
expect(source.getFeatures()[0].get('features').length).to.be(2); expect(source.getFeatures()[0].get('features').length).to.be(2);
}); });
it('hydrates cluster feature with additional fields', function () {
const feature1 = new Feature(new Point([0, 0]));
const feature2 = new Feature(new Point([0, 0]));
feature1.set('value', 1);
feature2.set('value', 2);
const source = new Cluster({
source: new VectorSource({
features: [feature1, feature2],
}),
hydrate: function (clusterFeature, features) {
clusterFeature.set('sum', 3);
},
});
source.loadFeatures(extent, 1, projection);
expect(source.getFeatures().length).to.be(1);
expect(source.getFeatures()[0].get('sum')).to.be(3);
});
}); });
describe('#setDistance', function () { describe('#setDistance', function () {