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 feature classes.

# Import system modules
import arcpy
import os

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

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

# Create the master FileGeodatabase
arcpy.CreateFileGDB_management(out_location, gdb)

# 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, out_location)

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

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

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  
    feature_classes = arcpy.ListFeatureClasses('*', '', 'Placemarks')
    for fc in feature_classes:
        print("COPYING: {} FROM: {}".format(fc, fgdb))
        fcCopy = os.path.join(fgdb, 'Placemarks', fc)
        arcpy.FeatureClassToFeatureClass_conversion(
            fcCopy, gdb_location, fgdb[fgdb.rfind(os.sep) + 1:-4])
                    

Tags

Credits

Use limitations