KML To Layer (Conversion)

License Level:BasicStandardAdvanced

Summary

Converts a KML or KMZ file into feature classes and a layer file. The layer file maintains the symbology found within the original KML or KMZ file.

Learn more about KML support in ArcGIS

Usage

Syntax

KMLToLayer_conversion (in_kml_file, output_folder, {output_data}, {include_groundoverlay})
ParameterExplanationData Type
in_kml_file

The KML or KMZ file to translate.

File
output_folder

The destination folder for the file geodatabase and layer (.lyr) file.

Folder
output_data
(Optional)

The name of the output file geodatabase and layer file (.lyr). Defaults to the name of the input KML file.

String
include_groundoverlay
(Optional)

Include ground overlay (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.

  • GROUNDOVERLAYGround overlays are included in the output.
  • NO_GROUNDOVERLAYGround overlays are not included in the output. This is the default.
Boolean

Code Sample

KMLToLayer example 1 (Python window)

Converts a KMZ file into an FGDB 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 FileGeodatabases will then be consolidated into a single FileGeodatabase.

Note: This script does not maintain the Layer files from the KML To Layer tool.

# 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: " + 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: " + fc + " FROM: " + fgdb    
    fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc    
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])
  

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

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Yes
ArcGIS for Desktop Standard: Yes
ArcGIS for Desktop Advanced: Yes
10/24/2012