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.
If you look in the left side of that matrix in the help doc you can see that the optional parameters have (optional) under them. So only the first three parameters are required. The input types are listed on the right. You are actually missing the data frame parameter in your call. Also, the code I posted above grabs the name of your map document as a string. You need the actual map document object for the first parameter.
arcpy.MapToKML_conversion(inputMap, 'Layers', outputKML)
Try using GetParameterAsText for your model variables inside your script. Python is interpreting '%saveKML%' as strings.
env.workspace = arcpy.GetParameterAsText('0')
outputKML = arcpy.GetParameterAsText('1') + '.kmz'
Paste your complete code.
Try
import os import arcpymxd = arcpy.mapping.MapDocument("CURRENT")
mxdLocation = mxd.filePath
head, tail = os.path.split(mxdLocation) mxdname = tail.replace(".mxd", "")
It seems you are getting there.
Edit:
had to fix mxdLocation, case on in line 7 to match line 5.
I actually finally got one of the above suggestions to work in regards to getting the map name as a string. Their suggestion wasn't properly written, so it didn't work for me. I changed ".filepath" to ".filePath" and it worked like a charm.
Here is the latest run and error message for the entire code:
>>> # 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'layer file drive path' ... ... # 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 ... mapname = str(mxd.filePath).split('\\')[-1:][0][:-4] ... inputMap = mxd ... dataFrame = mapname ... outputKML = "output_test.kmz" ... ... # Execute MapToKML ... arcpy.MapToKML_conversion(inputMap, dataFrame, outputKML) ... ... ... ... Runtime error Traceback (most recent call last): File "<string>", line 48, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\conversion.py", line 2278, in MapToKML raise e RuntimeError: Object: Error in executing tool
There's something with my execution of the MapToKML tool that it's not liking. The three variables that I used in the MapToKML execution are the three that are required inputs. Do you think that it requires that all 10 inputs be included?
Does your dataframe name match your mxd name?
Hardcode the output path and see what happens.
outputKML = "C:\\<insert stuff>\myfile.kmz"
No you don't need all inputs, if there are any that you are skipping you'll need to account for those like:
(stuff1, stuff2, , more stuff, , etc)
The data frame does have the same name as the mxd.
I did as you suggested above and I still got the exact same error as above.
Question regarding your suggestion, though:
I thought that the line that sets the current workspace (line 39) within Part 3 was taking care of where the outputKML would be saved...?
Your dataFrame variable holds the name of the map, not the required data frame name, unless they happen to be the same.
How do I make it hold the actual data frame instead of a string variable?
Sorry, just read the help and it looks like you only need the data frame name, not the data frame object. I hoped to have edited that in time.
If it's the first dataframe then:
df = arcpy.mapping.ListDataFrames(mxd)[0]
then just use df in your export