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. Ground overlays are not converted by default. Use the Include Ground Overlay option to create rasters.
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
arcpy.conversion.KMLToLayer(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 |
Derived Output
Name | Explanation | Data Type |
output_layer | The output layer file. | Group Layer |
out_geodatabase | The output geodatabase containing a feature class within a feature dataset. | Workspace |
Code sample
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])
Environments
Licensing information
- Basic: Yes
- Standard: Yes
- Advanced: Yes