Inserting spaces into CamelCase

I have some trauma related to regular expressions from my university days. Yes, they are super useful. Do I enjoy using them? Does anyone?

Image result for regular expressions meme

I routinely modify and create new instances of the AMIS GIS data model. Depending on the client and the type of assets they have, there can be 4 feature datasets, containing about 5 feature classes each, or 9 feature datasets with up to 50 feature classes spread across the database.

Inevitably at some point in the process of losing my mind, I will forget that the alias is lost when creating a new feature class, or randomly copying it over or whatever. I then end up with dozens of feature classes with really ugly looking abbreviated layer names in CamelCase. So for posterity, and so that I will never ever forget this ridiculously easy thing again:

# @date 05/10/2017
# @author Cindy Jayakumar
# Change the alias of feature classes to the
# title case version of the CamelCase name
# e.g. WaterPumpStation => Water Pump Station
import arcpy
import re
gdb = r"C:\Some\Arb\Folder\test.gdb"
def alterFCAlias(name):
return re.sub(r"\B([A-Z])", r" \1", name)
for root, _, fcs in arcpy.da.Walk(gdb):
for fc in fcs:
alias = alterFCAlias(fc)
arcpy.AlterAliasName(fc, alias )
print("{0} => {1}".format(fc, alias))

# @credit
import re
text = "ThisIsATest"
re.sub(r"\B([A-Z])", r" \1", text)

One thing I’ve omitted from the script is that I usually store the feature class names with a prefix, e.g. fc_name = wps_WaterPumpStation. In this case, I would use split on the feature class name before passing it to the alterFCAlias function i.e. fc_name.split("_")[1].

Link for mobile users is here.

Aaaaaaand I’ve just realised that I’ve forgotten this basic task so many times over the last few years that I actually already have a blog post about it, except for some reason I was updating the layer names in ArcMap every time instead of resetting it once on the feature classes themselves.

Image result for what were you thinking meme

One thought on “Inserting spaces into CamelCase

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.