ArcPy script in toolbox - what do I do about optional parameters?

2015
3
Jump to solution
08-20-2012 11:42 PM
RosieBell
New Contributor
I made my first Python tool this week, a Search by Attribute tool to search our cadastre. When I have required parameters it works beautifully. As soon as I make the parameters optional, it doesn't return any results (even if I complete all the fields).

What am I doing wrong? I added the 'if' line after reading another thread about optional parameters, but it doesn't make any difference with this problem. Thanks.


#define user parameter for road name, and locality  SelCondition = arcpy.GetParameterAsText(0) if (not SelCondition) or (SelCondition == "#") or (len(SelCondition.strip()) == 0):     SelCondition = ""   LocalityName = arcpy.GetParameterAsText(1) if (not LocalityName) or (LocalityName == "#") or (len(LocalityName.strip()) == 0):     LocalityName = ""   # select cadastral lots on road name and locality, and lot no arcpy.SelectLayerByAttribute_management("Cadastre","NEW_SELECTION", "\"ROAD_NAME\" = "+SelCondition)  arcpy.SelectLayerByAttribute_management("Cadastre","SUBSET_SELECTION", "\"LOCALITY\" = "+LocalityName) 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JochenManegold
Esri Contributor
I think the code is fine. I tested it, and for me it worked:

import arcpy value = arcpy.GetParameterAsText(0) if value == "#" or value == "":    value = None if value:   arcpy.SelectLayerByAttribute_management("dachflaechen","NEW_SELECTION", "\"OBJECTID\" = "+value) else:   arcpy.AddError("no value")

View solution in original post

0 Kudos
3 Replies
JochenManegold
Esri Contributor
I think the code is fine. I tested it, and for me it worked:

import arcpy value = arcpy.GetParameterAsText(0) if value == "#" or value == "":    value = None if value:   arcpy.SelectLayerByAttribute_management("dachflaechen","NEW_SELECTION", "\"OBJECTID\" = "+value) else:   arcpy.AddError("no value")
0 Kudos
KenCarrier
Occasional Contributor III
Try this

#define user parameter for road name, and locality

SelCondition = arcpy.GetParameterAsText(0)
if (not SelCondition) or (SelCondition == "#") or (len(SelCondition.strip()) == 0):     SelCondition = ""

LocalityName = arcpy.GetParameterAsText(1)
if (not LocalityName) or (LocalityName == "#") or (len(LocalityName.strip()) == 0):     LocalityName = ""

# select cadastral lots on road name and locality, and lot no
roadQuery = " UPPER(\"{0}\") = UPPER('{1}') ".format(ROAD_NAME, string.upper(SelCondition))
arcpy.SelectLayerByAttribute_management("Cadastre","NEW_SELECTION", roadQuery)

localityQuery = " UPPER(\"{0}\") = UPPER('{1}') ".format(LOCALITY, string.upper(LocalityName))
arcpy.SelectLayerByAttribute_management("Cadastre","NEW_SELECTION", localityQuery)
0 Kudos
RosieBell
New Contributor
Sorry for the slow reply. I tried both solutions shortly after they were posted, but couldn't get either of them to work. I tried again jm's solution again today and it did the job, many thanks. carrierkh, no doubt it was my inexperience that was the problem, so thankyou for your help.
0 Kudos