I am trying to create a script on a wildfire boundary layer (polygon) that will return the name of every county it touches, so users can select a fire parameter and know what counties are affected. I had this working a while back and I provided what I had below, but now it returns nothing but a blank row. Any ideas? I'm not set on the expression below if there is a better solution.
var counties = FeatureSetByName($map,"USA Counties (Generalized)");
var intersectFires = Intersects(counties, $feature);
var uniqueFires = [];
for (var feat in intersectFires) {
if (IndexOf(uniqueFires, feat) == -1){
uniqueFires[Count(uniqueFires)] = [" " + feat.NAME];
}
}
return uniqueFires
Solved! Go to Solution.
I would take a look at the function Push, which handles what you're doing in line 6 a bit more clearly. You could also use Distinct to remove duplicates from an array.
Also, uniqueFires here is an array. If you just want the names, Concatenate could be useful here, to.
Finally, consider using FeatureSetByPortalItem to make the connection to the county layer more explicit.
All together, it might look something like this:
// get the counties layer
var portal = Portal('https://arcgis.com')
var counties = FeatureSetByPortalItem(
portal,
'7566e0221e5646f99ea249a197116605',
0,
['NAME']
)
// intersect feature with counties
var xs = Intersects(
$feature,
counties
)
// create array of intersected county names
var c_array = []
for (var c in xs){
Push(c_array, c['NAME'])
}
// remove duplicates
var unique_counties = Distinct(c_array)
// concatenate array, return string
return Concatenate(unique_counties, ', ')
And here's what it looks like in action:
Thanks for your response @jcarlson!
I put in the code and set up the pop-up but I'm still not seeing any of the county names. Any ideas?
There's not a function, but the pre-Push method uses an index variable, and then incrementing it. By accessing the next item in an array by number, you can assign values to it, which will work the same way.
var idx = 0
for (var c in xs) {
c_array[idx] = c['NAME']
idx ++
}
I would take a look at the function Push, which handles what you're doing in line 6 a bit more clearly. You could also use Distinct to remove duplicates from an array.
Also, uniqueFires here is an array. If you just want the names, Concatenate could be useful here, to.
Finally, consider using FeatureSetByPortalItem to make the connection to the county layer more explicit.
All together, it might look something like this:
// get the counties layer
var portal = Portal('https://arcgis.com')
var counties = FeatureSetByPortalItem(
portal,
'7566e0221e5646f99ea249a197116605',
0,
['NAME']
)
// intersect feature with counties
var xs = Intersects(
$feature,
counties
)
// create array of intersected county names
var c_array = []
for (var c in xs){
Push(c_array, c['NAME'])
}
// remove duplicates
var unique_counties = Distinct(c_array)
// concatenate array, return string
return Concatenate(unique_counties, ', ')
And here's what it looks like in action:
Thanks for your response @jcarlson!
I put in the code and set up the pop-up but I'm still not seeing any of the county names. Any ideas?
Double check how the popup itself is configured. Are you referencing {expression/expr2} (or whatever the expression number is) in the popup text? I can't thing why else it wouldn't show up.
Okay I got it working. I had to do the old fashioned clear the browser cache/ try in another browser and it looks like that did the trick. Thanks for your help!
Very nice example! @jcarlson
I'm trying to do something very similar with two Intersecting layers. I got it working on arcGIS pro, but then I realized I don't have the function 'Push' in our portal...
Is there a similar function I can use instead of 'Push' ?
There's not a function, but the pre-Push method uses an index variable, and then incrementing it. By accessing the next item in an array by number, you can assign values to it, which will work the same way.
var idx = 0
for (var c in xs) {
c_array[idx] = c['NAME']
idx ++
}
Thanks for your quick response
It worked like a charm!