reproject all feature classes to different fgdb in Python script

2398
1
05-28-2013 05:08 AM
RobertBuckley
New Contributor III
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
Tags (2)
0 Kudos
1 Reply
RaphaelR
Occasional Contributor II
hi Robert,

as described in the help topic, the transform method is an optional parameter(String), which you´ll need to specify.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000007m000000
for example:
in_FC = path to in_FC
out_FC = path to out_FC
install_dir = arcpy.GetInstallInfo()['InstallDir']
outCS = os.path.join(install_dir, r"Coordinate Systems\Projected Coordinate Systems\UTM\WGS 1984\Northern Hemisphere\WGS 1984 UTM Zone 33N.prj")
transform_method = "MGI_To_WGS_1984_3"

arcpy.Project_management(in_FC, out_FC, outCS, transform_method)
0 Kudos