how to add multiple filters using query() of feature layer

150
4
2 weeks ago
Manager_HMWSSBCDC
New Contributor III

time = f"closingdate >= DATE '{from_date_str}' AND closingdate <= DATE '{to_date_str}'"
value = 'MCC'
field_filter = f"trip_name == '{value}'"
query_filter = f"{time_filter} AND {field_filter}"

# Query the feature layer
query_result = tankers.query(where=f"trip_name = 'MCC' AND time_filter={time}")

 

Can anyone suggest what's wrong in the above code.

0 Kudos
4 Replies
RPGIS
by
Occasional Contributor III

Which language is this? I want to say python, but it is hard to tell from the looks of it.

If it is python, then you would need to adjust your dates to be equivalent to the following:

from datetime import datetime

FromDate = <FromDateField>.isoformat(timespec='seconds')
ToDate = <ToDateField>.isoformat(timespec='seconds')

time = f"closingdate >= {FromDate} AND closingdate <= {ToDate}"

# Query the feature layer
query_result = tankers.query(where=f"trip_name = {'MCC'} AND {time}")

 

0 Kudos
Manager_HMWSSBCDC
New Contributor III

Hi, 

Thanks for the reply. I wrote this using ArcGIS API Python

I followed according to your code:

 

portal_item = gis.content.search(query="title:tankers", item_type="Feature Layer")
flayer = portal_item[3]
tankers = flayer.layers[0]

from datetime import datetime
# Convert the string to a datetime object
FromDate = datetime.strptime(from_date_str, '%d-%b-%Y').isoformat(timespec='seconds')
ToDate = datetime.strptime(to_date_str, '%d-%b-%Y').isoformat(timespec='seconds')

time = f"closingdate >= {FromDate} AND closingdate <= {ToDate}"

# Query the feature layer
query_result = tankers.query(where=f"trip_name = {'MCC'} AND {time}")

 

But I got the following error message:

Exception: Unable to complete operation.
Query with count request failed.
(Error Code: 400)

 where as in documentation, the syntax is different

query(where="trip_name='MCC'",time_filter=[<start_time>,<end_time>)

I tried the above one too, its not throwing any error but considering only first parameter in where clause not considering time_filter parameter.

0 Kudos
MarkKinnaman
New Contributor III

 

Try the code below as I believe the time_filter parameter wants a datetime object and when you use isoformat it converts it to a string.

portal_item = gis.content.search(query="title:tankers", item_type="Feature Layer")
flayer = portal_item[3]
tankers = flayer.layers[0]

from datetime import datetime
# Convert the string to a datetime object
FromDate = datetime.strptime(from_date_str, '%d-%b-%Y')
ToDate = datetime.strptime(to_date_str, '%d-%b-%Y')

# Query the feature layer
query_result = tankers.query(where=f"trip_name = {'MCC'},time_filter=[FromDate, ToDate])

 

RPGIS
by
Occasional Contributor III

You do not need to use the datetime.strptime() function since python will automatically convert a datetime python object to a sql datetime object when you specify the actual datefield.

from datetime import datetime
Sample = datetime.now().isoformat(timespec='seconds')
print( Sample )
#printed result = '2024-05-13T08:42:27'

.

0 Kudos