Wednesday 19 June 2019

arcpy - Python Script to convert a mass of KMLs to a single fGDB


I have a mass of KML files, approx 350+ that I need in a single fGDB. I lifted a string of code from ESRI help to attempt to do this - I got it working to the point of creating all the individual gGDBs (one per KML) but its failing with an invalid character in a variable somewhere. Below is the code, and then the error that it throws out. I've added some print commands to show whats in the variables right before it fails. Help? Also, this is my first time trying to use python for actual work, so I apologize in advance for n00bish mistakes.


# 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="C:\\Users\\mperez\\Desktop\\IT-Paths"

# Set local variables and location for the consolidated file geodatabase
outLocation = "V:\\2155\\active\\215510456\\GDB"

MasterGDB = r'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 kmz in arcpy.ListFiles('*.KM*'):
print "CONVERTING: " + os.path.join(arcpy.env.workspace,kmz)
arcpy.KMLToLayer_conversion(kmz, 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 r"COPYING: " + fc + r" FROM: " + fgdb
fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc

print r"fcCopy = " + fcCopy
print r"MasterGDBLocation = " + MasterGDBLocation
print r"fgdb = " + fgdb
arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])


# Clean up
del kmz, wks, fc, featureClasses, fgdb

Output from shell showing error:



COPYING: Points FROM: V:\2155\active\215510456\GDB\canal-move-2-golden-gate-blvd.gdb
fcCopy = V:\2155\active\215510456\GDB\canal-move-2-golden-gate-blvd.gdb\Placemarks\Points
MasterGDBLocation = V:\2155\active\215510456\GDB\AllKMLLayers.gdb
fgdb = V:\2155\active\215510456\GDB\canal-move-2-golden-gate-blvd.gdb

Traceback (most recent call last):
File "C:\Users\mperez\Desktop\batch_kml_to_gdb.py", line 46, in
arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\conversion.py", line 1547, in FeatureClassToFeatureClass
raise e

ExecuteError: ERROR 000354: The name contains invalid characters
Failed to execute (FeatureClassToFeatureClass).


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...