Select to view content in your preferred language

Running the Map to KML tool

8130
34
Jump to solution
05-14-2015 12:00 PM
CoyPotts1
Deactivated User

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.

0 Kudos
34 Replies
CoyPotts1
Deactivated User

Still kicking back and referencing line 2278 of the conversion.py script.

... # Part 3:
... # Map to KML tool
... 
... # Set current workspace
... env.workspace = "C:\Users\cpotts\Desktop\Test_Folders"
... 
... # Set local MapToKML variables
... inputMap = mxd
... df = arcpy.mapping.ListDataFrames(mxd)[0]
... outputKML = "C:\Users\cpotts\Desktop\Test_Folders\output_test.kmz"
... mapScale = 0
... composite = 'NO_COMPOSITE'
... vector = 'VECTOR_TO_VECTOR'
... extent = r'Entire Coverage Area\Fiber Segments'
... imageSize = 1024
... dpi = 96
... ignore_z = 'ABSOLUTE'
... 
... # Execute MapToKML
... arcpy.MapToKML_conversion(mxd, df, outputKML, mapScale, composite, vector, extent, imageSize, dpi, ignore_z)
... 
... 
... 
Runtime error 
Traceback (most recent call last):
  File "<string>", line 54, 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
>>> 

I also went ahead and set all of the other variables too just to try it out.  It accepts them all, but I get the error at the bottom.

0 Kudos
JeffWard
Honored Contributor

The documentation is wrong.  The first parameter doesn't take the mxd object, it takes the path.

dataFrame = arcpy.mapping.ListDataFrames(mxd)[0].name
arcpy.MapToKML_conversion(mxd.filePath, dataFrame, outputKML)

I put in Carl Sunderman's bit to get the name of the first data frame in the map.

Jeff Ward
Summit County, Utah
CoyPotts1
Deactivated User

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.

0 Kudos
JeffWard
Honored Contributor

Glad it works.  I sent feedback on the help doc saying it was misleading.

Jeff Ward
Summit County, Utah
0 Kudos
CarlSunderman
Frequent Contributor

Glad it works. Python is actually pretty easy to learn, so keep using it and you can do some pretty cool things.

nice catch jmward