Help writing Arcade to get distinct values from intersection in Field Maps

386
3
Jump to solution
05-01-2024 10:32 PM
Labels (1)
LindsayRaabe_FPCWA
Regular Contributor II

I'm anticipating a problem I'm going to need to solve next week and don't believe my knowledge is up to it. Sooooo, I'm hoping the brains trust can help me out! 

I will have 2 polygon layers in Field Maps. One contains features (plantation areas) and another will have a new feature added to it which intersects with multiple plantation areas. What I want to achieve is an arcade script that runs an intersect between the 2 layers (which I know how to do) but then takes the planting year of all the intersect plantation areas (e.g. 1992, 1992, 1992, 1995, 1996, 1996, ...) and return a concatenated string of the Distinct values (e.g. 1992, 1995, 1996, ...). This string will then get passed to Survey123 to auto populate a field in the survey. 

Is this possible?

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
1 Solution

Accepted Solutions
JohnEvans6
Occasional Contributor

Should be able to. I'm not super familiar with field maps. If I've got a good read on what you're asking for I think it looks something like

var poly1 = FeatureSetByName($map, "Plantation Areas")
var intersects = Intersects(Geometry($feature), poly1)
var distinct_planting = []
var display_output = ""

for(var f in intersects) {
  Push(distinct_planting, f['plantation_year_field'])
}

distinct_planting = Distinct(distinct_planting)

for (var pyear in distinct_planting) {
  display_output = display_output + distinct_planting[pyear] + ' '
}

display_output = Trim(display_output)

return { 
	type : 'text', 
	text : display_output
}

 

View solution in original post

3 Replies
JohnEvans6
Occasional Contributor

Should be able to. I'm not super familiar with field maps. If I've got a good read on what you're asking for I think it looks something like

var poly1 = FeatureSetByName($map, "Plantation Areas")
var intersects = Intersects(Geometry($feature), poly1)
var distinct_planting = []
var display_output = ""

for(var f in intersects) {
  Push(distinct_planting, f['plantation_year_field'])
}

distinct_planting = Distinct(distinct_planting)

for (var pyear in distinct_planting) {
  display_output = display_output + distinct_planting[pyear] + ' '
}

display_output = Trim(display_output)

return { 
	type : 'text', 
	text : display_output
}

 

LindsayRaabe_FPCWA
Regular Contributor II

Awesome starting point. Changed my layer and field names and tested it out with the below results. 

LindsayRaabe_FPCWA_0-1715128362678.png

 

Just need to work on it a bit to filter out the "0" values (where no Year value exists) and sort from oldest to most recent (as well as the formatting to just return a value instead of the table and "text" values). 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
LindsayRaabe_FPCWA
Regular Contributor II

Modified to sort values (Line 10), exclude 0 values  (Line 7) and return just the output value (Line 18). 

LindsayRaabe_FPCWA_1-1715128780181.png

 

var poly1 = FeatureSetByName($map, "FPC Plantations")
var intersects = Intersects(Geometry($feature), poly1)
var distinct_planting = []
var display_output = ""

for(var f in intersects) {
  iif(f['EstabYear']==0,"",Push(distinct_planting, f['EstabYear']))
}

distinct_planting = Distinct(Sort(distinct_planting))

for (var pyear in distinct_planting) {
  display_output = display_output + distinct_planting[pyear] + ' '
}

display_output = Trim(display_output)

return(display_output)

 

And further modified to separate with commas and spaces ', ' (Line 13) plus removing the last comma and space (Line 16) which negates the need for the Trim function which only removed the original trailing space. 

LindsayRaabe_FPCWA_0-1715129850595.png

var poly1 = FeatureSetByName($map, "FPC Plantations")
var intersects = Intersects(Geometry($feature), poly1)
var distinct_planting = []
var display_output = ""

for(var f in intersects) {
  iif(f['EstabYear']==0,"",Push(distinct_planting, f['EstabYear']))
}

distinct_planting = Distinct(Sort(distinct_planting))

for (var pyear in distinct_planting) {
  display_output = display_output + distinct_planting[pyear] + ', '
}

return(Left(display_output, Count(display_output)-2))

 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos