Select to view content in your preferred language

Python Script Help: Batch Describe Projection

2527
11
02-24-2011 08:00 AM
AshleyMott
Occasional Contributor
Hi all,
I got a lot of help in creating a Python scribt that allows me to describe a dataset's projection: http://forums.arcgis.com/threads/23918-Check-Projection-Coordinate-tool-or-script

Now, I'd like to do the same thing in a "batch." Basically, the ugly window that pops up when I right click on my Describe Projection script tool and hit batch won't do. It needs to be a nicer interface and more automated (just like any batch GPTool). Although, I do get the output that I want.

I'm having two problems with my batch describe script:
1. My loop does not work. I am learning Python, so basic concepts elude me. Don't laugh at my code please!
2. I am getting indent errors that I cannot seem to correct.

I also am curious as to why the tool won't automatically batch if I set the script tool's input parameter to be multivalue.

Any help is most appreciated.
Regards, Ashley

import arcpy

#Define message constants so they may be translated easily
unknown_projection = "Unknown"
# Get the feature class to describe
#
featureClass = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(featureClass)
#Loop through each dataset and describe projection.
for dataset in inDatasets:
        try:
            #Describe input dataset to check if a projection is already defined.
            dsc_Dataset = ConversionUtils.gp.Describe(dataset)
            cs_Dataset = dsc_Dataset.SpatialReference
            #Check if a projection is already define for the input dataset.
            if cs_Dataset.Name != unknown_projection:
                ConversionUtils.gp.AddWarning(msgPrjAlreadyDefine)
# Print SpatialReference object properties
SR = desc.spatialReference
print SR.name
# shows results in commandline of IDLE
print SR.exportToString()
# Show results in geoprocessing tool dialog 
arcpy.AddMessage(SR.name)
arcpy.AddMessage(SR.exportToString())
Tags (2)
0 Kudos
11 Replies
RDHarles
Regular Contributor
Hi Ashley,

Looks to me like you need to add this:
arcpy.ListFeatureClasses()

...and move the Describe inside the loop.

Also, if you have a "try:", you need an "except:"

Maybe something like this (untested):

import arcpy

#Define message constants so they may be translated easily
unknown_projection = "Unknown"

#Loop through each dataset and describe projection.
inDatasets = arcpy.ListFeatureClasses()
for dataset in inDatasets:
    try:
        # Describe each fc
        desc = arcpy.Describe(dataset)
        #Describe input dataset to check if a projection is already defined.
        dsc_Dataset = ConversionUtils.gp.Describe(dataset)
        cs_Dataset = dsc_Dataset.SpatialReference
        #Check if a projection is already define for the input dataset.
        if cs_Dataset.Name != unknown_projection:
            ConversionUtils.gp.AddWarning(msgPrjAlreadyDefine)
        # Print SpatialReference object properties
        SR = desc.spatialReference
        print SR.name
        # shows results in commandline of IDLE
        print SR.exportToString()
        # Show results in geoprocessing tool dialog 
        arcpy.AddMessage(SR.name)
        arcpy.AddMessage(SR.exportToString())

    except:
        arcpy.AddMessage("ERROR!")
        
0 Kudos
AshleyMott
Occasional Contributor
Hi R.D.,
Thanks for your help. Your changes make sense to me. Thank you for helping me get unstuck from those errors. However, I am getting an error that I haven't seen yet:

<type 'exceptions.RuntimeError'>: ERROR 000638: Workspace not set for GeoProcessor.
Failed to execute (BatchDescribeProjection).


I am not getting a line #, so I am not sure where to start. I have pulled shapefiles/feature classes from many different folders and network drives and I get the same error. I also searched for the error in the support pages, with no luck.

Have you seen this error before?
0 Kudos
RDHarles
Regular Contributor
Oh, you probably just need to set your workspace to where your data is, something similar to this:

arcpy.env.workspace = "c:/mystuff/project"

...put this in the beginning of the script, right below "import arcpy"
0 Kudos
AshleyMott
Occasional Contributor
Thanks again R.D.!

Do you always have to set a workspace or can script tools run data from anywhere on your network?

Unfortunately, I think the loop is giving me an error now. I am going to watch some Python training this morning. Maybe that will help alleviate my ignorance! 🙂

I really appreciate your time R.D.

Ashley

This is what the code looks like now:
import arcpy
arcpy.env.workspace = "T:\WORK FOLDER"

#Define message constants so they may be translated easily
unknown_projection = "Unknown"

