I have experienced, and no doubt you have too, the tedium of saving out many layer files for the layers you have just created in your ArcMap map document. Having spent perhaps hours perfecting the symbology – getting that labelling to show just as you would like, tweaking the colours so they’re just right – the last thing you want to do is to have to save out what could be tens or even hundreds of layer files to a workspace. In this blog I show you how 4 lines of Python can save you a whole lot of time.
How do I save a layer file?
ArcMap allows you to save a layer out to a layer file using this workflow:
1) Right click on the layer and select “Save As Layer File…”:
2) A dialog is shown that allows you to navigate to a workspace and save your layer file:
There must be a better way!
Repeating this workflow many times is pretty tedious! The answer? Use Python.
A simple Python script with just four lines of code enables you to save every layer in your map to the same workspace that you saved your map document.
What does the Python script actually do?
Let’s go line-by-line through the script and look at what each line does:
1. This imports the arcpy module so that it can be used in your Python script.
2. Gets a reference to the currently loaded map document (replace CURRENT with a path name to access other map documents).
3. For every layer in the map document, perform the action stated on the next line.
4. Save a copy of the layer to a file with a name that is the layer name listed in the map document, with the suffix “.lyr”. The layers are saved to the same location as the Map Document (MXD).
This example demonstrates that those GIS professionals who gain an understanding of Python and the arcpy module can look forward to becoming more productive in their use of ArcMap.
Watch this blog as a video: Creating Layer Files with Python
Posted by Simon Kettle, GIS Consultant, Exprodat