KML To Layer

Title  KML To Layer

Summary

Geoprocessing tool that converts a KML or KMZ file into feature classes and a layer file.


Usage


Syntax

Parameter Explanation
in_kml_file

The KML or KMZ file to translate.

output_data (Optional)

The name of the output file geodatabase and layer file. The default is the name of the input KML file.

include_groundoverlay (Optional)

Include ground overlays from the KML (raster, air photos, and so on). Use caution if the KMZ points to a service that serves raster imagery. The tool will attempt to translate the raster imagery at all available scales. This process could be lengthy and possibly overwhelm the service.

  • Checked—Ground overlay is included in the output.
  • Unchecked—Ground overlay is not included in the output. This is the default.

Code Samples

KMLToLayer example 1 (Python window)

Converts a KMZ file into a file geodatabase from the Python window.


import arcpy

arcpy.KMLToLayer_conversion(r'C:\kmls\earthquakes.kml',r'C:\gisdata\fromkmls','earthquake_09')
                    

KMLToLayer example 2 (stand-alone script)

The following script will convert a folder of KMZ and KML files into their respective file geodatabase. The feature classes inside these file geodatabases will then be consolidated into a single file geodatabase.


# Name: BatchKML_to_GDB.py
# Description: Converts a directory of KMLs and copies the output into a single fGDB.
#              A 2 step process: first convert the KML files, and then copy the featureclases

# Import system models
import arcpy, os

# Set workspace (where all the KMLs are)
arcpy.env.workspace="C:/VancouverData/KML"

# Set local variables and location for the consolidated file geodatabase
outLocation = "C:/WorkingData/fGDBs"
MasterGDB = 'AllKMLLayers.gdb'
MasterGDBLocation = os.path.join(outLocation, MasterGDB)

# Create the master FileGeodatabase
arcpy.CreateFileGDB_management(outLocation, MasterGDB)

# Convert all KMZ and KML files found in the current workspace
for kmz in arcpy.ListFiles('*.KM*'):
  print ("CONVERTING: {0}".format(os.path.join(arcpy.env.workspace, kmz)))
  arcpy.KMLToLayer_conversion(kmz, outLocation)


# Change the workspace to fGDB location
arcpy.env.workspace = outLocation

# Loop through all the FileGeodatabases within the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Skip the Master GDB
wks.remove(MasterGDBLocation)

for fgdb in wks:  
     
  # Change the workspace to the current FileGeodatabase
  arcpy.env.workspace = fgdb    

  # For every Featureclass inside, copy it to the Master and use the name from the original fGDB  
  featureClasses = arcpy.ListFeatureClasses('*', '', 'Placemarks')
  for fc in featureClasses:
    print ("COPYING: {0} FROM: {1}".format(fc, fgdb))    
    fcCopy = os.path.join(fgdb, 'Placemarks', fc)
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])
  

# Clean up
del kmz, wks, fc, featureClasses, fgdb
                    

Tags

Credits

Use limitations