Change style with Arcade Find function

310
7
09-19-2019 04:00 PM
EvanThoms
Occasional Contributor II

First Arcade expression here and I can't figure out what I am doing wrong. Using an AGOL-hosted feature layer of geologic unit polygons, I am trying to change the style so that there are two classes; polygons of one particular unit and everything else. Eg.,

var test = $feature["UNIT"];

if (Find("Kaf", test)>-1) {
 return "Kaf";
} else {
 return "other";
}

and this is what I get:

The gray 'Other' polygons are actually 'Kaf' polygons. 

Tags (2)
0 Kudos
7 Replies
XanderBakker
Esri Esteemed Contributor

Hi Evan Thoms , 

Your expression seems to be OK, but I can only be sure when I see a couple of examples from the UNIT field. Just so you know... When the UNIT contains "KAF" (upper case) it won't find "Kaf".

0 Kudos
EvanThoms
Occasional Contributor II

Xander, thanks for taking a look. 

Right, "KAF" appears no where in that field. I know I am looking for text that should be found in the field. 

Here's a bit more. With another layer, again of geology polygons, I set up the expression below to find lithology terms in any of 8 fields in which they could possibly occur:

var lithFlds = [$feature["MAJOR1"], $feature["MAJOR2"],- 
 $feature["MAJOR3"], $feature["MINOR1"], $feature["MINOR2"], -
 $feature["MINOR3"], $feature["MINOR4"], $feature["MINOR5"]]

var lith = 'Sandstone'

for(var k in lithFlds){
 if (Find(lith, lithFlds)>-1){
 return lith;
 } else {
 return "not found";
 }
}

When I put this in to the online AGOL expression builder and run test, it works only when one of the Globals included has the lith value displayed when I click on the pencil icon to the right of it

I don't know for sure what those values indicate but my guess is they represent the values that would be returned from the feature that is in the center of the current extent over at the map or at least some kind of sampling of the feature layer? Ah, wait, I see I can edit that value. What's the purpose of this?

Despite the fact that the test runs successfully, when I save the expression and back out of the dialog and go back to the map, the class symbology is as I first described. 

When I search for a lithology term that does not appear in any of those edit boxes, the test fails.

And finally, I tried this expression out in Pro on the same feature layer and it basically timed out trying to search all of the records. I had to shut it down.

Maybe I am up against some kind of record limit? The feature layer has some 300k rows.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi ethoms ,

Perhaps when you try to visualize the polygons, you want to zoom into a zone to see the result. Having an expression that reads out 8 attributes and loop through them for 300K feature will definitely impact the visualization. It might be better to create a new attribute and fill it with the result of the expression below to avoid this. 

You used the return statements inside the loop and that might have caused the problem. Try the expression below and see if that works:

var lithFlds = [$feature["MAJOR1"], $feature["MAJOR2"], $feature["MAJOR3"], $feature["MINOR1"], $feature["MINOR2"], $feature["MINOR3"], $feature["MINOR4"], $feature["MINOR5"]];
var lith = 'Sandstone';

var result = "not found";
for (var k in lithFlds) {
    if (Find(lith, lithFlds[k])>-1) {
        result = lith;
    }
}

return result;

Please share some of the values stored in the UNIT field to see what is going wrong with the expression of the original question. 

0 Kudos
EvanThoms
Occasional Contributor II

That works a bit better. If I search on the same lithology term as the one appearing in the edit box, the classes (usually) get assigned to the renderer. But, again, when I search for a different term; that I know exists after querying a nearby polygon, the test fails and the symbology is as first described.

I tried searching only one field and the results were the same which suggests the problem has little to do with searching eight.

Zooming in or being out at a small scale seems to have no effect. The test only works when I am searching for the lithology in the edit box. 

Note that, like in my first post, when the style is not updated correctly, the polygons associated with the term for which I am searching are being symbolized differently than the 'not found' class, but they are appearing with the symbol for  'Other' and the count is 0. 

I had some success yesterday with filters and group filters, so I think will move off this Arcade track for now.

From the original question, some Unit value examples are Tmv, PzPxqs, Qs, Kkg.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Evan Thoms ,

Sorry to hear that is is not working. I still wonder what will happen if you create a new field and populate it with the expression and draw you map on that field.

From the original question, some Unit value examples are Tmv, PzPxqs, Qs, Kkg.

In this case none of the values contains "Kaf". Or do you want to validate each character in "Kaf" for existence in the units?

0 Kudos
EvanThoms
Occasional Contributor II

Those are just a few examples, the domain includes many many more values, including "Kaf". In that case, I could have used $feature.Unit == lith rather than Find. I just found Find under the Text functions and thought it was most appropriate. 

I still wonder what will happen if you create a new field and populate it with the expression and draw you map on that field.

I could try that, but of course that's the first thing listed as a situation you can avoid by using Arcade! 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi ethoms , 

I could try that, but of course that's the first thing listed as a situation you can avoid by using Arcade!

Very true, but when you use Arcade in the symbology profile on a featureclass of 300K features, performance should be considered.

0 Kudos