gernice1

Help

Discussion created by gernice1 on Jan 6, 2012
Latest reply on Jan 6, 2012 by dkwiens
# Create Geoprocessing Object
import sys, os, arcgisscripting
import arcpy
from arcpy import env


gp = arcgisscripting.create()

#I DONT UNDERSTAND WHAT IS THE WILDCARD IN THIS EXAMPLE AND HOW CAN I CHANGE IT TO SO THAT IT CAN BE USED IN MY SCRIPT
# Usage string
usage = "<input_workspace> <projection> {data_type} {wildcard} {type} {overwrite_projection}"

# Set some global variables...
# Set wildcard for data filter. Default is * (all)
wildcard = "*"

# Set type for data filter. Default is ALL
type = "ALL"

# Set boolean for overwriting projection definition. Default is YES (true).
bOverwriteProjection = True

# Set boolean for branching into subdirectories. Default is NO (false).
bRecursive = 0


def DefineProjection(inputWorkspace):
    gp.AddMessage("Searching for data in " + inputWorkspace)
    ##print inputWorkspace
    gp.workspace = inputWorkspace
    #Loop through each featureclass in input workspace and define a projection
    if data_type == "FEATURES":
        datasets = gp.listfeatureclasses(wildcard, type)
        dataset = datasets.Next()
    else:
        #data_type is RASTERS
        datasets = gp.listrasters(wildcard, type)
        dataset = datasets.Next()
       
    while dataset:
        # If {overwrite_projection} is NO,
        # check if the feature class has a projection already define by doing a describe.
        bDefineProject = 0
        if bOverwriteProjection == 0:
            gp.AddMessage("Checking " + dataset)
            dsc = gp.Describe(dataset)
            if dsc.SpatialReference.Name == "Unknown":
                bDefineProject = 1
            else:
                print "Projection already defined and will not be changed."
                gp.AddMessage("Projection already defined and will not be changed.")
        else:
            # {overwrite_projection} is "YES"
            bDefineProject = 1

        if bDefineProject == 1:
            try:
                gp.AddMessage("Defining projection for " + inputWorkspace + "\\" + dataset)
                gp.defineprojection_management(dataset, prj)
                print "Successfully defined projection for " + inputWorkspace + "\\" + dataset
            except:
                # Print Error Messages
                print gp.getmessages(2)
               
        dataset = datasets.Next()
    if bRecursive == 1:
        if os.path.isdir(inputWorkspace):
            lstDir = os.listdir(inputWorkspace)
            for dir in lstDir:
                subDir = inputWorkspace + "\\" + dir
                if os.path.isdir(subDir):
                    ##print "Branching into " + subDir
                    DefineProjection(subDir)
      

try:
    #Check if all required parameters have been provided.
    if len(sys.argv) < 3:
        raise Exception, "Invalid number of parameters provided. \n" + "Usage: " + usage

    # Set the input workspace environment
    inputWorkspace = ("C:\\Animals")

    #Check if <input_workspace> exists
    if not gp.exists(inputWorkspace):
        gp.AddError("Workspace " + inputWorkspace + " does not exist")
        raise Exception, "<input_workspace> does not exist."
    else:
        gp.AddMessage("Input workspace: " + inputWorkspace)

       
    # Set the projection information for multiple featureclasses.
    # First set the featureclasses to WGS84 and then Cylindrical Equal Area
    prj = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
    prj = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\"Coordinate Systems\Projected Coordinate Systems\World\Cylindrical Equal Area (world).prj"

     # Call the DefineProjection routine
    DefineProjection(inputWorkspace)
   
except Exception, ErrDesc:
    gp.AddError(ErrDesc)
    print ErrDesc

Outcomes