Wednesday 21 February 2018

arcgis 10.1 - How to convert all shapefiles in folder into KML using ArcPy?


I was trying to convert all the shp in a folder into kml.



featureclasses = arcpy.ListFeatureClasses()


for fc in featureclasses:


 # Set Local Variables

composite = 'COMPOSITE'

pixels = 1024


dpi = 96

clamped = 'CLAMPED_TO_GROUND'

scale = 1

outKML = fc[:-4] + ".kmz"

arcpy.LayerToKML_conversion(fc,outKML, scale, composite,'', pixels, dpi, clamped)


It always says Failed to execute. Parameters are not valid. ERROR 000732: Layer: Dataset ZZZ.shp does not exist or is not supported Failed to execute (LayerToKML).


But I can manually do it within ArcMap 10.1 Desktop...



Answer



This is because the Layer to KML tool takes either LAYERS (feature layers in a map for example), or LAYER FILES (.lyr files on disk pointing at featureclasses).


If you want to run this as a script outside of ArcMap you'll have to run MakeFeatureLayer on every shapefile, turning them into a layer first and pass that onto Layer to KML.


This is starter code...you'll have to modify it to make unique names. As-is it'll overwrite each KMZ it outputs.


featureclasses = arcpy.ListFeatureClasses()
for fc in featureclasses:
arcpy.MakeFeatureLayer_management(fc, "name1")

arcpy.LayerToKML_conversion("name1", r"c:\temp\foo1.kmz")

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...