I wrote a simple script supposed to convert all layers in a mxd into kmz and it doesn't work. Kmz are being created but they're not what they should be.

import arcpy

arcpy.env.workspace = pathworkspace
mxd = arcpy.mapping.MapDocument(pathmxd)

allLayers = arcpy.mapping.ListLayers(mxd)
for layer in allLayers:
    outputkmz = layer.name[:-4]+".kmz"

If i try to open myself in ArcMap the kmz created by the script, I get this error message. enter image description here

Some points are being displayed but in a strange way, like if they were aligned while it's not the case. If I open them with Google Earth, it just doesn't display anything.

Other information: - I'm using I'm using ArcMap 10.5.

  • I'm using PythonWin

  • If I try the script with a .mxd where there's only one layer in the ToC, it's working. As soon as I add another layer, it doesn't work anymore.

  • If I try the conversion manually with the toolbox in ArcMap (convert layer to kml), it's working, so I guess it means the problem doesn't come from my .lyr files.

  • Doesn't seem to be a problem of coordinates system

enter image description here enter image description here

This question is related to my other (more global) question here: Convert Shp to KMZ displaying labels using arcpy?

  • I can see your script is calling in arcpy, so I assume you are using ArcMap. Please update your question with the version of ArcMap you are using, the input layers you are using (are they *.lyr files or *.shp with a symbology assigned), how are you running code (in Arc or as a stand-alone script), have you assigned a sptatial reference to all the input layers? Is the spatial reference consistent between the input layers? I see the error you have shown when someone has defined an incorrect projection into a shapefile. i.e defining a WGS84 system onto a UTM shapefile. Commented Jan 23, 2018 at 10:52
  • Hi Keagan! I'm using ArcMap 10.5. The input layers are .lyr and there are indeed different spatial references between the input layers. I just tried the script with only one .lyr in the mxd, it was working. Then I added a second .lyr (same spatial reference) and it didn't work anymore. I'm using PythonWin for the script and I'm running it through the interactive window. Commented Jan 23, 2018 at 11:13
  • Actually, I guess I'm a bit confused between the shapefiles and the .lyr. In ArcMap Catalog, they're .lyr. Then I drag them in the ToC and from the ToC, when I look into the properties, data sources are .shp (not .lyr). So I don't really know if I should refer to the .shp or the .lyr. Commented Jan 23, 2018 at 11:27
  • Think of a lyr file as a store for the symbology of a shapefile or feature.I wont go into the details, but effectively a lyr file needs a feature or shapefile to display.Could you please show a screen shot of both of those features in your MXD and show the source tab.I think that the issue is an incorrectly defined spatial reference. Someone may have defined the coordinate system of your input files inconsistently, it happens a lot when spatial information is captured on a geographic (degree system) and defined a meter (UTM) system. It will show as being the same system, but will be incorrect. Commented Jan 23, 2018 at 11:43
  • I added the screenshots in the post. Commented Jan 23, 2018 at 12:00

1 Answer 1


To summarise:

  1. A *.lyr can be considered to be an empty file containing the definitions of symbology for a shapefile / feature class or raster. This link explains it fully ESRI Explanation
  2. You cannot directly add a kml or kmz file directly into ArcMap, you would need to convert it to a layer or shapefile.

I have looked over your code, and your needs and there are two potential solutions.

The First - Reading Directly from a Map Document (MXD)

Use this if you want to set up the symbology of the Feature first - in a MXD (as you want the KML to appear in Google Earth). The Python Script below takes all of the Features present on the map and converts them to KML / KMZ.

import arcpy

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument(r"C:\Temp\kmltest.mxd") #update with your mxd path
allLayer = arcpy.mapping.ListLayers(mxd)

for layers in allLayer:
    outputkmz = "C:/temp/" + layers.name[:-4] + ".kmz" #update with your path
print 'complete'

I tested this with a number of different input layers with varying projection systems and they all loaded into Google Earth correctly, as expected.

The Second - Reading directly from a Folder

This is for when you want to convert all Features in a directory to KML / KMZ. It can be used in a situation where a folder is constantly being updated with new features / or features are continually being updated. I haven't tested this with features in a database.I took some inspiration from this code: Example

import os
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:/temp/" #Update to your path
fclass = arcpy.ListFeatureClasses()

for fc in fclass:
    fcout = os.path.splitext(fc)[0]
    arcpy.MakeFeatureLayer_management(fc, fcout)
    arcpy.LayerToKML_conversion(fcout, fcout + '.kmz')
    print 'Completed conversion'

I have tested both sets of code and it works, both sets ran using features that were projected differently and some were even empty. From what we have discussed and how I have understood your issues, there is no reason why these wont work.

If these fail, then I would re-look at the input feature you are using on your side.

  • Thanks so much for the time you're spending on this. I just tried the first code (haven't changed anything) and I'm still having the same issue. First kmz is right but the next ones aren't working. It's soooooooo strange :( Commented Jan 24, 2018 at 12:22
  • I think you need to look at the inputs then. What are the sources of the layers in the MXD. Are they all shapefiles in a directory, or are they features within a geodatabase? Are you running the code from a *.py or are you running it from within the MXD using the Python Console? When I ran these I ran them with ArcMap closed, using the "Scripting Program". Commented Jan 24, 2018 at 12:26
  • I did, I tried with many different input... And they're all working if I try manually with the Toolbox so I guess it can't really come from the input right? They're shapefiles in a directory. I'm running the code from a .py in PythonWin, not from within the MXD. I now guess there must be a deeper issue behind this... Which scripting program are you using? Commented Jan 24, 2018 at 12:46
  • Try moving the input files from your current directory to another location. Do any of your inputs have special characters in the name (fullstops, commas, numbers etc?). I use "PyScripter" but it works when I use the straight IDLE from Python. Commented Jan 24, 2018 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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