Select to view content in your preferred language

Arcade Script to pull a intersect value from overlapping polygon layers based on a inputted value.

85
1
Thursday
jmcdonald_sdi
New Contributor II

Hey AGOL Blog,

I have 5 overlapping/stacked floor layers that each have a room_id attribute. I'm trying to run a script that checks a floor value inputted by the user in a separate field and returns the room_id value for that specific floor. 

// Check the value of the room level field: values are "base" "lowr" "uppr" "pent" "mezz"
var roomlevel = $feature.Room_Level

// Create a feature set using the 'Basement' layer in the map
var based = FeatureSetByName($map, "BORG_Basement_Level")

// Intersect the current location with the the level and
// get the first intersecting room
var base1 = First(Intersects($feature, based))

// Create a feature set using the 'Lower' layer in the map
var lowerd = FeatureSetByName($map, "BORG_LowerLevel")

// Intersect the current location with the the level and
// get the first intersecting room
var lower1 = First(Intersects($feature, lowerd))

// Create a feature set using the 'Upper' layer in the map
var upperd = FeatureSetByName($map, "BORG_UpperLevel")

// Intersect the current location with the the level and
// get the first intersecting room
var upper1 = First(Intersects($feature, upperd))

// Create a feature set using the 'Penthouse' layer in the map
var pentd = FeatureSetByName($map, "BORG_Penthouse")

// Intersect the current location with the the level and
// get the first intersecting room
var pent1 = First(Intersects($feature, pentd))

// Create a feature set using the 'Mezzanine' layer in the map
var mezzd = FeatureSetByName($map, "BORG_Mezzanine")

// Intersect the current location with the the level and
// get the first intersecting room
var mezz1 = First(Intersects($feature, mezzd))

// If the current location does intersect a feature,
// return the name of the region. Otherwise, return null

return When(
  roomlevel == "base", base1['room_id'],
  roomlevel == "lowr", lower1['room_id'],
  roomlevel == "uppr", upper1['room_id'],
  roomlevel == "pent", pent1['room_id'],
  roomlevel == "mezz", mezz1['room_id']
)

The final part of the script is where I'm having the most trouble, I don't know how to format the script to output the desired value reliably. Right now can only make it return the basement value.

Thanks!
0 Kudos
1 Reply
KenBuja
MVP Esteemed Contributor

Since you know which floor the feature is on, why run all the intersections? Just run it once according to the floor.

// Check the value of the room level field: values are "base" "lowr" "uppr" "pent" "mezz"
var roomlevel = $feature.Room_Level

var floor = Decode(roomlevel,
  "base", "BORG_Basement_Level",
  "lowr", "BORG_LowerLevel",
  "uppr", "BORG_UpperLevel",
  "pent", "BORG_Penthouse",
  "mezz", "BORG_Mezzanine")

var fs = FeatureSetByName($map, floor)

// Intersect the current location with the the level and
// get the first intersecting room
var location = First(Intersects($feature, fs));

iif(IsEmpty(location), null, location['room_id'])