0

I must convert .lyr files to Kml files. After searching, I already tested 3 codes. Unfortunately, when running the 3 codes Python displays same error:

Runtime error Traceback (most recent call last): File "", line 4, in File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\conversion.py", line 2322, in LayerToKML raise e ExecuteError: ERROR 000814: Invalid file type

I have no idea how to fix the error.

#1
arcpy.env.workspace = r"D:/ss3/layers"
ou2= r"D:\ss3\kml"
for fc in arcpy.ListFiles("*.lyr"):
    arcpy.LayerToKML_conversion(fc, 'buf_layer')
    arcpy.SaveToLayerFile_management('buf_layer', ou2 + os.path.splitext(fc)[0] + ".kml", "ABSOLUTE")

#2
arcpy.env.workspace = r"D:/ss3/layers"
ou2= r"D:\ss3\kml"
for fc in arcpy.ListFiles("*.lyr"):
    arcpy.MakeFeatureLayer_management(fc, 'buf_layer')
    arcpy.LayerToKML_conversion('buf_layer', ou2, "12000", "false", "DEFAULT", "1024", "96", "CLAMPED_TO_GROUND")

#3
import arcpy, os
arcpy.env.workspace = r"D:/ss3/polygons"
ou2= r"D:/ss3/layers/"
for fc in arcpy.ListFiles("*.shp"):
    arcpy.MakeFeatureLayer_management(fc, 'buf_layer')
    arcpy.SaveToLayerFile_management('buf_layer', ou2 + os.path.splitext(fc)[0] + ".lyr", "ABSOLUTE")
0

I don't think you need to make a feature layer and save it. You already have the layer files you would like to convert. The extension will be .kmz and not .kml. The .kml lives inside a zipped .kmz. You can extract the file and see for yourself. Some further reading from ArcGIS documentation:

This tool converts a feature or raster layer into a KML file containing a translation of Esri geometries and symbology. This file is compressed using ZIP compression, has a .kmz extension, and can be read by any KML client including ArcGIS Explorer, ArcGlobe, and Google Earth.

The code is commented to explain whats happening.

import arcpy, os

#location of you layers files
arcpy.env.workspace = "D:/ss3/polygonslayers"

#location to save kmz files
ou2 = "D:/ss3/kmz/"

#use wildcard to list only files with ".lyr" extension
for layer in arcpy.ListFiles("*.lyr"):

    #os.path.splitext: returns a tuple thus: (filename, .extension)
    # on conversion we keep the filename and swap extensions: .lyr for .kmz
    #filename.lyr ==> filename.kmz
    arcpy.LayerToKML_conversion(layer, ou2 + os.path.splitext(layer)[0] + ".kmz")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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