Select to view content in your preferred language

Street name abbreviation replacement script works only sometimes in ArcMap.

2851
8
Jump to solution
12-12-2018 03:40 AM
DamienDi_Vittorio
Emerging Contributor

I am trying to write a field calculation script that changes road names with abbreviations to the full name.  Example: Day Dr. => DAY DRIVE or Phillips Rd. => PHILLIPS ROAD.  I wrote and tested the function in Jupyter notebook and everything works fine.  I got the script to work on my points feature class but the same script will not work on the same field in my line feature class.  Any suggestions?

Result Output

def street_name_fix(StreetName):
    StreetName = StreetName.rstrip('.')
    if StreetName is not None:
        if StreetName != '': 
            if StreetName.split()[-1].upper() == 'RD':
                return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'ROAD')

            elif StreetName.split()[-1].upper() == 'CIR':
                return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'CIRCLE').upper()

            elif StreetName.split()[-1].upper() == 'DR':
                return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'DRIVE').upper()

            elif StreetName.split()[-1].upper() == 'LN':
                return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'LANE').upper()

            elif StreetName.split()[-1].upper() == 'CT':
                return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'COURT').upper()

            elif StreetName.split()[-1].upper() == 'PL':
                return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'PLACE').upper()

            elif StreetName.split()[-1].upper() == 'ST':
                return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'STREET').upper()

            elif StreetName.split()[-1].upper() == 'BLVD':
                return '{} {}'.format(' '.join(StreetName.split()[:-1]), 'BOULEVARD').upper()
            
           

            else:
                return StreetName.upper()
    
        else:
            return ''
    
    else:
        return StreetName
0 Kudos
1 Solution

Accepted Solutions
PeteCrosier
Frequent Contributor

My solution would look something like this:

def street_name_fix(StreetName):
    replacements = {'RD': 'ROAD',
                    'CIR': 'CIRCLE',
                    'DR': 'DRIVE',
                    'LN': 'LANE',
                    'CT': 'COURT',
                    'PL': 'PLACE',
                    'ST': 'STREET',
                    'BLVD': 'BOULEVARD'}
                    
    StreetName = StreetName.upper().strip().rstrip('.')

    try:
        return '{} {}'.format(' '.join(StreetName.split()[:-1]), replacements[StreetName.split()[-1]])

    except IndexError:
        return StreetName
    
    except KeyError:
        return StreetName

View solution in original post

8 Replies
PeteCrosier
Frequent Contributor

It is failing because you are .split-ing a string that contains a single whitespace in to a list - the result of this is an empty list.

>>> a = u" "
>>> a.split()
[]

DamienDi_Vittorio
Emerging Contributor

Thanks Pete Crosier‌! I guess check for the space string first? Any suggestions on how to do this for 1 or multiple spaces?

Cheers,
Damien

0 Kudos
PeteCrosier
Frequent Contributor

My solution would look something like this:

def street_name_fix(StreetName):
    replacements = {'RD': 'ROAD',
                    'CIR': 'CIRCLE',
                    'DR': 'DRIVE',
                    'LN': 'LANE',
                    'CT': 'COURT',
                    'PL': 'PLACE',
                    'ST': 'STREET',
                    'BLVD': 'BOULEVARD'}
                    
    StreetName = StreetName.upper().strip().rstrip('.')

    try:
        return '{} {}'.format(' '.join(StreetName.split()[:-1]), replacements[StreetName.split()[-1]])

    except IndexError:
        return StreetName
    
    except KeyError:
        return StreetName
DamienDi_Vittorio
Emerging Contributor

Pete Crosier‌ thank you so much!  Much more Pythonic and simple!!! Much appreciated!  This works beautifully!

MichaelVolz
Esteemed Contributor

What is the business need to using the full name?  Are you keeping the abbrevation and just adding the full name to a new field?  I'm asking because ESRI's local government information model (LGIM0 which is used as the schema for out-of-the-box (OOTB) solutions uses abbreviations so you would want to keep the original field information if you ever plan to use the OOTB solutions.  At the least just something to think about.

0 Kudos
DamienDi_Vittorio
Emerging Contributor

This is how the client wants it in the end...

0 Kudos
DanPatterson_Retired
MVP Emeritus

throw this line at the top of your scripts

# -*- coding: utf-8 -*-

it is a caution for Unicode encoding vs other forms, especially if you are using python 2.7 and your data may have been developed in a post 2.7 world

DamienDi_Vittorio
Emerging Contributor

I realized when I converted to ansi in Notepad++ that it would run. Maybe this is related.

0 Kudos