7

I have been working on a Python toolbox which contains 6 parameters. I am a little lost on all of the different data types available in ArcMap (it seems like there's hundreds of them!). However, one of the input parameters has a file extension of .shp (shapefile). I have always used the GPFeatureLayer data type and it has always worked for everything else I hope to do with my tool, although I don't completely understand why. I have only been doing my testing with point shapefiles for the time being.

What I would ultimately hope to do is check if the user has entered a shapefile that is not a point (or a collection of points). If it is not a point, throw a warning to the user, but still allow them to use the tool. From what I understand, you can check the shape type by utilizing the Describe() class. However, when I attempt to pass my parameter into Describe (in order to return a Describe object), ArcMap throws an error:

enter image description here

Here is how I define the parameter I am focusing on:

def getParameterInfo(self):
    """Define parameter definitions"""

    # First parameter = layer with customized features
    param0 = arcpy.Parameter(
        displayName = "Source layer with customized features",
        name = "source",
        datatype = "GPFeatureLayer",
        parameterType = "Required",
        direction = "Input")

And I was hoping to throw the warning in the updateParameters() method:

def updateParameters(self, parameters):
    if parameters[2].value == True:
        parameters[3].enabled = True
        parameters[4].enabled = True
        parameters[5].enabled = True

    else:       
        parameters[3].enabled = False
        parameters[4].enabled = False
        parameters[5].enabled = False       

    describe = arcpy.Describe(parameters[0])
    if describe.shapeType == 'Point':
        arcpy.AddWarning('Warning: This is a point shapefile')

Any ideas as to why I am getting this error?

Am I using the correct data type?

I have tried using other data types for this parameter, such as DEFeatureClass, GPPoint, and DEShapefile, but I received the following error if I tried adding my shapefile in as my parameter:

enter image description here

The error was the same regardless of which of the three I used.


My issue with the Describe() class has been cleared up by adding the .value extension in my updateParameters() method:

def updateParameters(self, parameters):
    describe = arcpy.Describe(parameters[0].value)

However, now when I attempt to use the setWarningMessage() function, I get no response from ArcMap. My updateParameters() consists of the following:

def updateParameters(self, parameters):
    describe = arcpy.Describe(parameters[0].value)
    #check = str(describe.shapeType)
    if describe.shapeType == 'Point':
        parameters[0].setWarningMessage('This is a point feature class')
    else:
        parameters[0].clearMessage()    

    if parameters[2].value == True:
        parameters[3].enabled = True
        parameters[4].enabled = True
        parameters[5].enabled = True

    else:       
        parameters[3].enabled = False
        parameters[4].enabled = False
        parameters[5].enabled = False       

    return

When I add a shapefile that I know in fact is a point, the tool does not output a warning. It simply accepts it without any feedback to the user.

Does it matter if the Describe.shapeType function has an output of Unicode?

I tested this through the ArcMap Python window with the following sequence of commands:

enter image description here

However, even if I try to convert the Describe.shapeType to a string, I still don't get a warning message. I've done the following tests:

  1. Rather than checking for: if describe.shapeType == 'Point', I tried to check for: if describe.shapeType == u'Point', which would force it to check for a Unicode data type.
  2. Converting the shapeType into a string via str(decribe.shapeType).
  3. Using the encode method to change the Unicode into a sequence of ASCII characters: describe.shapeType.encode('ascii', 'ignore').

Hence, I've hit a wall once again.

4
  • 2
    I don't think you use AddWarning() there. You should probably use setErrorMessage() - check this link out: resources.arcgis.com/en/help/main/10.1/index.html#//…
    – ianbroad
    Commented Oct 9, 2014 at 22:40
  • Regarding the confusion over parameter data types.. Where are you dragging and dropping the shapefile from? If your Data Type is DEShapefile, dragging and dropping the shapefile from ArcCatalog or browsing to the shapefile should definitely work. If you have added a shapefile to ArcMap and are dragging and dropping it from the ArcMap TOC - that will not work. It is now a feature layer and would require the GPFeatureLayer Data Type to be accepted as a parameter.
    – XIY
    Commented Oct 10, 2014 at 3:37
  • I am simply dragging and dropping the shapefiles from Windows Explorer directly into the ArcMap TOC. Does it matter if I drag and drop through Windows Explorer or ArcCatalog? Will it still convert the shapefile into a Feature layer once it is in the TOC?
    – coolDude
    Commented Oct 10, 2014 at 17:19
  • I would use GPLayer, because that will accept shapefiles, geodatabase feature classes and layers.
    – nmpeterson
    Commented Oct 10, 2014 at 17:36

2 Answers 2

6

I think it may be as simple as changing this line:

describe = arcpy.Describe(parameters[0].value)

or maybe

describe = arcpy.Describe(parameters[0].valueAsText)
3
  • Yes, I often forget to add the '.value'
    – Fezter
    Commented Oct 9, 2014 at 22:34
  • Same here, although I have never done this in a .pyt, just in normal script tools. I assume it behaves the same though.
    – crmackey
    Commented Oct 9, 2014 at 22:35
  • Very good point! I have this in other areas of my code and did not think of this. Thank you!
    – coolDude
    Commented Oct 10, 2014 at 16:57
3

In his comment, @ian is correct about AddWarning() being useless outside of a tool's execute() method. I think the method you do want is setWarningMessage(). Try this:

describe = arcpy.Describe(parameters[0].value)
if describe.shapeType in ('Point', 'Multipoint'):  # catch point & multipoint
    parameters[0].setWarningMessage('This is a point feature class')
else:
    parameters[0].clearMessage()
5
  • You and @ian, you are both correct about the setWarningMessage() function. However, it looks like I'm now having issues getting this part to work. . . I'll update my question with my latest code and a screenshot. I appreciate your help!
    – coolDude
    Commented Oct 10, 2014 at 16:59
  • Is the enabling/disabling of parameters working as expected?
    – nmpeterson
    Commented Oct 10, 2014 at 17:51
  • Yes it is. I even tried changing the order of the code so that the enabling would occur before the warning message, but it made no impact.
    – coolDude
    Commented Oct 10, 2014 at 17:52
  • 1
    Put my code in the updateMessages() method instead of updateParameters() and see if that fixes it.
    – nmpeterson
    Commented Oct 10, 2014 at 17:57
  • Okay, now it works. Thank you everyone for all of your help!
    – coolDude
    Commented Oct 10, 2014 at 18:43

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.