Sunday, 1 November 2015

arcpy - Determining if Layer is Point using Python Toolbox?


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.



Answer



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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...