Select to view content in your preferred language

Iterating through Arc Map Documents for "Map to KML" tool

865
3
Jump to solution
08-31-2018 02:59 PM
ColinBerg1
New Contributor II

Hi everyone,

I was hoping someone could help me with the tool called Map to KML.  I only want to be able to make it go through a folder that contains a bunch of  .mxd and have it produce kmz files for each map all in one shot.  I have tried to figure this out in Model Builder but I can't find an iterator or anything that will go through a folder with map documents that will be able to run through Map to KML.  Any ideas?  I'm not very fluent with using python so I want to try and accomplish this through Model Builder if possible.

Thanks

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ColinBerg1
New Contributor II
import arcpy
import os
#This will ask you to set a folder that contains the mxds.  This counts as your workspace.
#Then it will go through each mxd file in the list, and will run Map to KML on them using the variables set out in the for loop.
mxdfolder = arcpy.GetParameterAsText(0)
arcpy.env.workspace = mxdfolder

if len(arcpy.ListFiles('*.mxd')) > 0:
    for mxd in arcpy.ListFiles('*.mxd'):
        dataFrame = 'Layers'
        outKML = mxd[:-4]+'.kmz'
        arcpy.MapToKML_conversion(mxd, dataFrame, outKML)

else:
    arcpy.AddMessage('There are no map documents (*.mxd) in '+env.workspace)

In the end I forgot to close this down but the above code is what I ended up with and it works perfectly

Thanks!

View solution in original post

0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor

I see that Iterate Files does not work on mxd files. I think your only solution is to use Python in a Calculate Value tool. Fortunately this isn't that hard. Here is an untested Calculate Value setup based on the example code provided in the help for the Map to KML tool.

This requires that you have a model variable MXD folder with the folder location where the MXD files are (and where the kmzs are created).

# Calculate Value expression
# ff(r"%MXD folder%")

# Calculate Value code block
import os
import arcpy
def ff(mxdfolder)
    arcpy.env.workspace = mxdfolder
    for mxd in arcpy.ListFiles('*.mxd'):
        # Set Local Variables
        dataFrame = 'Layers'
        composite = 'NO_COMPOSITE'
        vector = 'VECTOR_TO_VECTOR'
        pixels = 2048
        dpi = 96
        clamped = 'ABSOLUTE'
        for scale in range(10000, 30001, 10000):
            # Strips the '.mxd' part of the name and appends '.kmz'
            outKML = mxd[:-4]+'.kmz'
            #Execute MapToKML
            arcpy.MapToKML_conversion(mxd, dataFrame, outKML, scale, 
                composite, vector, '', pixels, dpi, clamped)
    return True

# Calculate Value data type - True (1) if Calculate Value executes OK
# Boolean‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
ColinBerg1
New Contributor II

Cool! That will work, thanks!

Colin Berg

Mapping Technician

T: 403.295.0694

F: 403.295.2444

E: colin.berg@nwgeo.com<mailto:colin.berg@nwgeo.com>

North West Geomatics Ltd.

245 Aero Way NE

Calgary, AB T2E 6K2

www.nwgeo.com<http://www.nwgeo.com/>

North West is a part of HEXAGON.

0 Kudos
ColinBerg1
New Contributor II
import arcpy
import os
#This will ask you to set a folder that contains the mxds.  This counts as your workspace.
#Then it will go through each mxd file in the list, and will run Map to KML on them using the variables set out in the for loop.
mxdfolder = arcpy.GetParameterAsText(0)
arcpy.env.workspace = mxdfolder

if len(arcpy.ListFiles('*.mxd')) > 0:
    for mxd in arcpy.ListFiles('*.mxd'):
        dataFrame = 'Layers'
        outKML = mxd[:-4]+'.kmz'
        arcpy.MapToKML_conversion(mxd, dataFrame, outKML)

else:
    arcpy.AddMessage('There are no map documents (*.mxd) in '+env.workspace)

In the end I forgot to close this down but the above code is what I ended up with and it works perfectly

Thanks!

0 Kudos