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
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
.
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.
-
Is there any way to directly coy the layer or entire geodatabase to a shapefile? I'm working on a shitty laptop connected to a virtual desktop on campus so doing things in as few steps possible is ideal Dec 12, 2019 at 17:59
-
Write a small arcpy script with these two commands? I'm not at a PC right now, or I'd have written a sample. But it's not hard, see desktop.arcgis.com/en/arcmap/10.6/tools/conversion-toolbox/… and desktop.arcgis.com/en/arcmap/10.6/tools/data-management-toolbox/…– BerendDec 12, 2019 at 18:59