Skip to main content
Solved

create a personalised ID with letter and number

  • October 18, 2017
  • 7 replies
  • 28 views

alc33
Contributor
Forum|alt.badge.img+11
  • Contributor
  • 54 replies

Hello,

I would like to create a personalised ID (counter personalised) with letters and number.

I use 2 letters (A to Z) and 1 number (1 to 9), so I would like something like that :

AA1

AA2

AA3

...

AB1

AB2

AB3

...

I don't know how to do that and I don't find the solution on the web. Maybe a python script? But I haven't no knowledge on this langage.

If you have any idea...

Thank you for your future answer.

Best answer by ebygomm

And an fme solution, ignoring zeros

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

7 replies

david_r
Celebrity
  • 8394 replies
  • October 18, 2017

Here's a quick and dirty solution using a Python script:

import fmeobjects
 
class AlphanumericCounter(object):
    def __init__(self):
        self.counter = 0
       
    def counter_as_str(self):
        a = self.counter // (26*9)
        b = (self.counter - (a*26*9)) // 9
        c = self.counter - (a*26*9) - (b*9)
        return chr(65+a) + chr(65+b) + str(c+1)
        
    def input(self,feature):
        feature.setAttribute('id', self.counter_as_str())
        self.counter += 1
        if self.counter > (26*26*9):
            raise fmeobjects.FMEException('Too many features!')
        self.pyoutput(feature)

Each output feature will have a new attribute "id" that contains the counter.

If the number of features exceeds 6084 (id > ZZ9), it will terminate the translation with an error message. PythonCaller parameters:

0684Q00000ArLrUQAV.png


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3434 replies
  • Best Answer
  • October 18, 2017

And an fme solution, ignoring zeros


takashi
Celebrity
  • 7843 replies
  • October 19, 2017

The NumToAlphaConverter from FME Hub could also be used here.


takashi
Celebrity
  • 7843 replies
  • October 19, 2017

Here's a quick and dirty solution using a Python script:

import fmeobjects
 
class AlphanumericCounter(object):
    def __init__(self):
        self.counter = 0
       
    def counter_as_str(self):
        a = self.counter // (26*9)
        b = (self.counter - (a*26*9)) // 9
        c = self.counter - (a*26*9) - (b*9)
        return chr(65+a) + chr(65+b) + str(c+1)
        
    def input(self,feature):
        feature.setAttribute('id', self.counter_as_str())
        self.counter += 1
        if self.counter > (26*26*9):
            raise fmeobjects.FMEException('Too many features!')
        self.pyoutput(feature)

Each output feature will have a new attribute "id" that contains the counter.

If the number of features exceeds 6084 (id > ZZ9), it will terminate the translation with an error message. PythonCaller parameters:

0684Q00000ArLrUQAV.png

Close! The range of number should be 1 to 9 ;-)

 

 


david_r
Celebrity
  • 8394 replies
  • October 19, 2017
Close! The range of number should be 1 to 9 ;-)

 

 

Ah, yes... But everyone knows that an array starts at 0, not 1 ;-)

david_r
Celebrity
  • 8394 replies
  • October 19, 2017
Close! The range of number should be 1 to 9 ;-)

 

 

But thanks for the tip, I've fixed the code above

alc33
Contributor
Forum|alt.badge.img+11
  • Author
  • Contributor
  • 54 replies
  • October 19, 2017

Hello,

Thank for all yours replies and for your reactivity!

I choose to evacuate the 0 for a specific reason, that's why I ask only 1 to 9 ;) thanks david_r for the adaptation of your code :)

Have a good day everyone!