Select to view content in your preferred language

How Perform A Select Using "OR"?

2631
14
Jump to solution
01-17-2020 03:51 PM
Mark_Hotz
Frequent Contributor

I am using ArcGIS Pro 2.4.3, and just running this script through IDLE for ArcGIS Pro.

I am having some difficulty getting a standard expression in Python to return what I need.  I have created a simple query to try and run...if I can get this to work then I will insert this into my main program with added functions. 

When I run the script it only returns 16 records (i.e. records for only for variable Q1_A).  I'm trying to get it to return 18 records (which would include Q1_B).  

import arcpy

Q1_A = "CHARLES HAYS"
Q1_B = "SMIT CLYDE"
fc1 = (r"F:\ArcGIS_Pro_Projects\AIS\AIS_Data\AIS_Data.gdb\TEST_AIS_Data")

#expression1 = ("Vessel_Name = 'CHARLES HAYS' Or Vessel_Name = 'SMIT CLYDE'")
expression1 = ("Vessel_Name = '" + str(Q1_A) + "'" or "Vessel_Name = '" + str(Q1_B) + "'")
print ("")
print (expression1)
SelectResult = arcpy.SelectLayerByAttribute_management((fc1), "NEW_SELECTION", (expression1))
CountTotal = arcpy.GetCount_management((fc1))
CountResult = arcpy.GetCount_management(SelectResult)
print ("")
print (str(CountResult) + " Of " + str(CountTotal) + " Records Selected")
arcpy.SelectLayerByAttribute_management((fc1), "CLEAR_SELECTION")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hardcoding it (as per what is commented out) works exactly as I need it to work, and returns 18 records.

If I use an "and" instead of an "or" it returns 2 records (only for Q1_B).  Logic tells me that using an "or" it should select 18 records (i.e. for both Q1_A and Q1_B).

The print statement (e.g. print (expression1)) also only prints the variable Q1_A.  

What am I doing wrong here?  How do I get the variable "expression1" to read the entire string?  I know I'm missing something, but for the life of me I just can't see it...and I have been spinning my wheels on this all afternoon.

Suggestions?

Thank you

0 Kudos
14 Replies
DanPatterson_Retired
MVP Emeritus

You can tell how often I do queries

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

To your defense, Esri handled the change in a very passive way over a period of time, and they didn't clean up all their documentation completely.  The Add Field Delimiters tool still states that file geodatabase and shape files use double quotes.

0 Kudos
Mark_Hotz
Frequent Contributor

Thank you all for such awesome (and timely) advice.  I got my script to work, so I'm glad I posted the question.  I have only just recently returned to Python scripting so I'm in the process of relearning the syntax.  Obviously I need to learn a few things...even simple things, like the difference between single and double quotes, the combinations of these, and when to use them.  A couple of these responses have also opened up other options and approaches to solving the same problem...something of which I really appreciate.

I'm going to put all of these responses into my "How 2" folder for future reference for sure.

I'm scheduled to take the first ESRI Python course in February,  on the 20th, with plans to take the more advanced course in probably June if possible.  I might as well really sink my teeth into this as the benefits are HUGE when it comes to geoprocessing for sure (and much more).

I delved into arcpy in August-September as I needed to write a script that would automate a process from FME data conversion to Portal for ArcGIS publishing.  I got it to work entirely in December, but then I ran into problems.  The file geodatabase to which FME adds the converted AIS data has grown to be very cumbersome...it is now over 100-million points and I have only completed data from January to August (the plan is to convert data for each year).  However, before it's uploaded to Portal I filter it using the MOD option to select 1 in 10 records...so the final file size is much more manageable (I'm thinking that I should perhaps get FME to do this before it adds to the GDB too).

What I'm planning to do is every 4 months (using the Select script you helped me fix) select the previous 3 months of AIS data and then dump those records into a Q1, Q2 etc. file geodatabase, which will keep the main file geodatabase at a much more management size.  With over 100-million points my script currently takes over 40 hours to run, but it's only 3-4 hours if it's 3-4 months worth of data (the script will just run after midnight on the first of each month).

The final data in Portal is then used to create heat maps where our Operations Department can then analyze to see where ship traffic is mostly concentrated in and around our harbor.

Thank you very much again

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Mark, please mark one of the responses as Correct to close out the question.  If multiple responses helped, picked the one that helped the most (or was the first) as Correct and mark the others helpful.

0 Kudos
Mark_Hotz
Frequent Contributor

Done.

0 Kudos