ol/source/Cluster.js
父类
ol/source/Cluster-Cluster
主要功能
聚类点数据源。
参数:VectorSource({})
参数 | 类型 | 说明 |
---|
distance | number (defaults to 20) | 要素在多少像素距离内会被分为一类 | source | module:ol/source/Vector~VectorSource | 数据源 | | | |
方法
函数名 | 参数 | 源码 | 返回值类型 | 功能 |
---|
setDistance(distance) | distance number 像素距离 | source/Cluster.js, line 169 | 无 | 设定聚类像素距离 |
import EventType from '../events/EventType.js';
import Feature from '../Feature.js';
import GeometryType from '../geom/GeometryType.js';
import Point from '../geom/Point.js';
import VectorSource from './Vector.js';
import {add as addCoordinate, scale as scaleCoordinate} from '../coordinate.js';
import {assert} from '../asserts.js';
import {
buffer,
createEmpty,
createOrUpdateFromCoordinate,
getCenter,
} from '../extent.js';
import {getUid} from '../util.js';
class Cluster extends VectorSource {
constructor(options) {
super({
attributions: options.attributions,
wrapX: options.wrapX,
});
this.resolution = undefined;
this.distance = options.distance !== undefined ? options.distance : 20;
this.minDistance = options.minDistance || 0;
this.interpolationRatio = 0;
this.features = [];
this.geometryFunction =
options.geometryFunction ||
function (feature) {
const geometry = feature.getGeometry();
assert(geometry.getType() == GeometryType.POINT, 10);
return geometry;
};
this.createCustomCluster_ = options.createCluster;
this.source = null;
this.boundRefresh_ = this.refresh.bind(this);
this.updateDistance(this.distance, this.minDistance);
this.setSource(options.source || null);
}
clear(opt_fast) {
this.features.length = 0;
super.clear(opt_fast);
}
getDistance() {
return this.distance;
}
getSource() {
return this.source;
}
loadFeatures(extent, resolution, projection) {
this.source.loadFeatures(extent, resolution, projection);
if (resolution !== this.resolution) {
this.resolution = resolution;
this.refresh();
}
}
setDistance(distance) {
this.updateDistance(distance, this.minDistance);
}
setMinDistance(minDistance) {
this.updateDistance(this.distance, minDistance);
}
getMinDistance() {
return this.minDistance;
}
setSource(source) {
if (this.source) {
this.source.removeEventListener(EventType.CHANGE, this.boundRefresh_);
}
this.source = source;
if (source) {
source.addEventListener(EventType.CHANGE, this.boundRefresh_);
}
this.refresh();
}
refresh() {
this.clear();
this.cluster();
this.addFeatures(this.features);
}
updateDistance(distance, minDistance) {
const ratio =
distance === 0 ? 0 : Math.min(minDistance, distance) / distance;
const changed =
distance !== this.distance || this.interpolationRatio !== ratio;
this.distance = distance;
this.minDistance = minDistance;
this.interpolationRatio = ratio;
if (changed) {
this.refresh();
}
}
cluster() {
if (this.resolution === undefined || !this.source) {
return;
}
const extent = createEmpty();
const mapDistance = this.distance * this.resolution;
const features = this.source.getFeatures();
const clustered = {};
for (let i = 0, ii = features.length; i < ii; i++) {
const feature = features[i];
if (!(getUid(feature) in clustered)) {
const geometry = this.geometryFunction(feature);
if (geometry) {
const coordinates = geometry.getCoordinates();
createOrUpdateFromCoordinate(coordinates, extent);
buffer(extent, mapDistance, extent);
const neighbors = this.source
.getFeaturesInExtent(extent)
.filter(function (neighbor) {
const uid = getUid(neighbor);
if (uid in clustered) {
return false;
}
clustered[uid] = true;
return true;
});
this.features.push(this.createCluster(neighbors, extent));
}
}
}
}
createCluster(features, extent) {
const centroid = [0, 0];
for (let i = features.length - 1; i >= 0; --i) {
const geometry = this.geometryFunction(features[i]);
if (geometry) {
addCoordinate(centroid, geometry.getCoordinates());
} else {
features.splice(i, 1);
}
}
scaleCoordinate(centroid, 1 / features.length);
const searchCenter = getCenter(extent);
const ratio = this.interpolationRatio;
const geometry = new Point([
centroid[0] * (1 - ratio) + searchCenter[0] * ratio,
centroid[1] * (1 - ratio) + searchCenter[1] * ratio,
]);
if (this.createCustomCluster_) {
return this.createCustomCluster_(geometry, features);
} else {
return new Feature({
geometry,
features,
});
}
}
}
export default Cluster;
|