Arcade: create an array of field values

11066
15
06-04-2020 12:59 PM
JoeBorgione
MVP Emeritus

As the subject line suggests, I would like to create an array of all the values of a given field in a table.  The end goal is to create an attribute rule that can check a string against the array and make sure the string is in fact part of the array.

In arcpy, I would use the da.SearchCursor and append the values of the field to a list.  How does one accomplish the same basic operation in Arcade?

I never know what Geonet space to post Arcade questions to, so I'm tossing this into the ArcGIS Pro space...

That should just about do it....
0 Kudos
15 Replies
DanPatterson
MVP Esteemed Contributor

Distinct.... in Data Functions 

probably with a bunch of other fluff around it


... sort of retired...
0 Kudos
JoeBorgione
MVP Emeritus

Looked at that; in theory, each of the specific values in the table are already distinct; no repeats.  I'm having a hard time just understanding the logic in Arcade since I've never even dabbled in java script.  For example, I don't even understand how arcade knows what the value is of $layer....

Distinct($layer, 'Status')

If you ever want to get at all my personal info, (social security # banks etc) my universal password is cantwaittoretire  ....

That should just about do it....
DanPatterson
MVP Esteemed Contributor

Don't rush it... you will be split in 2


... sort of retired...
JoeBorgione
MVP Emeritus

hehehe... closer to 1.5, but who's counting?!

That should just about do it....
0 Kudos
DanPatterson
MVP Esteemed Contributor

Better call in Xander Bakker‌ He might be able fill in my suggestion.

in the meantime 

np.unique(arcpy.FeatureClassToNumPyArray(fc, fld))


... sort of retired...
0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Joe Borgione  and Dan Patterson ,

I see you are having fun with Arcade and Python. Can't wait to join the party 

Before getting in to what you can probably do with Arcade in the attribute rule, I have a question... If you want to assure that a value is within a list of possibilities, wouldn't that be solved by using a domain? I am sure that it can't be that simple, so I will continue with the Arcade fun part.

What Joe was mentioning earlier of using the Distinct function in Arcade is correct. The example however, as you noticed, is using $layer, and $layer is not available in the attribute rule profiles. So, I am mentioning profiles and that is where a lot of the fun questions start with: depending the profile (where you want to execute the Arcade expression) you have a set of functions that you can use and a set of globals (the $ thingies) at your disposal.

Attribute rules are executed at the database level. This is important since it defines what $ thingies are available. At a database level when an attribute rules is triggered, it has no knowledge of the map a layer might be participating in. So we don't have the $map and we algo wont have the $layer (since a layer only exists in a map). What we do have is the current $feature (and the $originalFeature which provides a lot of fun too) and the $datastore. 

What I normally do is I go to the Arcade playground (a place where you can play with Arcade and have fun or a huge headache): ArcGIS Arcade | ArcGIS for Developers . There you can select the profile of where you want to use Arcade:

Not sure if you want to create a Calculation AR or a Constraint AR (raise error during edit) or Validation AR (for example for batch validation). The globals and functions will not be very different but the return value will be. When you select the profile you will see the globals available:

The idea is to have access to the "layer" using the $datastore. The datastore is a FeatureSetCollection and you will have to indicate which FeatureSet you want to execute the rule on. For instance:

FeatureSetByName($datastore, "Name of your FeatureSet in the datastore")

In combination with the Distinct function is will be this:

var fs = Distinct(FeatureSetByName($datastore, "Name of your FeatureSet in the datastore") , "FieldName");

Now you will have a featureset which you can use to check if a value is in the list of values. You will still have to do a loop to see if the value is any record of the FeatureSet since you cannot use things like IndexOf on this since "fs" is not an array but a FeatureSet. So, probably more eficiently would be doing something like this:

var searchValue = 1023 // $feature.FieldName;
var sql = "NGHBRHDCD = @searchValue";
return (Count(Filter(FeatureSetByName($datastore,"Tax_Parcels"), sql)) > 0);

You read out the value you want to test and you set up a sql, which you use to filter the FeatureSet from the datastore and do a count on the result. If that is more than 0 the value exists (returns true) and if not it will return false.

DanPatterson
MVP Esteemed Contributor

Xander Bakker I am surmising that the language remains proprietary?  or is there a GitHub page I haven't found yet?  "Distinct" looks like "unique", unfortunate that they have limited the choice of language in places of the Pro ecosystem


... sort of retired...
0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Dan Patterson , 

So the language is kind of proprietary, although it leans heavily on a subset of JavaScript. It really lives in the ArcGIS ecosystem rather than just in the Pro ecosystem. I can imagine that not being able to use Python at the same places you can use Arcade limits the possibilities, but I understand that one of the reasons for introducing Arcade was being able to have something "light" yet powerful expression language that could be used across the platform no matter if you use it in desktop, web or inside mobile apps. There is still a long way to go to get there achieving the same support no matter what device or app you use, but that is where it is heading and from what I understand and Python would not have been the best way to achieve that. But this is my interpretation and not an official statement. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

Thanks Xander Bakker‌ I was wondering why, since a lot of people I know use python (and imports) for web stuff.  Perhaps what they do is different than what esri does and the curly bracket languages seem to dominate web-stuff.


... sort of retired...