How to set a defintionExpression in JS

692
6
Jump to solution
10-18-2019 02:15 PM
HeathAnderson
Occasional Contributor

I am struggling getting my definition expression to work with a time aware field.  I believe I have narrowed down the problem to the layer.definitionExpression.  I have successfully made a definition expression for a text field:

layer.definitionExpression = "project_type = 'SUP'"

but I have been unsuccessful in writing one for a time aware field.  Should I be using the field name {project_date} or 'time'?

timeSlider.watch("timeExtent", function() {
// only show earthquakes happened up until the end of
// timeSlider's current time extent.
//defExp = layer.definitionExpression = "time = " + timeSlider.timeExtent.end.getTime();
//defExp = layer.definitionExpression = "project_type = 'SUP'";
//defExp = layer.definitionExpression = "time=1571202000000";
defExp = layer.definitionExpression = "time="+1571202000000;
defExp2 = layer.definitionExpression = "project_date="+1571202000000;
//checks definitonExpression
console.log(defExp);
console.log(defExp2);

I know that the epoch time 1571202000000 exists in my dataset because I can query it in the rest end point and the query yields 2 results, however that doesn't seem directly translate to JS.  Can some one please help me.

0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there, 

As Robert Scheitlin, GISP‌, you should use standard sql query to query dates. When you are setting FeatureLayer.definitionExpression you are making Rest API query call (search for where).  This blog talks about how to query date fields.  

With all of the above said, you should not have to set the layer's definitionExpression whenever the timeSlider's timeExtent changes. You can set the TimeSlider.view property and all time-aware layers will automatically update to conform to the view's timeExtent. Please look through the TimeSlider SDK doc (see Update the view's doc section).

I have updated your app to do that and here is the link. I noticed that your service's time interval units is set to unknown. So in the test app, I am setting TimeSlider's stop.interval to one day as shown below:

const timeSlider = new TimeSlider({
container: "timeSlider",
playRate: 300,
view: view,
stops: {
interval: {
value: 1,
unit: "days"
}
}
});

View solution in original post

6 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Heath,

  I normally query dates in JS this way:

project_date = TIMESTAMP '2019-10-18 16:30:00'

0 Kudos
HeathAnderson
Occasional Contributor

Robert,

Thank you for your quick reply.  That code didn't seem to work either

defExp2 = layer.definitionExpression = "project_date = TIMESTAMP '2019-10-18 16:30:00'";

defExp2 = layer.definitionExpression = "project_date = TIMESTAMP 2019-10-18 16:30:00";

I had thought about using the TIMESTAMP earlier in my vetting process but seeing as how the database stores it in epoch as well as the timeslider querying in epoch I thought I though I would stick with it.

Either way, neither the TIMESTAMP or epoch code seems to be working with the fieldname or "time" designation

Best,

Heath

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Heath,

   It seems like you are wanting to set a features time extent (ie. begin time and end time) if so then look at the layer timeinfo property:

https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#timeInfo 

0 Kudos
HeathAnderson
Occasional Contributor

Yes i have a the startField set

const layer = new FeatureLayer({
url: "https://gis.appleton.org/pubserver/rest/services/Hosted/EnvisionAppleton/FeatureServer/0",
copyright: "City of Appleton",
title: "Envision Appleton",
// set the CSVLayer's timeInfo based on the date field
timeInfo: {
startField: "project_date", // name of the date field
interval: {
// set time interval to one day
unit: "days",
value: 1
}
},

Here is a copy of my code if you want to look at it instead of me sending snippets

https://codepen.io/handerson1/pen/gOOLYMw?editors=1001 

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

As Robert Scheitlin, GISP‌, you should use standard sql query to query dates. When you are setting FeatureLayer.definitionExpression you are making Rest API query call (search for where).  This blog talks about how to query date fields.  

With all of the above said, you should not have to set the layer's definitionExpression whenever the timeSlider's timeExtent changes. You can set the TimeSlider.view property and all time-aware layers will automatically update to conform to the view's timeExtent. Please look through the TimeSlider SDK doc (see Update the view's doc section).

I have updated your app to do that and here is the link. I noticed that your service's time interval units is set to unknown. So in the test app, I am setting TimeSlider's stop.interval to one day as shown below:

const timeSlider = new TimeSlider({
container: "timeSlider",
playRate: 300,
view: view,
stops: {
interval: {
value: 1,
unit: "days"
}
}
});

View solution in original post

HeathAnderson
Occasional Contributor

Hi Undral,

Thank you for the detail explanation.  That cleared it up for me

Best,

Heath

0 Kudos