Select to view content in your preferred language

Help completing Arcade Script in AGOL to extract & sum lengths from a layer and insert into another layer based on matching fields

255
2
Jump to solution
04-02-2025 01:32 PM
Labels (3)
Mowgli_GIS
Occasional Contributor

Hello, I'm trying to write a script in Field Maps that will extract and Sum the values from a field in one layer and then insert that value into a field in another layer based on matching fields.  I found some example of code that I think is what I need, or at least close to what I need but I can't figure out how to tailor it to my needs.  I have a basic understanding of Arcade and have been stuck on this for days.

I have several Project Areas with Cables running throughout the various areas.  My goal is to have a script that will Sum the total Length of cables in each project area, and then insert that total into a field in the Project Area, and it does this based on the Area ID in the Cable layer Matching the Area ID in the Project Areas layer. 

I have a script that can do this based on Cables that intersect the Project Area, but that won't work because numerous Cables intersect multiple Project Areas, but they are only assigned a single Area ID and thus I want that footage to be attributed to the total for that single Area.

I've included what I have so far below.  When I run the script in the code creation box, the output message gives me the Length total for the Entire Cable layer, and then when I go back to the map and click on a Design Area polygon the field is just blank, it doesn't give any footage.  Appreciate any help you could offer!

 

var Cables = FeatureSetByName($map, "Cable", ['Area_ID', 'Length'], true)

var Areas = FeatureSetByName($map, "Project Areas", ['Area_ID'], true)

var totalsum = 0

for (var $feature in Areas) {

  var matchvalue = $feature["Area_ID"]

var relatedfeatures = Filter(Cables, "Area_ID = @matchvalue")

for (var $feature in relatedfeatures) {

  totalsum += $feature["Length"]

  }

}

return totalSum

 

0 Kudos
1 Solution

Accepted Solutions
CodyPatterson
MVP Regular Contributor

Hey @Mowgli_GIS 

You're definitely on the right track, I looked through and made some changes for you that should get you in the right spot:

var Cables = FeatureSetByName($map, "Cable", ['Area_ID', 'Length'], true)
var Areas = FeatureSetByName($map, "Project Areas", ['Area_ID'], true)

var currentAreaID = $feature["Area_ID"]

var relatedCables = Filter(Cables, "Area_ID = @currentAreaID")

var totalSum = 0

for (var cable in relatedCables) {
    totalSum += cable["Length"]
}

return totalSum

In this, you should be placing this into the Project Area layers field calculator so it'll run for each of the featutes. The Area_ID will get the area ID as long as that matches the field names, and we filter the Cables layer down to the cables that match the Area_ID, then finally we sum the cables with the matching Area_ID instead of all.

Let me know if this works out for you!

Cody

View solution in original post

2 Replies
CodyPatterson
MVP Regular Contributor

Hey @Mowgli_GIS 

You're definitely on the right track, I looked through and made some changes for you that should get you in the right spot:

var Cables = FeatureSetByName($map, "Cable", ['Area_ID', 'Length'], true)
var Areas = FeatureSetByName($map, "Project Areas", ['Area_ID'], true)

var currentAreaID = $feature["Area_ID"]

var relatedCables = Filter(Cables, "Area_ID = @currentAreaID")

var totalSum = 0

for (var cable in relatedCables) {
    totalSum += cable["Length"]
}

return totalSum

In this, you should be placing this into the Project Area layers field calculator so it'll run for each of the featutes. The Area_ID will get the area ID as long as that matches the field names, and we filter the Cables layer down to the cables that match the Area_ID, then finally we sum the cables with the matching Area_ID instead of all.

Let me know if this works out for you!

Cody

Mowgli_GIS
Occasional Contributor

Thanks Cody!! This worked great!  Appreciate the help and feedback!