Hi, I am attempting to create an attribute expression through an address point layer that intersects a zoning layer. The zone layer has an attribute that is coded with different zones, i.e. "RR" for residential, "CG" for commercial. I'm using Intersect to find the polygon layer and retrieve the attribute but having an issue with re-writing the return result. I used the code below but am having an issue with the open brace { at the switch line in code below (line 10 in the image). Any help would be greatly appreciated!
// Variables for the layers
var point = $feature;
var polygons = FeatureSetByName($map, "COG Zoning");
// Initialize the output string
var result = "No intersection";
// Loop through polygons to check intersection
for (var poly in polygons) {
if (Intersects(point, poly)) {
// Customize this part to return specific text based on polygon attributes
switch (poly.Pro_Zoning) {
case "RR":
result = "Residential";
break;
case "CG":
result = "Commercial";
break;
// Add more cases as needed
default:
result = "Zoning data is not available";
}
}
}
// Return the result string
return result;
Solved! Go to Solution.
And did you try swapping the switch/case for Decode?
Also, rather than loop through the entire polygons layer and checking for intersection with your point, you're better off doing it the other way. Using Intersects between your point and the entire polygons layer will return a FeatureSet of only those polygons which intersect with the point.
var intersecting_polygons = Intersects($feature, FeatureSetByName($map, 'COG Zoning'))
// if intersection, decode value, otherwise return "no intersection"
if (Count(intersecting_polygons) > 0) {
var poly = First(intersecting_polygons)
return Decode(
poly.Pro_Zoning,
'RR', 'Residential',
'CG', 'Commercial',
//etc
'Zoning data not available'
)
} else {
return 'No intersecting zoning feature'
}
Where is the screenshot expression from? It's different from the text you posted… I wouldn't trust it, as Filter is for attribute-based selections. The function Intersects will spatially filter the other layer all by itself. Also, even though Arcade acts a lot like JS, it has no switch/case capabilities.
You'll need to change the expression, but switch/case can nicely be replaced with the function When. Or when you're just swapping one value for possible others, Decode.
// Variables for the layers
var point = $feature;
var polygons = FeatureSetByName($map, "COG Zoning");
// Initialize the output string
var result = "No intersection";
// Loop through polygons to check intersection
for (var poly in polygons) {
if (Intersects(point, poly)) {
// Customize this part to return specific text based on polygon attributes
result = Decode(
poly.Pro_Zoning,
'RR', "Residential",
'CG', "Commercial",
// Add more cases as needed
"Zoning data is not available"
)
}
}
// Return the result string
return result;
Hi Josh, it was my mistake. I had included a different image for the question. The correct image has been updated on the post and attached here
And did you try swapping the switch/case for Decode?
Also, rather than loop through the entire polygons layer and checking for intersection with your point, you're better off doing it the other way. Using Intersects between your point and the entire polygons layer will return a FeatureSet of only those polygons which intersect with the point.
var intersecting_polygons = Intersects($feature, FeatureSetByName($map, 'COG Zoning'))
// if intersection, decode value, otherwise return "no intersection"
if (Count(intersecting_polygons) > 0) {
var poly = First(intersecting_polygons)
return Decode(
poly.Pro_Zoning,
'RR', 'Residential',
'CG', 'Commercial',
//etc
'Zoning data not available'
)
} else {
return 'No intersecting zoning feature'
}
Hi Josh,
I tried the first set of code but kept receiving the "No Intersection" result even though the polygon layer intersected had an attribute. But your second set of code worked perfectly. Thanks!
Hi Josh,
A follow up question for this. What if I'm using multiple polygon layers? Meaning one polygon for the city and another polygon for the county.