0

I am writing a code that will query two attributes, zoom to layer and output a map. The problem I am having is in some cases the user will likely only want to query one of the two attributes.

How can I code this to allow for both instances?

In some instances the user will want to select the entire tract, while in others they will want to chose a field number specific to that tract.

import arcpy, os

#################### ASSIGN VARIABLES ###########################

Tract = ("111")
CLU = ("2")

#################################################################
     
# Select a tract using the TRACNBR attribute and zoom to selected
lyr = arcpy.mapping.ListLayers(mxd, 'LandUnits', df)[0]
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION",  'TRACTNBR IN ({0}) AND CLUNBR IN ({1})'.format(Tract, CLU))
df.zoomToSelectedFeatures()
lyr.definitionQuery = 'TRACTNBR IN ({0})AND CLUNBR IN ({1})'.format(Tract, CLU)
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
arcpy.RefreshActiveView()

Any ideas how I can set up this query?

9
  • Are you receiving input from the user at the start of the script? To create the variables Tract? CLU? You could also ask them, "Will you be querying Tract, CLU, or both?" Then based on their input, you set up the query.
    – timlohnes
    Aug 11, 2021 at 16:23
  • It's just an if block to change the WHERE clause, no? Your code doesn't seem to attempt a solution, or indicate how the values will change.
    – Vince
    Aug 11, 2021 at 16:27
  • @timlohnes Im setting this up to distribute. There are a few other folks in my work who would like the whole code that this pertains to for map making. So Im trying to set it up so they just have to input the variables in the "Assign Variable" block. Aug 11, 2021 at 17:12
  • @vince Im not a coder by any stretch. I'm a conservationist who makes a lot of maps for work. Im not really sure how to set up and If block for this clause. I dont really know how to even attempt a solution. I have tried to look it up, but didnt know what to look up exactly. The values are basically numbers assigned to fields. Im using this code to all the user to select a field and output maps for conservation. Aug 11, 2021 at 17:14
  • You can make a toolbox to give to other users. They click on a tool and it asks them the questions you need. They don't have to do anything in code. The tool opens a popup that asks them What tract number are you querying (leave blank for none)? Then, What CLU are you querying (leave blank for none). Does your code work the way you want now. Looks like that is a query for both Tract and CLU? Zooms to features, only shows the desired features.
    – timlohnes
    Aug 11, 2021 at 17:20

1 Answer 1

3

Something like this. If CLU is empty (""), then only query Tract. Else, query both.

if CLU == "":
  arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION",  'TRACTNBR IN ({0}) '.format(Tract))
  df.zoomToSelectedFeatures()
  lyr.definitionQuery = 'TRACTNBR IN ({0})'.format(Tract)
else:
  arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION",  'TRACTNBR IN ({0}) AND CLUNBR IN ({1})'.format(Tract, CLU))
  df.zoomToSelectedFeatures()
  lyr.definitionQuery = 'TRACTNBR IN ({0})AND CLUNBR IN ({1})'.format(Tract, CLU)
2
  • that worked perfect! thank you. I am slowly learning and this will be banked away for future use! Aug 11, 2021 at 19:54
  • Glad to hear it!
    – timlohnes
    Aug 11, 2021 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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