6

I have a python script in ArcGIS 10.1 which takes in a few inputs from the user. One of the inputs is a coordinate system. I would like to test whether the coordinate system the user chose was projected or geographic.

A sample of the code is:

input1 = arcpy.GetParameterAsText(0)
input2 = arcpy.GetParameterAsText(1)
coordsys = arcpy.GetParameterAsText(2)

desc1 = arcpy.Describe(input1)
desc2 = arcpy.Describe(input2)
descCoordSys = arcpy.Describe(coordsys)

The code runs fine until the last line.

I have forced the user to enter a coordinate system in the third line: enter image description here

What am I missing here?


EDIT:

I've tried to implement the changes proposed by @dmahr but it still doesn't work. It isn't getting to the nested try statement. It just quits with the error "There was an error with an input file. Please run again." I'm not sure what I'm doing wrong. I've tried with both projected and geographic coordinate systems.

input1 = arcpy.GetParameterAsText(0)
input2 = arcpy.GetParameterAsText(1)
coordsys = arcpy.GetParameter(2)

try:
    desc1 = arcpy.Describe(input1)
    desc2 = arcpy.Describe(input2)

    try:
        coordsys_linearunit = coordsys.linearUnitName
    except:
        arcpy.AddError("Input coordinate system is not projected.")
        sys.exit("Exiting.")

except:
    arcpy.AddError("There was an error with an input file.  Please run again.")
    sys.exit("Exiting.")
2
  • 4
    Try changing the type of the 3rd parameter from "Coordinate System" to "Spatial Reference". That got it working for me in a script tool. However in a standalone script GetParameter would always return a string, not a SpatialReference object.
    – blah238
    Commented Nov 13, 2012 at 1:46
  • 1
    @blah238 That worked. Thanks. I would never have thought to check there. If you post that as an answer, I'll accept it.
    – Fezter
    Commented Nov 13, 2012 at 2:12

2 Answers 2

6

Two possible solutions I've found in playing around with the SpatialReference class:

  1. In your script tool parameter's dialog, change the data type of the Coordinate System parameter to Spatial Reference. A spatial reference contains a bit more information than a coordinate system and is what is expected by GetParameter if you're expecting it to create a SpatialReference object.

    • Note: In my testing this only works when run as a script tool, not as a standalone Python script, presumably because GetParameter requires the script tool parameter definitions to infer what type of object to create from the input string.
  2. In addition to the above change, you can use the SpatialReference.loadFromString() method to create a spatial reference object from its string representation explicitly. This should work both within a script tool and a standalone Python script. E.g.:

    import arcpy
    input1 = arcpy.GetParameterAsText(0)
    input2 = arcpy.GetParameterAsText(1)
    srtxt = arcpy.GetParameterAsText(2)
    sr = arcpy.SpatialReference()
    sr.loadFromString(srtxt)
    if not sr.type == "Projected":
        raise RuntimeError("Input coordinate system is not projected.")
    

    You'll note I also changed the coordinate system type checking to be more explicit and changed the error handling logic to raise an error which is a bit more Pythonic, though you are free to handle it how you want.

5

The arcpy.GetParameterAsText function gets the parameter and converts it into a string object. You want to use the arcpy.GetParameter function instead.

Also, I believe that you don't have to use arcpy.Describe on a coordinate system parameter. Instead, an arcpy.SpatialReference object is returned. So you could use the following code:

input1 = arcpy.GetParameterAsText(0)
input2 = arcpy.GetParameterAsText(1)
coordsys = arcpy.GetParameter(2)

desc1 = arcpy.Describe(input1)
desc2 = arcpy.Describe(input2)

try:
    coordsyslinearunit = coordsys.linearUnitName
except:
    arcpy.AddError("Input coordinate system is not projected")

You could also integrate this checking process into the tool's validation methods, so that the user couldn't even run the tool without entering a projected coordinate system.

4
  • Your code makes perfect sense to me but I'm still having problems. I've edited my post to reflect my issue. I like the suggestion on editing the tool's validation methods. I'm not sure how to do that yet but I'll look into it when I get a chance. In the mean time, I'd like to get the original code to work first.
    – Fezter
    Commented Nov 12, 2012 at 23:28
  • addError should be AddError FYI
    – blah238
    Commented Nov 13, 2012 at 1:51
  • @blah238 fixed.
    – Fezter
    Commented Nov 13, 2012 at 2:13
  • Oops, sorry about the case error. Thanks for the fix @blah238
    – dmahr
    Commented Nov 13, 2012 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.