Parsing GeoJSONLayer Date Field for Time Slider

1598
3
Jump to solution
10-07-2021 07:32 AM
ADClark
New Contributor II

Working to implement a time slider for a GeoJSONLayer using ArcGIS JS 4.21, but have run into an issue regarding the formatting of the field in the GeoJSON file.

According to https://doc.arcgis.com/en/arcgis-online/manage-data/work-with-date-fields.htm the closest supported date format for an input file is YYYY-MM-DDThh:mm:ss.s, but the GeoJSON file I am working with has the format of YYYY-MM-DDThh:mm:ss (no fractional second included).

From the timeslider filter example, it looks like the timeslider is expecting unix timestamps.

To clarify my question- how can I get the timeslider to work with GeoJSON date fields that are currently formatted as YYYY-MM-DDThh:mm:ss ?

Example code:

  const layer = new GeoJSONLayer({
    url: geojsonurl,
    geometryType: "point",
    title: "Observations",
    visible: true,
    fields: [
      { name: "ObservationId", type: "string" },
      { name: "MeasuredAt", type: "date" }
    ],
    outfields: ["*"],
    timeInfo: {
      startField: "MeasuredAt", // name of the date field stored as "2020-06-13T09:33:00"
      interval: {
        unit: "days",
        value: 1
      }
    }

Example error:

//[object, object]
{
  "error":{
    "code": null,
    "description": "Value 2020-06-25T20:03:00 is not a valid value for the field type - 
    field: MeasuredAt,
    type: esriFieldTypeDate,
    nullable: true"
  }
}

 

Thanks for any pointers.

0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there, 

So the GeoJSONLayer creates date fields if the GeoJSON feature properties contain unix/epoch timestamp. In your case, feature property contains ISO 8601 strings. So you will need to:

  1. Intercept your data after it is fetched.
  2. Loop through the GeoJSON features
  3. Get the ISO value from the feature 
  4. Parse a unix/epoch timestamp from the ISO value 
  5. Create a new property of the feature and assign the new epoch timestamp
  6. Then in your GeoJSONLayer constructor
    1. Set fields property.  Fields can only contain the property that has the unix timeStamp and set the field type to date.
    2. Set timeInfo property as you shown above. But for the field use the field you created in the step above. 

This test app shows everything I listed above. However, this app needs to be updated when you upgrade to 4.22 or above versions. At 4.21, when you intercept the geojson request, the geojson data is returned as array buffers, so you will have to decode those array buffers at 4.21.

At 4.22, we are changing this behavior.  We updated the request flow of workers  so that an after interceptor receives the expected data type. This means after interceptors that were expecting array-buffers for client-side layers such as GeoJSONLayer and CSVLayer will receive the expected data types json or text. At the very end of this test app, I added an after interceptor function that will work at 4.22. 

Lastly, we will update the doc for GeoJSONLayer.timeInfo property to show the workflow we talked about in this discussion. 

Hope this helps, 

-Undral

View solution in original post

3 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

So the GeoJSONLayer creates date fields if the GeoJSON feature properties contain unix/epoch timestamp. In your case, feature property contains ISO 8601 strings. So you will need to:

  1. Intercept your data after it is fetched.
  2. Loop through the GeoJSON features
  3. Get the ISO value from the feature 
  4. Parse a unix/epoch timestamp from the ISO value 
  5. Create a new property of the feature and assign the new epoch timestamp
  6. Then in your GeoJSONLayer constructor
    1. Set fields property.  Fields can only contain the property that has the unix timeStamp and set the field type to date.
    2. Set timeInfo property as you shown above. But for the field use the field you created in the step above. 

This test app shows everything I listed above. However, this app needs to be updated when you upgrade to 4.22 or above versions. At 4.21, when you intercept the geojson request, the geojson data is returned as array buffers, so you will have to decode those array buffers at 4.21.

At 4.22, we are changing this behavior.  We updated the request flow of workers  so that an after interceptor receives the expected data type. This means after interceptors that were expecting array-buffers for client-side layers such as GeoJSONLayer and CSVLayer will receive the expected data types json or text. At the very end of this test app, I added an after interceptor function that will work at 4.22. 

Lastly, we will update the doc for GeoJSONLayer.timeInfo property to show the workflow we talked about in this discussion. 

Hope this helps, 

-Undral

ADClark
New Contributor II

Very thorough reply, thank you. Any timetable for 4.22 release?

 

 

0 Kudos
UndralBatsukh
Esri Regular Contributor

It will be released end of December 2021. 

0 Kudos