Select to view content in your preferred language

# Arcade expression for Min, Max, Avg and Count

1698
5
05-19-2022 05:03 AM
Labels (1)
New Contributor II

I am working on the typology of urban blocks. Each block contains a number of buildings, each of which has an area and a height.
I would like to calculate the maximum, minimum, average and median area and height in each block on arcade. Can you help me please?

1 Solution

Accepted Solutions
MVP Frequent Contributor

If you want to do this in the CalculateField tool, you can use this expression, modified for each statistic:

``````// load the whole feature class
var fs_buildings = FeatureSetByName(\$datastore, "BuildingFC", ["BlockID", "BuildingID", "BuildingHeight", "BuildingArea"], false)

// only select rows with the current feature's BlockID
var id = \$feature.BlockID
var fs_buildings_block = Filter(fs_buildings, "BlockID = @ID")

// edit this line for each statistic and field
return Min(fs_buildings_block, "BuildingHeight")``````

Have a great day!
Johannes
5 Replies
MVP Frequent Contributor
• You need a table with a block id, building height, and building area
• You need one of the FeatureSetBy*() functions to load your building data
• You need the GroupBy() function to group your building data by block id.

For the functions, look here:

As an example, you can use the following code in the Playground | ArcGIS Arcade | ArcGIS Developer

``````var fs_buildings = {
"geometryType": "",
"fields": [
{"name": "BlockID", "type": "esriFieldTypeInteger"},
{"name": "BuildingID", "type": "esriFieldTypeInteger"},
{"name": "BuildingHeight", "type": "esriFieldTypeDouble"},
{"name": "BuildingArea", "type": "esriFieldTypeDouble"}
],
"features": [
{"attributes": {"BlockID": 1, "BuildingID": 1, "BuildingHeight": 10., "BuildingArea": 300.}},
{"attributes": {"BlockID": 1, "BuildingID": 2, "BuildingHeight": 30., "BuildingArea": 500.}},
{"attributes": {"BlockID": 1, "BuildingID": 3, "BuildingHeight": 15., "BuildingArea": 250.}},
{"attributes": {"BlockID": 2, "BuildingID": 4, "BuildingHeight": 16., "BuildingArea": 400.}},
{"attributes": {"BlockID": 2, "BuildingID": 5, "BuildingHeight": 10., "BuildingArea": 300.}},
{"attributes": {"BlockID": 3, "BuildingID": 6, "BuildingHeight": 10., "BuildingArea": 300.}}
]
}
fs_buildings = FeatureSet(Text(fs_buildings))
//return fs_buildings

// actually use one of the FeatureSetBy*() functions:
//var p = Portal(...)
//var fs_buildings = FeatureSetByPortalItem(p, item, layer, ["BlockID", "BuildingID", "BuildingHeight", "BuildingArea"], false)

var statistics = [
{"name": "Count", "expression": "BuildingID", "statistic": "COUNT"},
{"name": "HeightMax", "expression": "BuildingHeight", "statistic": "MAX"},
{"name": "HeightMin", "expression": "BuildingHeight", "statistic": "MIN"},
{"name": "HeightMean", "expression": "BuildingHeight", "statistic": "AVG"},
{"name": "AreaMax", "expression": "BuildingArea", "statistic": "MAX"},
{"name": "AreaMin", "expression": "BuildingArea", "statistic": "MIN"},
{"name": "AreaMean", "expression": "BuildingArea", "statistic": "AVG"},
]
var fs_grouped_buildings = GroupBy(fs_buildings, "BlockID", statistics)
return fs_grouped_buildings``````

Have a great day!
Johannes
New Contributor II

Thank you very much for your quick response

in fact I already have a table with the BlockID, the ID of the buildings, the area and the height of each building. and I would like to calculate from this table.

New Contributor II

could you, please, tell me how to calculate each statistic by adding a new field on my buildings table and calculating the field?

MVP Frequent Contributor

If you want to do this in the CalculateField tool, you can use this expression, modified for each statistic:

``````// load the whole feature class
var fs_buildings = FeatureSetByName(\$datastore, "BuildingFC", ["BlockID", "BuildingID", "BuildingHeight", "BuildingArea"], false)

// only select rows with the current feature's BlockID
var id = \$feature.BlockID
var fs_buildings_block = Filter(fs_buildings, "BlockID = @ID")

// edit this line for each statistic and field
return Min(fs_buildings_block, "BuildingHeight")``````

Have a great day!
Johannes
New Contributor II

IT work...Thank you very much !!!!