Key parameters
| Parameter | Example | What it does |
|---|---|---|
runOnClick | onAreaSummed | Name of a function in functions, called when the polygon is closed, with (layersValues, inputs, feature); this is the layer. |
text | Draw an area to sum | Label of the switch in the layer panel. |
id | area_sum | Names the tool, e.g. to call Ext.getCmp("area_sum").items.get(0).getSummedLayerValues(layer, feature) from code. |
notify | false | Hide the notification shown when the user switches the tool on. |
Every parameter, with its type and default, is in the reference at the end of this page.
Complete example
The query 25 lines · runs as is
// SummedArea: draw a polygon (double-click to close); runOnClick receives one sum per map in
// `name`. The same raw map is read twice: as average density and as covered area.
[
{
title: "Population density - area sum",
name: "CSR:pop_density_estimate_2015,CSR:pop_density_estimate_2015",
styles: "1,1",
source: "calculate",
operation: "average,area",
opacity: 0.7,
visibility: true,
paramsButtonConfig: [{ type: "query", pressed: true }],
descriptionHtml:
"{{summedarea|id=area_sum|text=Draw an area to sum|runOnClick=onAreaSummed}}",
functions: {
onAreaSummed: function (layersValues) {
ExtjsUtils.ALERTIFY.log("Area with data: <b>" +
ExtjsUtils.NUMBER.abbreviateNumber(Math.round(layersValues[1] / 100)) + " km²</b>");
},
},
expression: function (layersVals) {
return layersVals[0];
},
},
];Customize it
What the callback receives
layersValues has one sum per map in name, in the same order, followed by two extra entries: the number of cells read, and an array with the count of cells with data for each map. Cells without data are skipped. The layer’s expression result is not summed. feature is the drawn polygon, and this is the layer.
The example reads the same map twice with operation: "average,area": layersValues[0] adds up the average density of each cell, layersValues[1] the area each cell covers, which is the number the notification shows.
Choose the operation
The tool adds up the cells drawn on screen, so with an ordinary map the total changes with the zoom. Maps published as raw maps solve this: read with area, each cell carries the original area it covers; read with sum, the total of the original cells it covers. Their sums stay the same at any zoom. The layer must use an operation the map is published with, plus the map’s name and styles, exactly as in the example; the tokens are listed in the property catalogue.
Use the sums in the calculation
The tool never fills inputs.id[ID]: the sums reach only your callback. To feed them to the calculation, store them in an InputManager, whose setValues also recalculates the layer:
descriptionHtml: "{{summedarea|id=area_sum|text=Draw an area to sum|runOnClick=onAreaSummed}}" +
"{{inputmanager|id=sums}}",
functions: {
onAreaSummed: function (layersValues, inputs) {
inputs.id["sums"].setValues({ area: layersValues[1] }); // recalculates the layer
},
},
Pitfalls
- The sum reads the map tiles the browser has loaded, so draw the polygon inside the visible part of the map.
- The tool switches itself off after every polygon;
unselectonly changes the message shown when it is switched on. - For a rectangle, AreaIntegral is faster: it needs four reads whatever the size, on a map published with the
integraloperation.