# Import system modules import arcpy from arcpy import env import os # Set environment settings env.workspace = "C:/Users/a391665/Documents/AAAAPERSONALE/rugareto" # Execute CreateFileGDB arcpy.CreateFileGDB_management("C:/park", "test4") # Set local variables outWorkspace = "c:/park/test4.gdb" # Use ListFeatureClasses to generate a list of shapefiles in the # workspace shown above. fcList = arcpy.ListFeatureClasses() # Execute CopyFeatures for each input shapefile for shapefile in fcList: # Determine the new output feature class path and name outFeatureClass = os.path.join(outWorkspace, shapefile.strip(".shp")) arcpy.CopyFeatures_management(shapefile, outFeatureClass)
def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): print os.path.join(workspace, fc) arcpy.FeatureClassToGeodatabase_conversion(fc, "c:\\park\\test4.gdb") #outFeatureClass = os.path.join(outWorkspace, fc.strip(".shp")) #arcpy.CopyFeatures_management(fc, outWorkspace + "\\" + fc.strip(".shp")) print os.path.join(workspace, fc, "TO GDB")
That's embarassing, I had some Python training (via webcourse) but apparently ain't enough...
This is my side project, to get new skills.
I did as suggested and it works fine for a while, then I get the following error (SHP are the ones I used to test the precious scripts):
Luca:
I believe the ESRI documentation for FeatureClassToGeodatabase_conversion answers this question
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000021000000
�?�The name of the output feature classes will be based on the name of the input feature class name. For example, if the input is C:\base\streams.shp, the output feature class will be named streams.
�?�If the name already exists in the output geodatabase, a number will be appended to the end to make it unique, for example, "_1".
Using FeatureClassToGeodatabase_conversion works properly because when it hits an input feature class with the same name it appends a _1 to the name of the second instance and so on _2 for the third instance instead of throwing an error.
Runtime error <class 'arcgisscripting.ExecuteError'>: Undefined geographic transformation.
# Import system modules import arcpy from arcpy import env import os import traceback # Set environment settings env.workspace = "C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto\\GIS" print("env.workspace completed successfully") #check GDB exist if os.path.exists("c:\\park\\test4.gdb"): arcpy.Delete_management("c:\\park\\test4.gdb") # Execute CreateFileGDB arcpy.CreateFileGDB_management("C:\\park", "test4") print("CreateFileGDB completed successfully") # Set local variables outWorkspace = "c:\\park\\test4.gdb" print("outWorkspace completed successfully") # Iterate def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): dsc = arcpy.Describe(fc) if dsc.spatialReference.name == "WGS_1984_UTM_Zone_32N": print os.path.join(workspace, fc, "prj: WGS_1984_UTM_Zone_32N") arcpy.FeatureClassToGeodatabase_conversion(fc, "c:\\park\\test4.gdb") print os.path.join(workspace, fc, "importaed in GDB") else: print os.path.join(workspace, fc, "DIFFERENT PRJ!") # Determine the new output feature class path and name #outfc = os.path.join(workspace, fc) # Set output coordinate system #prjFile = "C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\UTM\\WGS 1984\\Northern Hemisphere\\WGS 1984 UTM Zone 32N.prj" print "PRJ CREATED!" # Create a spatial reference object using a projection file outCS = arcpy.SpatialReference("C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\UTM\\WGS 1984\\Northern Hemisphere\\WGS 1984 UTM Zone 32N.prj") print "outCS CREATED!" arcpy.Project_management(fc, "c:\\park\\test4.gdb", outCS) print "PROJECTED!" for ws in arcpy.ListWorkspaces(): fcs_in_workspace(os.path.join(workspace, ws)) fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto\\GIS")
Luca:
I would first check to see if the prj file is correct. Take one of the feature classes in your file geodatabase and set it to the indicated projection within ArcMap or ArcCatalog. If this fails, it might indicate an issue with your prj file.
Also, are these feature classes already in a projection. If so, maybe you do not need to use the arcpy.SpatialReference method, but go directly to arcpy.Project_management. I have a python script where I reproject multiple feature classes in a loop which already have a projection and I just use the arcpy.Project_management method to accomplish this task.
# Load required toolboxes...
arcpy.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Data Management Tools.tbx")
# Create a spatial reference object using a projection file
outCS = arcpy.SpatialReference('WGS 1984 UTM Zone 32N')
Runtime error <type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function.
# Import system modules import arcpy from arcpy import env import os import traceback # Load required toolboxes... arcpy.AddToolbox("C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\ArcToolbox\\Toolboxes\\Data Management Tools.tbx") # Set environment settings env.workspace = "C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto\\GIS\\test" print("env.workspace completed successfully") #check GDB exist if os.path.exists("c:\\park\\test4.gdb"): arcpy.Delete_management("c:\\park\\test4.gdb") # Execute CreateFileGDB arcpy.CreateFileGDB_management("C:\\park", "test4") print("CreateFileGDB completed successfully") # Set local variables outWorkspace = "c:\\park\\test4.gdb" print("outWorkspace completed successfully") # Iterate def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): dsc = arcpy.Describe(fc) if dsc.spatialReference.name == "WGS_1984_UTM_Zone_32N": print os.path.join(workspace, fc, "prj: WGS_1984_UTM_Zone_32N") arcpy.FeatureClassToGeodatabase_conversion(fc, "c:\\park\\test4.gdb") print os.path.join(workspace, fc, "importaed in GDB") else: print os.path.join(workspace, fc, "DIFFERENT PRJ!") # Determine the new output feature class path and name outfc = os.path.join(outWorkspace, fc) # Set output coordinate system #prjFile = "C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\UTM\\WGS 1984\\Northern Hemisphere\\WGS 1984 UTM Zone 32N.prj" print "PRJ CREATED!" # Create a spatial reference object using a projection file outCS = arcpy.SpatialReference('WGS 1984 UTM Zone 32N') print "outCS CREATED!" arcpy.Project_management(fc, outfc, outCS) print "PROJECTED!" for ws in arcpy.ListWorkspaces(): fcs_in_workspace(os.path.join(workspace, ws)) fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto\\GIS\\test")