*Before I get into today’s script, I’ve noticed that the embedded gists do not appear on mobile devices when accessing the feed for this blog via Feedly or some other RSS reader. The post needs to be opened in a web browser to view the code. I’ll also be including a link to it at the end of the post.
When I create feature classes for asset management data manually instead of automatically, I always make sure to name them consistently. However, I rarely change the feature class alias manually. This means that when I need to pull all the layers into a map to create a kml, I need to rename each layer.
Which is why I turn to Python instead.
|# @date 14/08/2015|
|# @author Cindy Williams|
|# Quickly formats the names of all the layers in a map|
|# document. In this example, the part of the name before|
|# the first full stop is removed, underscores are replaced|
|# spaces, a regex is used to insert spaces before capitals|
|# and the string is converted to proper case.|
|# For use in the Python window in ArcMap.|
|mxd = arcpy.mapping.MapDocument("CURRENT")|
|lyrs = arcpy.mapping.ListLayers(mxd)|
|for lyr in lyrs:|
|new_name_replaced = lyr.name.split(".").replace("_", " ")|
|new_name_spaced = re.sub(r"(\w)([A-Z])", r"\1 \2", new_name_replaced)|
|lyr.name = new_name_spaced.title()|
Of course, the way the name gets split will change depending on the current format of the names. In this instance, the part of the name that I needed occurred after the first full stop, and contained underscores which needed to be replaced by spaces. I then used a regular expression to insert spaces between
Link to the code is here.