Skip to content

Commit

Permalink
Merge pull request #981 from simon-leech/cluster-resolution
Browse files Browse the repository at this point in the history
Vector Cluster Changes
  • Loading branch information
RobAndrewHurst authored Oct 24, 2023
2 parents 4e1dfb2 + 3f6ae31 commit a4a72f6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
3 changes: 2 additions & 1 deletion lib/layer/format/_format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export default {
mvt,
cluster: vector,
geojson: vector,
wkt: vector
wkt: vector,
vector
}
87 changes: 56 additions & 31 deletions lib/layer/format/vector.mjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
export default layer => {

if (!layer.srid) {
console.warn(`No SRID provided for ${layer.key}`)
}
// 3857 is assumed to be the default SRID for all vector format layer.
layer.srid ??= '3857'

if (layer.properties) {
console.warn(`Layer: ${layer.key},layer.properties{} are no longer required for wkt & geojson datasets.`)
console.warn(`Layer: ${layer.key}, layer.properties{} are no longer required for wkt & geojson datasets.`)
}

// Set default layer params if nullish.
layer.params ??= {}

// If layer configuration is wrong and contains both cluster.distance and cluster.resolution, error and return
if (layer.cluster?.distance && layer.cluster?.resolution) {

console.error(`Layer: ${layer.key}, cluster.distance and cluster.resolution are mutually exclusive. You cannot use them both on the same layer. Please remove one of them. `)

return;
};

if (layer.cluster?.resolution) {
layer.format = 'cluster';
layer.params.viewport = true;
layer.params.z = true;
layer.params.resolution = layer.cluster.resolution;
layer.params.template = layer.cluster.hexgrid ? 'cluster_hex' : 'cluster';
}
clusterConfig(layer)

// Assign style object if nullish.
layer.style ??= {}

layer.srid ??= '4326'
layer.style ??= {};

layer.setSource = (features) => {

Expand Down Expand Up @@ -106,6 +90,7 @@ export default layer => {
locale: layer.mapview.locale.key,
layer: layer.key,
table,
srid: layer.srid,
filter: layer.filter?.current,
...layer.params
})}`)
Expand Down Expand Up @@ -144,19 +129,11 @@ export default layer => {
}

// Change method for the cluster feature properties and layer stats.
layer.L.on('change', e => {

// Do not process cluster for non cluster layers.
if (!layer.cluster) return;
layer.cluster?.distance && layer.L.on('change', e => {

// To prevent layer.L.change() from crashing if called before data is loaded.
if (!layer.cluster.source) return;

// The OL cluster must not be processed with a resolution.
if (layer.cluster.resolution) return;

if (!layer.cluster?.distance) return;

delete layer.max_size;

const feature_counts = layer.cluster.source.getFeatures().map(F => {
Expand Down Expand Up @@ -191,5 +168,53 @@ export default layer => {
// Calculate max_size for cluster size styling.
layer.max_size = Math.max(...feature_counts)
})
}

function clusterConfig(layer) {

// The clusterConfig can not work without the layer having a cluster config object.
if (typeof layer.cluster !== 'object') return;

// Check if both cluster.distance and cluster.resolution are set.
if (layer.cluster.distance && layer.cluster.resolution) {
console.warn(`Layer: ${layer.key}, cluster.distance and cluster.resolution are mutually exclusive. You cannot use them both on the same layer. Please remove one of them. `)
return;
};

// Check if neither cluster.distance and cluster.resolution are set.
if (!layer.cluster.distance && !layer.cluster.resolution) {
console.warn(`Layer: ${layer.key}, cluster.distance or cluster.resolution must be set.`)
return;
};

// If cluster.resolution is used, the layer srid must be set to 3857.
if (layer.cluster.resolution) {

// Check if resolution is numeric.
if (typeof layer.cluster.resolution === 'number') {
// Assign resolution as float.
layer.params.resolution = parseFloat(layer.cluster.resolution);
}
// Otherwise, warn and return.
else {
console.warn(`Layer: ${layer.key}, cluster.resolution must be a number.`)
return;
}

// Check if srid is set to 4326, not allowed for cluster layer
if (layer.srid === '4326') {
console.warn(`Layer: ${layer.key}, srid 4326 is not allowed for cluster.resolution layers.`)
return;
};

// Provide default params for resolution cluster.
layer.params.viewport = true;
layer.params.z = true;

// Format is cluster if resolution is set.
layer.format = 'cluster';

// Assign default template.
layer.params.template ??= layer.cluster.hexgrid ? 'cluster_hex' : 'cluster';
}
}
2 changes: 1 addition & 1 deletion lib/mapp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ self.mapp = (function (mapp) {

Object.assign(mapp, {
version: '4.7.2',
hash: 'f836737e4c92e114f788dc6461ffcd5ecddba295',
hash: '4026463d6e57c02f0da8ad676d81f59e47baacc8',
language: hooks.current.language || 'en',

dictionaries,
Expand Down

0 comments on commit a4a72f6

Please sign in to comment.