Select to view content in your preferred language

Likely Simple Python Question: dynamically selecting records

3023
16
Jump to solution
12-12-2013 12:05 PM
KatieGaut1
Regular Contributor
Hello,

    I have a question that is likely very easily solved with some Python coding....I just have no experience in the area.  It was suggested in the General Forum that I post my question here. 

Is there a way to dynamically select records based on a field set in Data Driven Pages? I have a graph in my layout that I want to update with each scroll with each "Next Page".  I have a table set up with a page definition query for the data I want graphed. Unfortunately, the graph still plots all records, not just the ones displayed with the page definition query. So, for example, even though (with the page definition query) my table displays records only from Station_ID = 101, the graphs displays records from all Station_IDs. The graph only works properly (displaying only Station_ID = 101 items) if they are manually selected.

Is there anyway to automate this? I'm guessing there's an easy Python code I could run to manage this....

Thanks in advance and please ask questions if I haven't made myself clear!

Katie
Tags (2)
0 Kudos
16 Replies
PSArcOnlinePSArcOnline
Deactivated User
Thank you Wayne,
this could be what i am searching for. But i had to try first. Sadly i haven't much time left this jear. But i will answer if it worked as soon as i implement it.

I have a quick question. For selecting the features for my graph i use
arcpy.SelectLayerByAttribute_management ("LayerWithGraphInfo","NEW_SELECTION","IndexID<>0")


Isn't there a "Select All" argument? "IndexID<>0" works because in the attribute table there is no IndexID value 0.

You use
arcpy.SelectLayerByLocation_management(lyr, '', '', '', 'SWITCH_SELECTION')


Why "LayerByLocation" and why "SWITCH_SELECTION"? Is this faster or less prone to errors?
0 Kudos
T__WayneWhitley
Honored Contributor
I just like the SWITCH_SELECTION option because it doesn't rely on a field (so the field to query doesn't have to exist).  You could implement it with your query if you wish, IndexID<>0.

I don't think there is a 'select all' option, not directly; however (and I tested this), the SWITCH_SELECTION is essentially a 'select all' function provided there is no initial selection.  The only time the query I used would ever fail is upon initialization (which I mentioned in the previous post), or if there are overlapping value selections to be made (SWITCH_SELECTION would not keep any currently selected features selected).  But I think that's covered too, in this case, because a page definition query is applied, and that depends on uniquely coded attributes.  (In Katie's case it is, but now that I think about, there may be exceptions...and you'd want to adjust your method of selection accordingly.)

I didn't test the performance - likely select by location in this case is faster since the actual spatial selection params are skipped.  I have to say it seems a little odd this was built into the sel by loc instead of sel by att.  But that's how it works.

Wayne
0 Kudos
PSArcOnlinePSArcOnline
Deactivated User
Again, thanks for your quick answer. I will try to change this.

For the moment my questions have been answered. At least for this year. 🙂
0 Kudos
KatieGaut1
Regular Contributor
Hi Wayne,

   Again - I so appreciate your persistence!  I've spent a few hours looking through the tutorials, help pages and links you've posted.  I've created the add-in extension from your code, using my file name for the 'tempsStation' and 'created' the add-in with no errors.  I then enable the extension in ArcMap and try to load the add-in but get an error of, "No GUI components found in this Add-In."  Argh! 
    Any thoughts?  At least you're helping out other folks along the way it sounds like.  Thanks so much for your time and effort.  It's very much appreciated!

Thanks,
Katie
0 Kudos
T__WayneWhitley
Honored Contributor
If you can open your mxd and go to Customize (on the main menu) and click Extensions and in the pop-up window your extension (whatever you named it) is listed, that means it is already loaded.  Clicking the checkmark on means you are enabling it (yes do that).  You should be ready to test it.

The way I wrote the code, it does nothing on loading the map - so click the arrow on the DDP toolbar to go to the next page and the add-in should take over to select your Page Definition Query records, which in turn should update the related graph (the graph properties should be set to display selected records).

Let me know if you get that far - if not we'll need to troubleshoot.  If you cannot enable your extension, then something else is wrong in creating the add-in.

Wayne
0 Kudos
PSArcOnlinePSArcOnline
Deactivated User
Hello Katie,

if you are interested then I will send you my little test project with my addin "select all button". But this will be my old version. I had no time to implement Waynes method yet. So if you want send me a PN with your Email. Don't want to upload it here because I think it is not sophisticated enough for public. 😉

Edit: I use ArcGIS 10.2
0 Kudos
KatieGaut1
Regular Contributor
Hello all,
   I got the code working and wanted to share - in case it helps the next person down the road.  I couldn't get the extension code, but this button works as well.  A big clunkier, but works all the same.  It was very simple in the end.  Thanks so much Wayne and PSArcOnline!

import arcpy import pythonaddins  class Selection(object):     """Implementation for SelectAllButton_addin.button (Button)"""     def __init__(self):         self.enabled = True         self.checked = False     def onClick(self):         # Select all features (in Test there are no features with FID 0).         arcpy.SelectLayerByAttribute_management ("XYMin_Max_Avg_Temp_with_Lat_Long","NEW_SELECTION","Station_Id"<>'0')         pass
0 Kudos