Skip to content

Latest commit

 

History

History
67 lines (45 loc) · 1.88 KB

attributeFromLargestArea.md

File metadata and controls

67 lines (45 loc) · 1.88 KB

Attribute from Intersection

This expression returns an attribute value from a featureSet where the largest feature intersects the input feature.

Use cases

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.

Workflow

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')

Expression Template

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)