Select to view content in your preferred language

Running the Map to KML tool

8135
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

I finally got this part to work.  The part that was throwing my code off is that you type a lowercase "p" in filepath, but it needs to be uppercase.

>>> import arcpy
>>> import arcpy.mapping as map
>>> mxd = map.MapDocument("CURRENT")
>>> mapname = str(mxd.filePath).split('\\')[-1:][0][:-4]
>>> print mapname
NW-CA-FERRYGRW-VZW

This is exactly what I needed to get the map name as a string.

0 Kudos
JeffWard
Honored Contributor

filePath1 = r"\\company.local\dfs\dept_name\gis_folder\projects\"

You need to take out the double backslash at the beginning of this if you are going to preface it with r.

Jeff Ward
Summit County, Utah
0 Kudos
JeffWard
Honored Contributor

Unless it's a network path - doh!

Jeff Ward
Summit County, Utah
0 Kudos
CarlSunderman
Frequent Contributor

As far as getting the file name,  i'm not sure of a real elegant way, but i'm sure there is.

Perhaps you could use

import os

#os.path.split(path)
head, tail = os.path.split("C:/tmp/myfile.mxd")
#head, tail = os.path.split("//tmp/myfile.mxd)

>>>print tail
myfile.mxd

or something along those lines. then strip off the mxd.

this would work for  "/" as well as "\"

0 Kudos
CoyPotts1
Deactivated User

I haven't had much of an opportunity to test out the suggestions for pulling out the mxd name, but I have a script that runs all the way up to the MapToKML portion.

# 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'\\company.local\dfs\dept_name\1)folder\folder\layerFiles folder'

# Set layer to apply symbology to
inputLayer = r'\\company.local\dfs\dept_name\1)folder\folder\folder\layerFile.lyr'

# Set layer that output symbology will be based on
symbologyLayer = r'DataFrame\FeatureClass'

# 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 = '%saveKML%'

# Set local MapToKML tool variables
inputMap = mxd
outputKML = '%outputKML%' + '.kmz'

# Execute MapToKML
arcpy.MapToKML_converstion(inputMap, outputKML)



I went ahead and set the script up to grab two variables that are within a model builder model.  The saveKML is the file path to be input by the user, and the outputKML is the name of the KML file, also input by the user.

Originally I set this script up in NotePad, saved as a .txt file, loaded into the ArcMap python window, saved as a .py script.  From there I loaded it into a script inside my toolbox and then inserted that script within the model with the two variables.  When doing it this way I got an error that said syntax error on line 2, but I couldn't figure out why.  So I went and manually entered the entire script into the ArcMap python window piece by piece, and it was all working up to the point executing the MapToKML tool.  Once I pressed enter after that line, it kicked back the following error:

"Runtime error

Traceback (most recent call last):

  File "<string>", line 1, 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"

Everything up to that point ran just fine.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Don't know if this is the whole problem, but "conversion" is spelled wrong.

CoyPotts1
Deactivated User

Heh, I do have a t in there . I'll see tomorrow what I get when I take that out.

I think it's about time to give this a rest for tonight.

0 Kudos
CoyPotts1
Deactivated User
arcpy.MapToKML_conversion(inputMap, outputKML)    
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, 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

The "t" was only in the version that I had written in NotePad.  The code above is what I had typed into the actual ArcMap Python window.

0 Kudos
CarlSunderman
Frequent Contributor

I normally hardcode values like inputs and outputs until i get everything working, then start adding the variables.

I would skip using model builder since you have all the python in the above comments to get it working. I personally have never used it

CoyPotts1
Deactivated User

I would like to get away from it, but it was the only way I could think of to add a dynamic variable that the user could change.  I'm getting errors with the code suggestions above to try and strip the file path.  I get the following error:

>>> import arcpy
>>> import arcpy.mapping as map
>>> mxd = map.MapDocument("CURRENT")
>>> mapname = str(mxd.filepath).split('\\')[-1:]
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'MapDocument' object has no attribute 'filepath'
>>> 

Just as you have suggested, I went ahead and gave literal values in place of the variables and I get the following:

... # Part 3:
... # Map to KML tool
... 
... # Set current workspace
... # env.workspace = "C:\Users\cpotts\Desktop\Test_Folders"
... 
... # Set local MapToKML variables
... inputMap = mxd
... outputKML = "output_test.kmz"
... 
... # Execute MapToKML
... arcpy.MapToKML_conversion(inputMap, outputKML)
... 
... 
... 
... 
Runtime error 
Traceback (most recent call last):
  File "<string>", line 46, 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

As per this LINK there are 10 conditions that are within the MapToKML tool, and I wonder if they're all required in order for the tool to run.  If that is the case, hothw do you configure pyon to simply run with the defaulted value instead of having to set that parameter.  When I run the tool manually, the data frame that it selects defaults to the only data frame in our map, which is again named identically to the map document, thus leading to the same issue as stated above where I still need to get the map name into a string so I can use it as a variable.

I suppose I could change our map template to have a static data frame name, which I could then hard code into the script since it would never change, but I don't want to do that unless I have to. 

The image below shows the inputs that I use when I manually run the tool.  I choose my mxd, it defaults to the only data frame in the map, I name the output file, and then I set the extent properties to match that of one of the feature classes.  Every other input I leave as the default value.

Capture.JPG

Thanks again for all the help guys!  I think we're getting closer to a solution .  This is the first script that I have ever tried to construct to run ArcMap tools, so I'm definitely at the forefront of the beginners stage lol.

0 Kudos