Solved

Determine in-between alphabetic values

  • 13 September 2018
  • 4 replies
  • 1 view

I've got a table with POSTCODE_FROM and a POSTCODE_TO attributes, constituting several alphanumeric ranges, for example:

(from) 7887AA (to) 7887AA

(from) 7887AB (to) 7887AC

(from) 7887AD (to) 7887AJ

(from) 7887AK (to) 7887BB

Each feature has a FROM and a TO value and I would like to determine all in-between values, e.g. 7887AE, 7887AF, 7887AG, 7887AH, 7887AI.

 

Does anyone know what (sequences of) transformers are capable of doing this?

icon

Best answer by ebygomm 13 September 2018, 21:54

View original

4 replies

Userlevel 4

Try the following in a PythonCaller, it will clone the incoming feature for each possible variant between POSTCODE_FROM and POSTCODE_TO:

from fmeobjects import *
import string
digs = string.digits + string.ascii_uppercase

def int2base(x, base):
    # Shamelessly stolen from https://stackoverflow.com/a/2267446
    if x < 0:
        sign = -1
    elif x == 0:
        return digs[0]
    else:
        sign = 1
    x *= sign
    digits = []
    while x:
        digits.append(digs[int(x % base)])
        x = int(x / base)
    if sign < 0:
        digits.append('-')
    digits.reverse()
    return ''.join(digits)
 
class StringIterator():
    
    def input(self, feature):
        str_from = feature.getAttribute('POSTCODE_FROM')
        str_to = feature.getAttribute('POSTCODE_TO')
        int_from = int(str_from, len(digs))
        int_to = int(str_to, len(digs))
        for x in range(int_from, int_to+1):
            f = feature.clone()
            f.setAttribute('POSTCODE', int2base(x, len(digs)))
            self.pyoutput(f)

Set "Class or function to process features" to "StringIterator", then expose the attribute "POSTCODE"'.

Sample result for a single feature entering with (from) 7887AD (to) 7887AJ:

0684Q00000ArKclQAF.png

Badge +10

It's a bit convoluted if you don't want to resort to python

postcode-from-to.fmw

Userlevel 2
Badge +17

I tried this with the InlineQuerier.

SQL Query:

select a.*, b."POSTCODE"
from ranges as a
inner join postcodes as b
on b."POSTCODE" between a."POSTCODE_FROM" and a."POSTCODE_TO"

0684Q00000ArKUrQAN.png

It's a bit convoluted if you don't want to resort to python

postcode-from-to.fmw

This method is most useful to me. Thanks a LOT!

Reply