Skip to main content
Solved

Normalisation of street name with Python or Any Transformer


Hi,

I am having problem to find a suitable way to normalise Street Name in my geodatabase.

Example of Street Name to be normalised;

Example List of abbrivations to search and replace;

Any idea how to solve it either with a transformer or Python Script.

Many Thanks!

Abdul

Best answer by takashi

Hope this works fine!

0684Q00000ArKDtQAN.png

# PythonCaller Script Example (Python 2.7 only)
class FeatureProcessor(object):
    def __init__(self):
        self.map = None
        
    def input(self,feature):
        if self.map == None:
            search = feature.getAttribute('_map{}.To Search')
            change = feature.getAttribute('_map{}.To change')
            self.map = zip(search, change)
            
        name = feature.getAttribute('Street Name')
        for src, dst in self.map:
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)
# PythonCaller Script Example (works with both Python 2.7 and 3.5)
class FeatureProcessor(object):
    def __init__(self):
        self.search = None
        self.change = None
        
    def input(self,feature):
        if self.search == None:
            self.search = feature.getAttribute('_map{}.To Search')
            self.change = feature.getAttribute('_map{}.To change')
            
        name = feature.getAttribute('Street Name')
        for src, dst in zip(self.search, self.change):
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)

View original
Did this help you find an answer to your question?

8 replies

david_r
Celebrity
  • June 15, 2017

Your captures aren't showing, but in general you can use a series of StringReplacer transformers to search and replace text inside e.g. street name attribute.


mark2atsafe
Safer
Forum|alt.badge.img+49
david_r wrote:

Your captures aren't showing, but in general you can use a series of StringReplacer transformers to search and replace text inside e.g. street name attribute.

Also there are some address postcode validators in the FME Hub that might be of use.

 

 


  • Author
  • June 16, 2017
david_r wrote:

Your captures aren't showing, but in general you can use a series of StringReplacer transformers to search and replace text inside e.g. street name attribute.

str-name-and-normalization-list-sample.xlsx

 

Hi David,

Thanks for your answer but they are so many about 100s and if I do with StringReplacer it will be much work and mess :). we have a python module for this problem from an other collegue but I can not intergrate it into FME because have not worked with Python.

Attach you can find the sample data.

 


takashi
Evangelist
  • Best Answer
  • June 16, 2017

Hope this works fine!

0684Q00000ArKDtQAN.png

# PythonCaller Script Example (Python 2.7 only)
class FeatureProcessor(object):
    def __init__(self):
        self.map = None
        
    def input(self,feature):
        if self.map == None:
            search = feature.getAttribute('_map{}.To Search')
            change = feature.getAttribute('_map{}.To change')
            self.map = zip(search, change)
            
        name = feature.getAttribute('Street Name')
        for src, dst in self.map:
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)
# PythonCaller Script Example (works with both Python 2.7 and 3.5)
class FeatureProcessor(object):
    def __init__(self):
        self.search = None
        self.change = None
        
    def input(self,feature):
        if self.search == None:
            self.search = feature.getAttribute('_map{}.To Search')
            self.change = feature.getAttribute('_map{}.To change')
            
        name = feature.getAttribute('Street Name')
        for src, dst in zip(self.search, self.change):
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)


takashi
Evangelist
  • June 16, 2017
takashi wrote:

Hope this works fine!

0684Q00000ArKDtQAN.png

# PythonCaller Script Example (Python 2.7 only)
class FeatureProcessor(object):
    def __init__(self):
        self.map = None
        
    def input(self,feature):
        if self.map == None:
            search = feature.getAttribute('_map{}.To Search')
            change = feature.getAttribute('_map{}.To change')
            self.map = zip(search, change)
            
        name = feature.getAttribute('Street Name')
        for src, dst in self.map:
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)
# PythonCaller Script Example (works with both Python 2.7 and 3.5)
class FeatureProcessor(object):
    def __init__(self):
        self.search = None
        self.change = None
        
    def input(self,feature):
        if self.search == None:
            self.search = feature.getAttribute('_map{}.To Search')
            self.change = feature.getAttribute('_map{}.To change')
            
        name = feature.getAttribute('Street Name')
        for src, dst in zip(self.search, self.change):
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)

hmm, I don't know why the script doesn't change any string with Python 3.5, though it seems to work fine with Python 2.7.

 


  • Author
  • June 16, 2017
takashi wrote:

Hope this works fine!

0684Q00000ArKDtQAN.png

# PythonCaller Script Example (Python 2.7 only)
class FeatureProcessor(object):
    def __init__(self):
        self.map = None
        
    def input(self,feature):
        if self.map == None:
            search = feature.getAttribute('_map{}.To Search')
            change = feature.getAttribute('_map{}.To change')
            self.map = zip(search, change)
            
        name = feature.getAttribute('Street Name')
        for src, dst in self.map:
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)
# PythonCaller Script Example (works with both Python 2.7 and 3.5)
class FeatureProcessor(object):
    def __init__(self):
        self.search = None
        self.change = None
        
    def input(self,feature):
        if self.search == None:
            self.search = feature.getAttribute('_map{}.To Search')
            self.change = feature.getAttribute('_map{}.To change')
            
        name = feature.getAttribute('Street Name')
        for src, dst in zip(self.search, self.change):
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)

Dear @takashi,

 

Thanks a lot. It is sort of working. 

 


takashi
Evangelist
  • June 16, 2017
vdiyalshah wrote:

Dear @takashi,

 

Thanks a lot. It is sort of working.

 

Good to hear.

 

Note that the previous script works only with Python 2.7. I added another script that works with both Python 2.7 and Python 3.5

 

 


takashi
Evangelist
  • June 16, 2017
takashi wrote:

Hope this works fine!

0684Q00000ArKDtQAN.png

# PythonCaller Script Example (Python 2.7 only)
class FeatureProcessor(object):
    def __init__(self):
        self.map = None
        
    def input(self,feature):
        if self.map == None:
            search = feature.getAttribute('_map{}.To Search')
            change = feature.getAttribute('_map{}.To change')
            self.map = zip(search, change)
            
        name = feature.getAttribute('Street Name')
        for src, dst in self.map:
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)
# PythonCaller Script Example (works with both Python 2.7 and 3.5)
class FeatureProcessor(object):
    def __init__(self):
        self.search = None
        self.change = None
        
    def input(self,feature):
        if self.search == None:
            self.search = feature.getAttribute('_map{}.To Search')
            self.change = feature.getAttribute('_map{}.To change')
            
        name = feature.getAttribute('Street Name')
        for src, dst in zip(self.search, self.change):
            name = name.replace(src, dst)
            
        feature.setAttribute('Normalized', name)
        self.pyoutput(feature)

OK. The behavior of "zip" function in Python 3.5 was different from Python 2.7. It's the reason why the previous script didn't work with Python 3.5 interpreter.

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings