Select to view content in your preferred language

Arcade - generating unique IDs for new and existing polygons

743
7
11-13-2024 07:29 AM
Laura
by MVP Regular Contributor
MVP Regular Contributor

I need help with creating a system for generating unique IDs for a layer. Most of the features already have unique IDs in a large numeric range, and all of them begin with the letter "P" (e.g., P10057). This setup was done prior to me. I need to keep it the same as the layer is tied to a related table(s) with that ID.

What I'm looking for is a solution that will automatically generate a unique ID for each new feature created, ensuring that the ID is not already in use. Additionally, I have about 12 existing polygons that were created without IDs, and I need help assigning unique IDs to those as well.

 

Laura_0-1731511653423.png

 

Any help is appreciated, thanks!

0 Kudos
7 Replies
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @Laura 

So the only way to do this, because you are using alpha numeric values instead of numerical values, is you would need to loop through the entire feature set, removing the p from each value to get the number, and then populate a list of numbers and only returning the maximum number. From there you would simply add 1 to the max and return that number as the new id.

 

var fs = $featureset

var Numbers = []
for( var row in fs ){
    var id = row['<insert id field name>']
    id = Left( id , Count( id ) - 1 )
    Push( Numbers , id )
    }
var N = Max( Numbers ) + 1
return 'P'+Text( N )

It is typically recommended to use numerical values for ids unless there isn't another option or because it is used because of some other application/dataset.

 

Laura
by MVP Regular Contributor
MVP Regular Contributor

Am I not filling something in here?

Laura_0-1731520244176.png

 

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @Laura 

You simply added the feature value. Delete '$feature.' and it should work.

I also forgot to include this in the script.

    id = Number( Left( id , Count( id ) - 1 ) )

 Did you select arcade as the calculation method?

0 Kudos
Laura
by MVP Regular Contributor
MVP Regular Contributor

Yes, arcade.

Laura_0-1731526596385.png

 

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @Laura,

Try changing the $featureset to just $feature and see how that works.

What you could do is take the highest value in the list and then add the object id to create the unique number and then convert that to text and add 'P' at the beginning.

Unfortunately, alpha-numeric ids are really difficult to work with because of the fluctuations in values.

The code snippet above does work, but it would mostly work with attribute rules.

If you truly need something akin to that, then my only other suggestion would be to create an attribute rule, similar to how the field calculation is set up, and then try running a nonchalant calculation to trigger the rule.

 

0 Kudos
Laura
by MVP Regular Contributor
MVP Regular Contributor

Still throwing error, I called support and will see what they come up with. 

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @Laura,

I failed to realize that the code that I sent you does not work in terms of field calculations but it will work as an attribute rule. If you create an attribute rule and use the code expression as the one that was sent, and run an arbitrary calculation then it should work.

0 Kudos