This expression returns an attribute value from a featureSet where the largest feature intersects the input feature.
To suggest a value from a majority intersection. For instance, when drawing a polygon, return the county name for which the majority of the new polygon is in.
Copy and paste the expression found in the expression template below to the Arcade editor in ArcGIS Online, the relevant location in ArcGIS Pro, or the relevant location in a custom app.
To configure the script to your layer, edit the first line to specify the layer you would like to use instead of the example layer.
var set = FeatureSetByName($datastore,"Building Footprints")
Also edit the last line to specify the attribute you would like to use intead of the example objectid
.
getAttributeFromLargestArea($feature, set, 'objectid')
This Arcade expression will extract the attribute value from a feature in a featureSet where the input feature intersection area is the largest.
// Refer to the name of the layer used to search areas here.
var layerName = 'Building Footprints';
// your field name should go here
var fieldName = "objectid";
var set = FeatureSetByName($datastore, layerName)
function getAttributeFromLargestArea(feat, set, field) {
var items = intersects(set, feat);
var counts = count(items);
if (counts == 0) {
return { 'errorMessage': 'No intersection found' };
}
if (counts == 1) {
var result = first(items);
return result[field];
}
var largest = -1;
var result;
for (var item in items) {
var size = area(intersection(item, feat));
if (size > largest) {
largest = size;
result = item[field];
}
}
return result;
}
getAttributeFromLargestArea($feature, set, fieldName)