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

Enable auto generation of ids for GeoJSON sources #7043

Merged
merged 2 commits into from Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -26,14 +26,14 @@
"csscolorparser": "~1.0.2",
"earcut": "^2.1.3",
"geojson-rewind": "^0.3.0",
"geojson-vt": "^3.1.4",
"geojson-vt": "^3.2.0",
"gl-matrix": "^2.6.1",
"grid-index": "^1.0.0",
"minimist": "0.0.8",
"pbf": "^3.0.5",
"quickselect": "^1.0.0",
"rw": "^1.3.3",
"supercluster": "^4.0.1",
"supercluster": "^4.1.1",
"tinyqueue": "^1.1.0",
"vt-pbf": "^3.0.1"
},
Expand Down
3 changes: 2 additions & 1 deletion src/source/geojson_source.js
Expand Up @@ -128,7 +128,8 @@ class GeoJSONSource extends Evented implements Source {
tolerance: (options.tolerance !== undefined ? options.tolerance : 0.375) * scale,
extent: EXTENT,
maxZoom: this.maxzoom,
lineMetrics: options.lineMetrics || false
lineMetrics: options.lineMetrics || false,
generateId: options.generateId || false
},
superclusterOptions: {
maxZoom: options.clusterMaxZoom !== undefined ?
Expand Down
3 changes: 3 additions & 0 deletions src/source/source_cache.js
Expand Up @@ -217,6 +217,9 @@ class SourceCache extends Evented {
}

this._cache.reset();
//Reset the SourceCache when the source has been reloaded. For GeoJSON sources,
// the `id` of features is not guaranteed to be identical when updated
this._state = new SourceFeatureState();

for (const i in this._tiles) {
if (this._tiles[i].state !== "errored") this._reloadTile(i, 'reloading');
Expand Down
5 changes: 5 additions & 0 deletions src/style-spec/reference/v8.json
Expand Up @@ -377,6 +377,11 @@
"type": "boolean",
"default": false,
"doc": "Whether to calculate line distance metrics. This is required for line layers that specify `line-gradient` values."
},
"generateId": {
"type": "boolean",
"default": false,
"doc": "Whether to generate ids for the geojson features. When enabled, the `feature.id` property will be auto assigned based on it's index in the `features` array, over-writing any previous values."
}
},
"source_video": {
Expand Down
3 changes: 2 additions & 1 deletion src/style-spec/types.js
Expand Up @@ -119,7 +119,8 @@ export type GeoJSONSourceSpecification = {|
"cluster"?: boolean,
"clusterRadius"?: number,
"clusterMaxZoom"?: number,
"lineMetrics"?: boolean
"lineMetrics"?: boolean,
"generateId"?: boolean
|}

export type VideoSourceSpecification = {|
Expand Down
8 changes: 7 additions & 1 deletion src/ui/map.js
Expand Up @@ -1339,11 +1339,17 @@ class Map extends Camera {
*
* @param {Object} [feature] Feature identifier. Feature objects returned from
* {@link Map#queryRenderedFeatures} or event handlers can be used as feature identifiers.
* @param {string} [feature.id] Unique id of the feature.
* @param {string} [feature.source] The Id of the vector source or GeoJSON source for the feature.
* @param {string} [feature.sourceLayer] (optional) *For vector tile sources, the sourceLayer is
* required.*
* @param {string} [feature.id] Unique id of the feature.
* @param {Object} state A set of key-value pairs. The values should be valid JSON types.
*
* This method requires the `feature.id` attribute on data sets. For GeoJSON sources without
* feature ids, set the `generateIds` option in the `GeoJSONSourceSpecification` to auto-assign them. This
* option assigns ids based on a feature's index in the source data. Changing the feature data using
* `map.getSource('some id').setData(..)` resets the cache of feature states and requires the
* caller re-apply the state as needed with the updated `id` values.
*/
setFeatureState(feature: { source: string; sourceLayer?: string; id: string; }, state: Object) {
this.style.setFeatureState(feature, state);
Expand Down
6 changes: 4 additions & 2 deletions test/unit/source/geojson_source.test.js
Expand Up @@ -145,7 +145,8 @@ test('GeoJSONSource#update', (t) => {
maxZoom: 10,
tolerance: 4,
buffer: 256,
lineMetrics: false
lineMetrics: false,
generateId: true
});
t.end();
}
Expand All @@ -155,7 +156,8 @@ test('GeoJSONSource#update', (t) => {
data: {},
maxzoom: 10,
tolerance: 0.25,
buffer: 16
buffer: 16,
generateId: true
}, mockDispatcher).load();
});

Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Expand Up @@ -4355,9 +4355,9 @@ geojson-rewind@^0.3.0:
concat-stream "~1.6.0"
minimist "1.2.0"

geojson-vt@^3.1.4:
version "3.1.4"
resolved "https://registry.yarnpkg.com/geojson-vt/-/geojson-vt-3.1.4.tgz#c8ffefbe3613d3ad2747e963b0b63b9e62ff11b8"
geojson-vt@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/geojson-vt/-/geojson-vt-3.2.0.tgz#039cea549df5f892c48cff8eb66c2e217d61c241"

get-caller-file@^1.0.1:
version "1.0.2"
Expand Down Expand Up @@ -9613,9 +9613,9 @@ sugarss@^1.0.0:
dependencies:
postcss "^6.0.14"

supercluster@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/supercluster/-/supercluster-4.0.1.tgz#eead7ab49f50322b265e0087859ebcabdc5c2ed8"
supercluster@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/supercluster/-/supercluster-4.1.1.tgz#cf13c3b28a3fb3db5290bfad7f524e244bd4ce78"
dependencies:
kdbush "^2.0.1"

Expand Down