Skip to content

Instantly share code, notes, and snippets.

@arashmad
Last active December 11, 2020 13:21
Show Gist options
  • Save arashmad/252fbfd2a55c612a808261c27392d8a7 to your computer and use it in GitHub Desktop.
Save arashmad/252fbfd2a55c612a808261c27392d8a7 to your computer and use it in GitHub Desktop.
Creating .kmz file automatically using ESRI ArcPy
import os
import arcpy
def mxdToKMZ(url):
if os.path.exists(path):
mxd_dir = os.path.split(path)[0]
kmz_dir = mxd_dir + "/kml"
os.mkdir(kmz_dir)
mxd = arcpy.mapping.MapDocument(r"path\to\mxd\file\name.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = arcpy.mapping.ListLayers(mxd, "", df)
for layer in layers:
lyr_name = layer.name
kmz_name = + lyr_name + '.kmz'
arcpy.LayerToKML_conversion(layer, kmz_name)
else:
raise Exception("File not found. => Check .mxd file path.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment