robertdbuckley38

reproject all feature classes to different fgdb in Python script

Discussion created by robertdbuckley38 on May 28, 2013
Latest reply on May 28, 2013 by rafaelr
Hi,

I would like to write a script which does the following;

1. Lists all feature classes in a geodatabase

2. Checks that all the feature classes have a defined coordinate system - if not, then stop script and output message

3. If nr 3 passes, then reproject all the featureclasses into a certain coordinate system using a specific transformation method

4. All reprojected feature classes should be saved into a predefined different geodatabase

5. Output message when successful



import arcpy, os
from arcpy import env

# Set this to the input FGDB
env.workspace = r'Path_to_old_GDB.gdb'
Dir = env.workspace

env.overwriteOutput = 1

# Variables
gdb_out = r'Path_to_new_GDB.gdb'
sr = r'Path_to_new_CRS.prj' 

##################################
# Parameters for script tool

# Input FGDB
env.workspace = arcpy.GetParameterAsText(0)

# Target FGDB
gdb_out = arcpy.GetParameterAsText(1)

##################################
# List the old FDs
dslist = arcpy.ListDatasets("", "Feature")

# Add old FDs to target FGDB (FC will be projected in new FDs)
if dslist == "Null":
    pass

else:
    for ds in dslist:
        arcpy.CreateFeatureDataset_management (gdb_out, ds, sr)

        # List FCs in FDs and copy them to new FDs
        fclist = arcpy.ListFeatureClasses("","",ds)
        for fc in fclist:
            arcpy.CopyFeatures_management(fc, os.path.join(gdb_out, ds, fc))

# Finally, add/project any standalone FCs to the new FGDB
for infc in arcpy.ListFeatureClasses():
        ds = arcpy.Describe(infc)
        if ds.featureType == "Simple":

            # Determine if the input has a defined coordinate system, can't project it if it does not
            dsc = arcpy.Describe(infc)

            if dsc.spatialReference.Name == "Unknown":
                print ('Undefined Coordinate System:  ' + infc)
            else:      
                # run project tool
                arcpy.Project_management(infc, os.path.join(gdb_out, infc), sr)
        else:
            print 'No stand alone feature classes to copy'





This is what I have so far....doesn´t work as I want though.

There is no option for the transformation method and the dialogs do not work. Any help would be much appreciated. Thanks

Outcomes