You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my efforts to speed up the creation of GeMS map packages, one thing that always seemed to take more time than it should was exporting layer files for the map. I typically did it manually, right-clicking each layer and saving the layer file. Well, no more! Here's a Python script that can be added to a toolbox or run from the Python window in ArcGIS Pro. I added it to the "Finalize" group of tools in the GeMS toolbox. See the script notes for more info.
In other good news, this fall ArcGIS Pro 3.2 will debut the "Save items to style" in the Share ribbon Save As section, similar to the ArcMap "Export map styles" tool.
UPDATE: Fixed a problem with annotation "sublayers" causing script to fail.
### ExportLyrxFiles.py
### Andrew L. Wunderlich
### andrew.wunderlich@tn.gov
### 9/5/2023
### Iterates through the current active map tab in ArcGIS Pro and exports all layers to LYRX files.
### Layers in group layers are exported to a folder within the output folder using the group layer name. See notes regarding layer naming issues.
### Works in both a data view and layout view in ArcGIS Pro.
### NOTES:
### Layer names in map should be simple. The script can find and deal with most illegal file name characters,
### however, backslashes are a valid part of group layer names and can't easily be differentiated from
### backslashes that the user may have put in the map layer name. The following characters are explicitly
### forbidden in any Windows environment: " | % : / , . [ ] < > * ?. If the script encounters any of these
### characters, it replaces them with a dash. Repeating illegal characters are replaced by a single dash
### regardless of the original character: e.g., <<<layer>>> or <?/layer/?> would both become -layer-.
###
### When naming layers in the map, spaces and underscores are also discouraged, as well as # pound, & ampersand,
### { left curly bracket, } right curly bracket, $ dollar sign, ! exclamation point, ' single quotes, @ at sign,
### + plus sign, ` backtick, and = equal sign. While not all these characters will cause an error, some
### certainly will and others may produce unexpected or undesirable results.
### When adding to a toolbox:
### GENERAL:
### Name: ExportMapLayersToLyrx
### Label: Export map layers to .lyrx
### Description: Iterates through the current active map tab in ArcGIS Pro and exports all layers to LYRX files.
### Layers within groups are exported to a folder with the group layer name within the output folder.
### Works in both a data view and layout view in ArcGIS Pro. See notes regarding layer naming issues.
### SOURCE:
### Script File: point to this file
### PARAMETERS:
### "Output folder" as a Folder
import arcpy, os, re
import GeMS_utilityFunctions as guf
# This code can be run directly in the Python code window in ArcGIS Pro if an outFolder is specified
# Set the output folder. Use sys.argv[1] if used through the toolbox.
#outFolder = r"C:\temp\LAYERS\LYRX"
outFolder = sys.argv[1]
# Get the current ArcGIS Pro project file
aprx = arcpy.mp.ArcGISProject('CURRENT')
# Set the map to look for layers to export
aprxMap = aprx.activeMap
# Define list of illegal characters for file names in Windows:
# Can't include the backslash here, it messes up the group layer paths
illChars = "\"|%:/,.[]<>*?"
# Iterate through the layers and export them to lyrx files
for lyr in aprxMap.listLayers():
# Check if link is broken
if lyr.isBroken == False:
# Check if layer is a group layer (don't make a lyrx file; will become folder)
if lyr.isGroupLayer == False:
# Check to make sure the layer is compatible with being saved as a lyrx file
if lyr.supports("CONNECTIONPROPERTIES"):
# Set the lyrx base name (as seen in the TOC; not the FULL name, i.e., lyr.longName)
lyrxName = str(lyr.name)
#guf.addMsgAndPrint("layer name: " + lyrxName)
# Split the full name str(lyr) using the layer base name to get the path
grpPath = str(lyr).split(lyrxName)[-2:]
#guf.addMsgAndPrint("group path: " + str(grpPath[0]))
# Check for characters in the layer name that would cause an OSError in lyr.saveACopy
for c in illChars:
lyrxName = lyrxName.replace(c,"-")
# Get rid of a backslash in the layer file name here
lyrxName = lyrxName.replace("\\","-")
# Replace consecutive dashes with a single dash
lyrxName = re.sub(r"--+", "-", lyrxName)
# Create the group path name if layer is nested in a group layer (will be blank if not)
lyrxPath = str(grpPath[0])
# Check for characters in the group name that would cause an OSError in lyr.saveACopy
# Backslashes in the group layer name WILL create nested folders. NO GOOD BUT HARD TO FIX.
for c in illChars:
lyrxPath = lyrxPath.replace(c,"-")
# Replace consecutive dashes with a single dash
lyrxPath = re.sub(r"--+", "-", lyrxPath)
# Concatenate the complete path here
lyrxPath = os.path.join(outFolder, lyrxPath)
# Create the output path (folder) if it doesn't already exist
if not os.path.exists(lyrxPath):
os.makedirs(lyrxPath)
# Set the lyrx file name
lyrxFile = os.path.join(outFolder, lyrxPath, lyrxName + ".lyrx")
# Save the lyrx file
lyr.saveACopy(lyrxFile)
guf.addMsgAndPrint("Saved: " + lyrxFile)
#else: # layer isn't exportable as a lyrx file (annotation groups, wms sublayers, etc.)
#guf.addMsgAndPrint(str(lyr.longName) + " is not exportable as a lyrx file")
else:
# Layer has a broken link, do nothing. Although it probably doesn't matter...
guf.addMsgAndPrint("Layer " + lyr.name + " has a broken link and will not be saved...")
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
In my efforts to speed up the creation of GeMS map packages, one thing that always seemed to take more time than it should was exporting layer files for the map. I typically did it manually, right-clicking each layer and saving the layer file. Well, no more! Here's a Python script that can be added to a toolbox or run from the Python window in ArcGIS Pro. I added it to the "Finalize" group of tools in the GeMS toolbox. See the script notes for more info.
In other good news, this fall ArcGIS Pro 3.2 will debut the "Save items to style" in the Share ribbon Save As section, similar to the ArcMap "Export map styles" tool.
UPDATE: Fixed a problem with annotation "sublayers" causing script to fail.
Beta Was this translation helpful? Give feedback.
All reactions