I am trying to batch process KML polygons from a folder into a temp geodatabase.
From there, Iif this works and runs then I am trying to Append the polygons to result to a Server connection (Postgres withe append layer). Going to use here the ModelBuilder when the script runs.
But I am stuck on a error
Traceback (most recent call last):
File "C:\Users\Bjorn.tenBroeke\Documents\Bjorn\KML.py", line 20, in
for kml in arcpy.ListFiles('.KM'):
TypeError: 'NoneType' object is not iterable
Failed to execute (Script).
# 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 featureclases
# Import system models
import arcpy, os
# Set workspace (where all the KMLs are)
arcpy.env.workspace= (r"C:\DATA\KML")
# Set local variables and location for the consolidated file geodatabase
outLocation = "C:/WorkingData/fGDBs"
MasterGDB = 'AllKMLLayers.gdb'
MasterGDBLocation = os.path.join(outLocation, MasterGDB)
# Create the master FileGeodatabase
arcpy.CreateFileGDB_management(outLocation, MasterGDB)
# Convert all KMZ and KML files found in the current workspace
for kml in arcpy.ListFiles('*.KM*'):
print "CONVERTING: " + os.path.join(arcpy.env.workspace,kml)
arcpy.KMLToLayer_conversion(kml, outLocation)
# Change the workspace to fGDB location
arcpy.env.workspace = outLocation
# Loop through all the FileGeodatabases within the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Skip the Master GDB
wks.remove(MasterGDBLocation)
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
featureClasses = arcpy.ListFeatureClasses('*', '', 'Placemarks')
for fc in featureClasses:
print "COPYING: " + fc + " FROM: " + fgdb
fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc
arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])
# Clean up
del kmz, wks, fc, featureClasses, fgdb
Best Answer
The error means the workspace folder/directory does not exist (r"C:\DATA\KML"). Check to make sure the "KML" folder/directory exists.