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.
Usage
This tool creates a file geodatabase containing a feature class within a feature dataset. The feature class name will be named point, line, polygon, or multipatches, dependent on the original features of the KML file. At the same folder level as the file geodatabase will be a layer file which can be added to a map to draw the features. This layer file draws features based on their schema of point, line, or polygon, while maintaining the original KML symbology.
Each feature class created will have attributes which maintain information about the original KML file. The original folder structure, name, and pop-up information, as well as fields that help define how the features sit on a surface, all make up the attributes of each feature.
Rasters, or ground overlays, will be converted into a raster catalog inside the file geodatabase. The source raster in its native format is available in a folder of GroundOverlays at the same level as the output file geodatabase.
Output will be generated in the WGS84 coordinate system. The output features can be reprojected to another coordinate system, if desired, using the Project tool.
Input up to KMZ version 2.2 of the OGC KML standard is mostly supported. Point locations that use the address tag (by way of geocoding) are not supported. A valid latitude and longitude location is required inside the source KML.
Syntax
KMLToLayer_conversion (in_kml_file, output_folder, {output_data}, {include_groundoverlay})
Parameter | Explanation | Data 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. The default is 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.
| 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: {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
Environments
Licensing information
- ArcGIS for Desktop Basic: Yes
- ArcGIS for Desktop Standard: Yes
- ArcGIS for Desktop Advanced: Yes