0

How does one convert a .kmz or .kml to a shapefile using arcpy? tried the KMZ to Layer but it outputs a .lyr and a .gdb containing one feature class. I need a shapefile, or a way to convert the contents of the .gdb to a shapefile.

2 Answers 2

3

A KMZ/KML can store multiple geometrie-types. A shapefile just one. I guess that is the reason for the FGDB output (multiple Feature Classes). So the FGDB can theoretically contain a point, line and polygon feature class. With arcpy.ListFeatureClasses and arcpy.FeatureClassToShapefile_conversion a conversion is possible.

import arcpy 
import os

def kmz2shapes(kmz, out_folder, temp_data_name):
    temp_gdb = os.path.join(out_folder, '{}.gdb'.format(temp_data_name))
    arcpy.env.workspace = temp_gdb

    arcpy.KMLToLayer_conversion(
        in_kml_file=kmz, 
        output_folder=out_folder,
        output_data=temp_data_name)

    fcs = [fc for fc in arcpy.ListFeatureClasses(feature_dataset='Placemarks')]
    arcpy.FeatureClassToShapefile_conversion(fcs, out_folder)


kmz2shapes("C:/path/to/data_test.kmz", "C:/path/to/kmz2shape", 'temp_data')

The function just converts the data to shapefiles and does not delete temporary data like the filegeodatabase and the lyr-files. There will be an error when executing the function twice with the same value for temp_data_name.

1
  • This was very helpful, thank you!
    – jacktrader
    Aug 17, 2021 at 16:45
2

The KML To Layer tool will always output a File Geodatabase. I don't know why that is, but the documentation for the Output Location says (emphasis mine):

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

If you need a shapefile, you will need KML To Layer first, and then Copy Features (in the Data Management tools, under Features.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.