#Loop through each dataset and describe projection.
inDatasets = arcpy.ListFeatureClasses()
for dataset in inDatasets:
    try:
        # Describe each fc
        desc = arcpy.Describe(dataset)
        #Describe input dataset to check if a projection is already defined.
        dsc_Dataset = ConversionUtils.gp.Describe(dataset)
        cs_Dataset = dsc_Dataset.SpatialReference
        #Check if a projection is already define for the input dataset.
        if cs_Dataset.Name != unknown_projection:
            ConversionUtils.gp.AddWarning(msgPrjAlreadyDefine)
        # Print SpatialReference object properties
        SR = desc.spatialReference
        print SR.name
        # shows results in commandline of IDLE
        print SR.exportToString()
        # Show results in geoprocessing tool dialog 
        arcpy.AddMessage(SR.name)
        arcpy.AddMessage(SR.exportToString())

    except:
        arcpy.AddMessage("ERROR!")


Here is the error:
Executing: BatchDescribeProjection 'T:\WORK FOLDER\DEN_MP_DIA.shp'
Start Time: Fri Feb 25 08:15:38 2011
Running script BatchDescribeProjection...
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
Completed script BatchDescribeProjection...
Succeeded at Fri Feb 25 08:15:39 2011 (Elapsed Time: 1.00 seconds)
0 Kudos
RDHarles
Regular Contributor
There's 3 ways to do slashes in python, the way you did it (backslash) is not one of them.

One of these will work:

"T:/WORK FOLDER"
"T:\\WORK FOLDER"
r"T:\WORK FOLDER"
0 Kudos
RDHarles
Regular Contributor

Do you always have to set a workspace?
In general, yes.

There are exceptions depending "how" you are running your script, and what tools your using (at least in 9.x.)  Not positive about 10.x.


or can script tools run data from anywhere on your network?
You can run script tools from anywhere on your network but you'll have to change the path (workspace) in your script to where your data is.
0 Kudos
AshleyMott
Occasional Contributor
Changed the code to"
arcpy.env.workspace = "T:\\WORK FOLDER"

But, still getting error:
Executing: BatchDescribeProjection 'T:\WORK FOLDER\DEN_MP_DIA.shp';'T:\WORK FOLDER\dirt_spcs.shp'
Start Time: Fri Feb 25 14:38:27 2011
Running script BatchDescribeProjection...
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
ERROR!
Completed script BatchDescribeProjection...
Succeeded at Fri Feb 25 14:38:29 2011 (Elapsed Time: 2.00 seconds)


Then I tried to back to my original code (without batching) and put it in the loop:
import arcpy

#Loop through each dataset and describe projection.
fcs = arcpy.GetParameterAsText(0)
for fc in fcs:
    try:
        desc = arcpy.Describe(fc)
        # Print some SpatialReference object properties
        SR = desc.spatialReference
        print SR.name
        # shows results in commandline of IDLE
        print SR.exportToString()
        # Show results in geoprocessing tool dialog 
        arcpy.AddMessage(SR.name)
        arcpy.AddMessage(SR.exportToString())
    except:
        arcpy.AddMessage("ERROR!")


I got the same error. 😞

I looked at a bunch of loop code in the Resource Center. It looks right. I am missing something else.
0 Kudos
AshleyMott
Occasional Contributor
In general, yes.

There are exceptions depending "how" you are running your script, and what tools your using (at least in 9.x.)  Not positive about 10.x.

You can run script tools from anywhere on your network but you'll have to change the path (workspace) in your script to where your data is.


Does this let you grab inputs from anywhere on your network?
arcpy.env.workspace = arcpy.GetParameterAsText(0)
0 Kudos
RDHarles
Regular Contributor
OK, I took the try/except out.  That way you'll get an error that can help you vs. just the word "ERROR".
I also changed a couple other things that caused errors (I don't know what "ConversionUtils" is?).
I put 3 shapefiles in "T:/WORK FOLDER" on my computer and this code ran. Try this code  and see what happens.
R.D.

import arcpy
arcpy.env.workspace = "T:/WORK FOLDER"

#Loop through each dataset and describe projection.
inDatasets = arcpy.ListFeatureClasses()
for dataset in inDatasets:    
    # Describe each fc
    desc = arcpy.Describe(dataset)
    #Describe input dataset to check if a projection is already defined.
    dsc_Dataset = arcpy.Describe(dataset)
    cs_Dataset = dsc_Dataset.SpatialReference
    #Check if a projection is already define for the input dataset.
    if cs_Dataset.Name != "Unknown":
        arcpy.AddWarning("msgPrjAlreadyDefine")
    # Print SpatialReference object properties
    SR = desc.spatialReference
    print SR.name
    # shows results in commandline of IDLE
    print SR.exportToString()
    # Show results in geoprocessing tool dialog 
    arcpy.AddMessage(SR.name)
    arcpy.AddMessage(SR.exportToString())

print "\nDone.\n"
0 Kudos