Help

411
3
01-06-2012 09:03 AM
GerniceMuhamed
New Contributor
# 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
Tags (2)
0 Kudos
3 Replies
DarrenWiens2
MVP Honored Contributor
From the example on this page:

"The {wildcard} can be used to filter the list of feature classes or rasters.
Combination of * and characters that will help limit the results.
The asterisk (*) is the same as saying ALL. If no wildcard is specified then all
feature classes or rasters, in the workspace, will be returned.
0 Kudos
GerniceMuhamed
New Contributor
Ok thanks. I removed all the wildcards but now its telling me Invalid number of parameters provided.
Usage: <input_workspace> <projection> {data_type} {type} {overwrite_projection}..I am trying to reproject the data I have in the animal folder...Can you assist me?
0 Kudos
DarrenWiens2
MVP Honored Contributor
Honestly, your example is unnecessarily complicated. All you need is a list of your feature classes (gp.listfeatureclasses), loop through them one by one, and use either Define Projection (if you're assigning a projection to the feature classes for the first time) or Project (if you're changing projections from one projection to another). You could also use Batch Project.
0 Kudos