Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to export map layers from an MXD to KML using arcpy with the following code:

import arcpy, os, sys, traceback

arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument(r"C:\Layer_KML\MXD\Test.mxd")

try:
    for lyr in arcpy.mapping.ListLayers(mxd):
        i = 1 # forgive the wonky naming convention, I just wanted something quick for test purposes
        outKML = "C:\\Layer_KML\\KML\\" + "Test" + str(i) + ".kmz"

        # These lines are where the error/s is occuring
        newLayer = arcpy.SaveToLayerFile_management(lyr, "C:\\Layer_KML\\Layers\\" + str(lyr) + ".lyr")        
        arcpy.LayerToKML_conversion(newLayer, outKML, "20000", "false", "DEFAULT", "1024",     "96")

        i = i + 1

except:
        ...

Though my traceback keeps telling me : {ERROR 000623: Invalid value type for parameter in_layer.} for both (I commented out each indivudally to test) the .SaveToLayerFile, and LayerToKML routines. I am assuming these routines want to except a full file path, while the arcpy.ListLayer() only yields a basename. Maybe? I am really not sure. Any thoughts as to why this isn't working?

I have already seen this question : How to batch export layers to kml in Arcmap and this is not what I am looking for. I want to do this in Python rather than through a toolbox tool.

Any help is appreciated. Thanks!

share|improve this question

1 Answer 1

You don't need to save to layer file, just do this:

import arcpy, os, sys, traceback

arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument(r"C:\Layer_KML\MXD\Test.mxd")

try:
    for i, lyr in enumerate(arcpy.mapping.ListLayers(mxd)):
        outKML = "C:\\Layer_KML\\KML\\" + "Test" + str(i + 1) + ".kmz"

        # These lines are where the error/s is occuring
        arcpy.LayerToKML_conversion(lyr, outKML, 20000, "false", "DEFAULT", "1024",     "96")
except:
        ...
share|improve this answer
    
I had tried this but it did not work. I tried your code but my traceback still spews: Error Info: <class 'arcgisscripting.ExecuteError'>: ERROR 000622: Failed to execute (Layer To KML). Parameters are not valid. ERROR 000623: Invalid value type for parameter layer. –  Chaz Jan 16 '13 at 2:30
1  
In the documentation it looks like the arcpy.LayerToKML_conversion() is excepting a double for the scale parameter and you the code above seems to be passing a string. try this arcpy.LayerToKML_conversion(lyr, outKML, 20000, "false", "DEFAULT", "1024", "96") –  dango Jan 16 '13 at 8:28
    
This works dango, thanks a bunch. I knew it was something simple that I was overlooking. Its odd, I still get the same error, but I actually get working KML's in my output folder. However I think I narrowed down the problem. It was trying to export an ESRI basemap as a KML as well (since I guess it is a layer in mxd). I think the basemap kept causing the LayerToKML to fail. I dont suppose either of you know of a way to add a conditional to my script that only gets a list of the layers that are visible(turned on)? Thanks all. –  Chaz Jan 16 '13 at 14:15
    
maybe you could try something like this: –  dango Jan 17 '13 at 7:23
1  
you already have the layer object "lyr" so all you need to do is add an something like this "if lyr.visible == True:". Hope that helps. Check out this if you want to read more on arcpy.mapping layers help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/… –  dango Jan 17 '13 at 7:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.