Syntax for timeFilter

1755
9
12-18-2016 10:01 PM
NathanDaniels
New Contributor III

I am wondering if there is any sample code for the ArcREST timeFilter. I understand that it should be in Unix millisecond format. While the following line of code compiles in Python it returns the same number of records that I get without the timeFilter:

data = (fl.query(where="1=1", timeFilter="1482105600000") )

What I would like to get is only those records after that epoch time-stamp. Is my syntax correct? How would I also specify between a start and end time?

0 Kudos
9 Replies
RandyBurton
MVP Alum

Try:

1=1 AND timeFilter >= '2016-12-19 00:00:00'‍‍‍‍

or

1=1 AND timeFilter BETWEEN '2016-12-19 00:00:00' AND '2016-12-21 00:00:00'‍‍‍‍‍‍‍

If the response format is JSON, then you will need to convert any returned date/time fields from Unix millisecond format.  However for the query text, you will want to use a time string such as above, but something like '12/19/16' may also work.  You may also wish to use the DATE keyword before your date/time string.

0 Kudos
RandyBurton
MVP Alum

Here's a Python snippet to demonstrate:

# your url, something like:
URL = "https://services.arcgis.com/<abc123>/arcgis/rest/services/<myFeatureLayer>/FeatureServer/0/query"

query_dict = {
    "where" : "1=1 AND timeFilter BETWEEN DATE '2016-12-19 00:00:00' AND DATE '2016-12-21 00:00:00'",
    "outFields" : "OBJECTID, timeFilter, Field1, Field2, FieldN",
    "orderByFields" : "timeFilter",
    "returnGeometry" : "true",
    "f": "json", "token": token['token'] }

# results in json format
jsonResponse = urllib.urlopen(URL, urllib.urlencode(query_dict))
features = json.loads(jsonResponse.read(),
                      object_pairs_hook=collections.OrderedDict)[u'features']
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
NathanDaniels
New Contributor III

In the python snippet it specifies timeFilter in the outFields. Does this mean that there needs to be a field called timeFilter? How does the filter know which field to look at: CreationDate, EditDate etc.?

0 Kudos
RandyBurton
MVP Alum

I had made the assumption the date/time field name you were querying against was called "timeFilter".  Use whatever name this field is called.  You do not need to list it in the outFields if you are not interested in seeing or using it, likewise, you can sort (or not) on it or another field.

0 Kudos
NathanDaniels
New Contributor III

The timeFilter I am referring to is in the query section of this documentation:

http://esri.github.io/ArcREST/arcrest.ags.html

It would seem that timeFilter is an object where a UTC timestamp in milliseconds can be passed to it, but correct me if I’m not interpreting the docs right.

0 Kudos
RandyBurton
MVP Alum

I haven't played around much with the ArcREST modules on github.  Most of my interface with the REST API has been via ArcGIS Online (documentation here: ArcGIS REST API).  Where I used timeFilter in my code sample, you could query the EditDate or CreateDate fields if editor tracking is enabled, and then you probably would not need the timeFilter reference.  Probably something like:

data = (fl.query(where="1=1 AND EditDate >= DATE '2016-12-19 00:00:00'") )‍‍

I will look at the github code in the next few days to see how "timeFilter" is being used.

0 Kudos
RandyBurton
MVP Alum

I have looked at the code, and I believe the "timeFilter" equates to the "time" option found on the ArcREST API Query Feature Service page.  It gives 3 examples with a little explanation:

time=1199145600000  
time=1199145600000, 1230768000000
time=null, 1230768000000

When I experimented with this, the query always returned all rows.  My records have a CreateDate and an EditDate field, so I am not sure what the "timeFilter" or "time" option is doing.

I did use this line of code in the github code to successfully retrieve records after a specific date/time:

print (fl.query(where="EditDate > DATE '2016-05-29 18:30:00'",out_fields='*',returnGeometry=False) )
0 Kudos
RandyBurton
MVP Alum

Perhaps the "time" or "timeFilter" is for working with the archive class.  Working directly with the archive class mentions "If you are interested in seeing a specific feature at a specific time, refer to its object ID and enter the appropriate date and time."

0 Kudos
RandyBurton
MVP Alum

Using the ArcREST modules on github, your code would be:

# create a time filter object
tf = TimeFilter(start_time=123, time_zone="UTC", end_time=456)
# or start and end times
tf = TimeFilter(123, end_time=456)
# or just a start time
tf = TimeFilter(123)

data = (fl.query(where="1=1", timeFilter=tf) )
# or
data = (fl.query(where="1=1", timeFilter=TimeFilter(start_time=123, end_time=456)) )‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

It does not appear that "time_zone" is implemented in the TimeFilter class. You may need to adjust the time stamp (Unix millisecond format) to the database's time zone if it is UTC or something other than your local time.

0 Kudos