SelectByAttribute Problem

529
11
08-10-2017 08:13 AM
JonathanUrshan1
New Contributor

Hi, I am trying to write a script that selects a parcel number and writes it to a PDF. I am having trouble with the SelectLayerByAttribute tool.  I am writing the script in PyCharm not in the Python Window of ArcMap. This is what I have gathered from various sources online.  I am pretty sure its an issue with my syntax. Any help would be much appreciated.  This is what I have so far.

import arcpy

mxd = arcpy.mapping.MapDocument(r"//bondserv2/USERS-COUNTY/ahuber/Desktop/Bond.mxd")

parcel = "Parcel_Point"

df = arcpy.mapping.ListDataFrames(mxd, parcel)

layer = arcpy.mapping.ListLayers(mxd, "", df)

arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[PIN] = '021101104001'")

I run the SelectLayerByAttribute tool in the python window just like the above code and it works. But trying to get it working in the PyCharm IDE is being a real pain. I'm new to python and arcpy, this is my first attempt at automating some one my everyday tasks.

0 Kudos
11 Replies
XanderBakker
Esri Esteemed Contributor

In your case df is a list of dataframes. Add "[0]" at the end of the line to select the first dataframe from the list. And is "Parcel_point" the name of the dataframe? because that is what you are using in this case.

Same for "layer" which is a list of layers (probably with only one layer in it). Add "[0]" at the end of the command to select the first item of the list

Maybe try this:

import arcpy
mxd = arcpy.mapping.MapDocument(r"//bondserv2/USERS-COUNTY/ahuber/Desktop/Bond.mxd")
parcel = "Parcel_Point"

df = arcpy.mapping.ListDataFrames(mxd, parcel)[0]
layer = arcpy.mapping.ListLayers(mxd, parcel, df)[0]

arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[PIN] = '021101104001'")
JonathanUrshan1
New Contributor

the variable parcel (Parcel_Point) is the layer that I am targeting, and the "021101104001" is the value I am trying to select with the field name PIN. Parcel_Point is one of the shapefiles. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Did you try the code that I posted? You are running the code as a standalone script, so to see the selection, you would have to save the mxd.

0 Kudos
JonathanUrshan1
New Contributor

Yeah saved the mxd and ran the code. I got this error.

Traceback (most recent call last):
   File "C:/Users/ahuber/PycharmProjects/PrintToPDF/Print_to_PDF.py", line 7, in <module>
      df = arcpy.mapping.ListDataFrames(mxd, parcel)[0]
IndexError: list index out of range

0 Kudos
XanderBakker
Esri Esteemed Contributor

my bad... should have removed the "parcel" from the parameters...

Change line 5 to:

df = arcpy.mapping.ListDataFrames(mxd)[0]

JonathanUrshan1
New Contributor

Well, Good news is that the code runs and I don't get an error message. The bad news is that it still doesn't select the feature. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Are you saving the MXD? If you run the code as standalone script you will not see the result in for instance a session where you have the MXD open. 

To test the effect you could open the MXD, run the code (change the path to the MXD by "CURRENT") and see if anything gets selected. 

After selecting features what will be your next step?

0 Kudos
JonathanUrshan1
New Contributor

Changing the file path to "CURRENT" gives me the error that it can't find the map document when I run the code. I also added this script to my toolbox and it ran fine but didn't select the area either.  I work for the county assessor as their GIS Tech.  So I am trying to write a script that zooms to a parcel then exports the map to a pdf and prints it off. I have the export and print portion done. Just not the zoom to feature part.

Also thanks for being so patient with me. This is highly appreciated. I've been wanting to learn Arcpy for awhile. I already know basic Python but Arcpy is new to me.

0 Kudos
XanderBakker
Esri Esteemed Contributor

The "CURRENT" keyword is only available in the Python window. I suggested to run the script there to verify if a selection was made inside a session in the "Bond.mxd". 

So I am right that the goal is to have a tool or toolbar button that allows an end user to select a parcel and print and export a map? Will the user be working inside the Bond.mxd or should this work inside any ArcMap Session? 

If the selection does not work, you actually don't need a selection to zoom to a feature. If the end user will provide the "PIN" you could use that like this:

import arcpy
fc = r'\\folder\path to your\workspace.gdb\featureclass'  # replace with path to parcel featureclass
pin = '021101104001'

where = "[PIN] = '{0}'".format(pin)
fld = 'SHAPE@'

lst = [r[0] for r in arcpy.da.SearchCursor(fc, flds, where)]
if len(lst) > 0:
    feature = lst[0] # take first feature
    extent = feature.extent
    # zoom to extent using:
    # df.extent = extent