Select to view content in your preferred language

Using Code for Field Calculation

345
1
06-13-2024 12:11 PM
Labels (1)
emoreno
Regular Contributor

Hi ESRI Community,

I am working on a project where I need to populate a new field, named Drop_Length_Total, in my layer named "DoubleD". Using calculate field, I've tried to write code to sum the shape_lengths of two lines, but am running into issues. The workflow i'm trying to achieve is below:

If the pink line touches the orange dot, i'd like the shape_length of the pink line to be added to the shape_length of the blue line that also instersects the orange dot. in the end I'd like for these sums to be written to the Drop_Length_Total field in my DoubleD layer. I've included a visual of what would need to be added together below, with the yellow being one summation and the green being another. If anyone has any ideas as to how I can better achieve this workflow, or any code I could test to calculate the field, it would be greatly appreciated. Thank you for any help!

pink lines = DTTERM layer

Orange dots = VSP layer

blue lines = DTTAP layer

blue dots = taps layer

emoreno_0-1718305053694.pngemoreno_1-1718305188539.png

 

0 Kudos
1 Reply
Robert_LeClair
Esri Notable Contributor

Caveat, I used ChatGPT to write this Arcade expression based upon the statement - "Write an arcade expression that calculates a field called Drop_Length_Total in a layer called DoubleD where if a line layer called DTTERM touches a point layer called VSP, I'd like the shape_length of the DTTERM layer to be added to the shape_length of a layer called DTTAP that also intersects the same VSP point."

The script completed is as follows - haven't tested it as I don't have the same data as you:

// Initialize the total drop length variable
var dropLengthTotal = 0;

// Access the DTTERM layer
var dttermLayer = FeatureSetByName($map, "DTTERM");

// Access the DTTAP layer
var dttapLayer = FeatureSetByName($map, "DTTAP");

// Access the VSP points layer
var vspLayer = FeatureSetByName($map, "VSP");

// Loop through each VSP point
for (var vspPoint in vspLayer) {
// Find DTTERM lines that touch the current VSP point
var touchingDTTERM = Intersects(dttermLayer, vspPoint);

// Find DTTAP lines that intersect the same VSP point
var intersectingDTTAP = Intersects(dttapLayer, vspPoint);

// Initialize variables to hold lengths
var dttermLength = 0;
var dttapLength = 0;

// Sum the lengths of touching DTTERM lines
for (var line in touchingDTTERM) {
dttermLength += line.shape_length;
}

// Sum the lengths of intersecting DTTAP lines
for (var line in intersectingDTTAP) {
dttapLength += line.shape_length;
}

// Sum the lengths for the current VSP point
var totalLengthForVSP = dttermLength + dttapLength;

// Add to the total drop length
dropLengthTotal += totalLengthForVSP;
}

// Return the total drop length
return dropLengthTotal;

0 Kudos