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.

1

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.

|improve this answer|||||
1

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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