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

Simplify clusterProperties expression #7784

Merged
merged 4 commits into from
Jan 17, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions debug/cluster.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
"cluster": true,
"clusterRadius": 50,
"clusterProperties": {
"max": ["max", 0, ["get", "scalerank"]],
"sum": ["+", 0, ["get", "scalerank"]],
"has_island": ["any", false, ["==", ["get", "featureclass"], "island"]]
"max": ["max", ["get", "scalerank"]],
"sum": ["+", ["get", "scalerank"]],
"has_island": ["any", ["==", ["get", "featureclass"], "island"]]
}
});
map.addLayer({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"potpack": "^1.0.1",
"quickselect": "^1.0.0",
"rw": "^1.3.3",
"supercluster": "^5.0.0",
"supercluster": "^6.0.0",
"tinyqueue": "^1.1.0",
"vt-pbf": "^3.0.1"
},
Expand Down
13 changes: 1 addition & 12 deletions src/source/geojson_worker_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,37 +298,26 @@ class GeoJSONWorkerSource extends VectorTileWorkerSource {
function getSuperclusterOptions({superclusterOptions, clusterProperties}) {
if (!clusterProperties || !superclusterOptions) return superclusterOptions;

const initialValues = {};
const mapExpressions = {};
const reduceExpressions = {};
const globals = {accumulated: null, zoom: 0};
const feature = {properties: null};
const propertyNames = Object.keys(clusterProperties);

for (const key of propertyNames) {
const [operator, initialExpression, mapExpression] = clusterProperties[key];
const [operator, mapExpression] = clusterProperties[key];

const initialExpressionParsed = createExpression(initialExpression);
const mapExpressionParsed = createExpression(mapExpression);
const reduceExpressionParsed = createExpression(
typeof operator === 'string' ? [operator, ['accumulated'], ['get', key]] : operator);

assert(initialExpressionParsed.result === 'success');
assert(mapExpressionParsed.result === 'success');
assert(reduceExpressionParsed.result === 'success');

initialValues[key] = (initialExpressionParsed.value: any).evaluate(globals);
mapExpressions[key] = mapExpressionParsed.value;
reduceExpressions[key] = reduceExpressionParsed.value;
}

superclusterOptions.initial = () => {
const properties = {};
for (const key of propertyNames) {
properties[key] = initialValues[key];
}
return properties;
};
superclusterOptions.map = (pointProperties) => {
feature.properties = pointProperties;
const properties = {};
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@
},
"clusterProperties": {
"type": "*",
"doc": "An object defining custom properties on the generated clusters if clustering is enabled, aggregating values from clustered points. Has the form `{\"property_name\": [operator, initial_expression, map_expression]}`. `operator` is any expression function that accepts at least 2 operands (e.g. `\"+\"` or `\"max\"`) — it accumulates the property value from clusters/points the cluster contains; `initial_expression` evaluates the initial value of the property before accummulating other points/clusters; `map_expression` produces the value of a single point.\n\nExample: `{\"sum\": [\"+\", 0, [\"get\", \"scalerank\"]]}`.\n\nFor more advanced use cases, in place of `operator`, you can use a custom reduce expression that references a special `[\"accumulated\"]` value, e.g.:\n`{\"sum\": [[\"+\", [\"accumulated\"], [\"get\", \"sum\"]], 0, [\"get\", \"scalerank\"]]}`"
"doc": "An object defining custom properties on the generated clusters if clustering is enabled, aggregating values from clustered points. Has the form `{\"property_name\": [operator, map_expression]}`. `operator` is any expression function that accepts at least 2 operands (e.g. `\"+\"` or `\"max\"`) — it accumulates the property value from clusters/points the cluster contains; `map_expression` produces the value of a single point.\n\nExample: `{\"sum\": [\"+\", [\"get\", \"scalerank\"]]}`.\n\nFor more advanced use cases, in place of `operator`, you can use a custom reduce expression that references a special `[\"accumulated\"]` value, e.g.:\n`{\"sum\": [[\"+\", [\"accumulated\"], [\"get\", \"sum\"]], [\"get\", \"scalerank\"]]}`"
},
"lineMetrics": {
"type": "boolean",
Expand Down
7 changes: 1 addition & 6 deletions src/style-spec/validate/validate_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,9 @@ export default function validateSource(options) {
});
if (value.cluster) {
for (const prop in value.clusterProperties) {
const [operator, initialExpr, mapExpr] = value.clusterProperties[prop];
const [operator, mapExpr] = value.clusterProperties[prop];
const reduceExpr = typeof operator === 'string' ? [operator, ['accumulated'], ['get', prop]] : operator;

errors.push(...validateExpression({
key: `${key}.${prop}.initial`,
value: initialExpr,
expressionContext: 'cluster-initial'
}));
errors.push(...validateExpression({
key: `${key}.${prop}.map`,
value: mapExpr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"cluster": true,
"clusterRadius": 50,
"clusterProperties": {
"max": ["max", 0, ["get", "scalerank"]],
"sum": ["+", 0, ["get", "scalerank"]],
"has_island": ["any", false, ["==", ["get", "featureclass"], "island"]]
"max": ["max", ["get", "scalerank"]],
"sum": ["+", ["get", "scalerank"]],
"has_island": ["any", ["==", ["get", "featureclass"], "island"]]
}
}
},
Expand Down
5 changes: 2 additions & 3 deletions test/unit/style-spec/fixture/sources.input.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@
"data": "/test/integration/data/places.geojson",
"cluster": true,
"clusterProperties": {
"initial": ["+", ["get", "scalerank"], 0],
"zoom": ["+", 0, ["zoom"]],
"state": ["+", 0, ["feature-state", "foo"]]
"zoom": ["+", ["zoom"]],
"state": ["+", ["feature-state", "foo"]]
}
}
},
Expand Down
8 changes: 2 additions & 6 deletions test/unit/style-spec/fixture/sources.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,12 @@
"message": "sources.canvas: Please use runtime APIs to add canvas sources, rather than including them in stylesheets.",
"identifier": "source.canvas"
},
{
"message": "sources.cluster-properties.initial.initial: Feature data expressions are not supported with initial expression part of cluster properties.",
"line": 49
},
{
"message": "sources.cluster-properties.zoom.map: \"zoom\" and \"feature-state\" expressions are not supported with cluster properties.",
"line": 50
"line": 49
},
{
"message": "sources.cluster-properties.state.map: \"zoom\" and \"feature-state\" expressions are not supported with cluster properties.",
"line": 51
"line": 50
}
]
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11535,10 +11535,10 @@ sugarss@^2.0.0:
dependencies:
postcss "^7.0.2"

supercluster@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/supercluster/-/supercluster-5.0.0.tgz#2a5a9b1ffbd0d6180dea10039d78b5d95fdb8f27"
integrity sha512-9eeD5Q3908+tqdz+wYHHzi5mLKgnqtpO5mrjUfqr67UmGuOwBtVoQ9pJJrfcVHwMwC0wEBvfNRF9PgFOZgsOpw==
supercluster@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/supercluster/-/supercluster-6.0.0.tgz#7058b45545f8bacafb917a66ff2d4d36f74fd2ba"
integrity sha512-Jw0KG1zheUvNWYyl1NPb3yUURq6j6Q8rm+maY18+DDrJKmhkz0oxXoWQLf2usXVFJ0urQ8h0gh24yYV8y74WEg==
dependencies:
kdbush "^3.0.0"

Expand Down