Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accounting for IDs of cluster features when the promoteID properties is set #4899

Merged
merged 24 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## main

### ✨ Features and improvements
- When clustering circles and the promoteId is set to some parameter, the promoted id is used on non-clustered features and the cluster_id is used on clustered features. Previously the id was undefined for non-clustered features.
- _...Add new stuff here..._

### 🐞 Bug fixes
Expand Down
71 changes: 71 additions & 0 deletions src/data/feature_index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {FeatureIndex} from './feature_index';
import {OverscaledTileID} from '../source/tile_id';
import {VectorTileFeature} from '@mapbox/vector-tile';
popkinj marked this conversation as resolved.
Show resolved Hide resolved
describe('FeatureIndex', () => {
describe('getId', () => {
const tileID = new OverscaledTileID(0, 0, 0, 0, 0);

test('converts boolean ids to numbers', () => {
const featureIndex = new FeatureIndex(tileID, 'someProperty');
const feature = {
id: true,
popkinj marked this conversation as resolved.
Show resolved Hide resolved
properties: {
someProperty: true
},
geometry: {
type: 'Point',
coordinates: [0, 0]
},
extent: 4096,
type: 1,
loadGeometry: () => [],
toGeoJSON: () => ({})
} as unknown as VectorTileFeature;
expect(featureIndex.getId(feature, 'sourceLayer')).toBe(1);
});

test('uses cluster_id when cluster is true and id is undefined', () => {
const featureIndex = new FeatureIndex(tileID, 'someProperty');
const feature = {
id: 1,
properties: {
cluster: true,
cluster_id: '123',
someProperty: undefined
},
geometry: {
type: 'Point',
coordinates: [0, 0]
},
extent: 4096,
type: 1,
loadGeometry: () => [],
toGeoJSON: () => ({})
} as unknown as VectorTileFeature;

expect(featureIndex.getId(feature, 'sourceLayer')).toBe(123); // cluster_id converted to number
});

test('ignores cluster_id when cluster is false', () => {
const featureIndex = new FeatureIndex(tileID, 'someProperty');
const feature = {
id: 1,
properties: {
cluster: false,
cluster_id: '123',
someProperty: undefined
},
geometry: {
type: 'Point',
coordinates: [0, 0]
},
extent: 4096,
type: 1,
loadGeometry: () => [],
toGeoJSON: () => ({})
} as unknown as VectorTileFeature;

expect(featureIndex.getId(feature, 'sourceLayer')).toBeUndefined();
});
});
});
5 changes: 5 additions & 0 deletions src/data/feature_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ export class FeatureIndex {
const propName = typeof this.promoteId === 'string' ? this.promoteId : this.promoteId[sourceLayerId];
id = feature.properties[propName] as string | number;
if (typeof id === 'boolean') id = Number(id);

// When cluster is true, the id is the cluster_id even though promoteId is set
if (id === undefined && feature.properties?.cluster && this.promoteId) {
id = Number(feature.properties.cluster_id);
}
}
return id;
}
Expand Down