I am trying to create a script that will apply a symbology layer to the current map, save the map, export the map to KML, and then reapply the original symbology from a layer file.
Apply symbology from layer file:
# Import system modules
import arcpy
from arcpy import env
# Set the current workspace
env.workspace = "file path where layer files are saved"
# Set layer to apply symbology to
inputLayer = "layer file name"
# Set layer that output symbology will be based on
symbologyLayer = "feature class within map"
# Apply the symbology from the symbology layer to the input layer
arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer)
This part works fine.
Save the map:
# Set up map variable
import arcpy.mapping as map
# Set map variable to current mxd
mxd = map.MapDocument("CURRENT")
# Save the map document
mxd.save()
This part works fine.
Map to KML:
# Set environment settings
arcpy.env.workspace = "haven't figured this part out yet"
# Set Local Variables
dataFrame = 'Layers'
# Sets the KMZ name to be equal to the mxd
outKML = mxd+'.kmz'
#Execute MapToKML
arcpy.MapToKML_conversion(mxd, dataFrame, outKML)
This is the part that I'm having trouble with. I'm not entirely sure how to correctly construct the Map to KML portion. This script will be run on many different maps and the workspace for each map will be identical to each other with the exception of the map name. I tried to set the workspace to the following path, with no luck:
arcpy.env.workspace = "folder1\folder2\folder3\" + mxd + "\folder4\folder5"
But this results in this error "Parsing error SyntaxError: unexpected character after line continuation character (line 1)
Apply symbology from layer file:
Simply a repeat of the previous iteration of the tool with a different layer file.
Solved! Go to Solution.
EUREKA!
That did the trick!!!!!
The final working code is:
# Name: Map to KML # Description: This script is used within a model to export the map to a KMZ file for distribution # Author: Coy Potts # Parts: (1) Apply Symbology Layer File, (2) Save mxd, (3) MapToKML tool, (4) Apply Symbology Layer File # Import system modules import arcpy from arcpy import env import arcpy.mapping as map # Part 1: # Apply Symbology Layer file # Set current workspace env.workspace = r'file path for layer files' # Set layer to apply symbology to inputLayer = r'feature class' # Set layer file for the output symbology symbologyLayer = r'layer file' # Apply the symbology from the symbology layer to the input layer arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer) # Part 2: # Save the map # Set the map document to the current map mxd = map.MapDocument("CURRENT") # Save the map mxd.save() # Part 3: # Map to KML tool # Set current workspace env.workspace = "C:\Users\cpotts\Desktop\Test_Folders" # Set local MapToKML variables df = arcpy.mapping.ListDataFrames(mxd)[0].name outputKML = "C:\Users\cpotts\Desktop\Test_Folders\output_test.kmz" mapScale = 0 composite = 'NO_COMPOSITE' vector = 'VECTOR_TO_VECTOR' extent = r'feature class' imageSize = 1024 dpi = 96 ignore_z = 'ABSOLUTE' # Execute MapToKML arcpy.MapToKML_conversion(mxd.filePath, df, outputKML, mapScale, composite, vector, extent, imageSize, dpi, ignore_z)
Thanks for all of the help in getting this to working for me .
Now...I need to get the output kmz and workspace to be dynamic instead of static. I'll play around with the getparameterastext functions and see what I can come up with.
You want the output set like that, not the workspace, i think?
and the mxd name should be passed as a string
output = "folder1/folder2/folder3/" mxdname = "mystring" arcpy.<whatever>(output + mxdname + "/folder4/folder5/";
Okay, I sort of understand that. And yes, I was playing with other variations and I was getting the error saying that I couldn't mix a string and a map document, but I couldn't figure out how to translate my map document into a string like ymou do in your exaple.
I'm very green when it comes to Python scripting, so I'm not sure how to correctly answer your question regarding the workspace vs. output. The tool needs to have a workspace in order to know where to save the KML, correct? An alternative, and I think it's what you are showing above, is that you simply create the variables as you show, and then set outKML to equal the concatenation of those variables. So instead of saying save this output in this workspace, you're saying that the output KML equals the entire file path. Am I understanding that correctly?
In regards to the mxdname = "mystring", can you say mxdname = mxd? Or would that still have it be referencing a map document instead of turning it into a string. I'm just a little unclear on how to take my map document's name and add it into the whole folder path.
An additional question...what IDLE do you use to get the numbered structure next to each line of your code? I have been using the python window within ArcMap just because that's all I know to use, but I don't really like it all that much.
And thank you so much for the response!
Here's how to post code snippets in GeoNet.
Thank you Jeff, I will note that for future reference.
As far as I can tell, your main problem with:
arcpy.env.workspace = "folder1\folder2\folder3\" + mxd + "\folder4\folder5"
is ending your string with \" which translates to an escaped quotation mark, or literally the symbol ". That part of the string never closes. For paths, you should use r"path\path", or "path/path" or "path\\path".
Furthermore, the easy way to concatenate paths is by using os.path.join, which uses the system path separator to join path strings:
>>> import os.path ... first_concat = os.path.join(r"C:\folder1\folder2\folder3","someMapName") ... print first_concat ... second_concat = os.path.join(first_concat,r"folder4\folder5") ... print second_concat ... C:\folder1\folder2\folder3\someMapName C:\folder1\folder2\folder3\someMapName\folder4\folder5
Okay, that is also helpful. I'll go a little deeper into explanation. A sample file path is listed below (renaming the actual folder names for proprietary purposes):
\\company.local\dfs\dept_name\gis_folder\projects\region\region-state-name-carrier\deliverables
That is the full file path that I would be working with. We name the maps identically to the "region-state-name-carrier" naming convention (e.g. "NC-IL-MYHOUSE-VZW"). The mxd is saved at this stage of the file path. Once we create a KML output from the mxd, it is saved in the deliverables stage of the file path.
I thought that since all file paths are identical with the exception of the "region\region-state-name-carrier" portion, I could just make static variables for "\\company.local\dfs\dept_name\gis_folder\projects\" and "\deliverables", and use the map name as the dynamic variable that will change from project to project. Essentially I need to take the map name and convert it to a string. I would take that string and strip out only the first two characters to uses as my "region", and then I would use the whole string as my "region-state-name-carrier" portion. I'll list the potential variables below for a better understanding.
mapname = mxd
- I haven't figured out how to do this yet. This is where I want to turn my map name into a string.
region = mapname[:2]
- I haven't figured this part out either, but through google I found that this is one way to strip parts of a string apart, I but it wasn't working when tried.
filePath1 = r"\\company.local\dfs\dept_name\gis_folder\projects\"
filePath2 = r"\deliverables"
arcpy.env.workspace = filePath1 + region + mapname + filePath2
if that doesn't work I am brainstorming and thinking this might work for the workspace:
workspace = filePath1 + region + mapname + filePath2
arcpy.env.workspace = workspace
I'm only thinking of this because the arcpy.env.workspace function might not like concatenating the multiple variables...but I really don't know :/.
Thanks for your input so far!
How about:
mapname = str(mxd.filepath).split('\\')[-1:]
This gets the path of the map document, converts it to a string, splits it by the double backslash and puts the last element in mapname.
Edited to fix my hasty slicing error.
After looking at this, this comes back as a single element list.
This might be better:
mapname = str(mxd.filepath).split('\\')[-1:][0][:-4]