How to update time slider start and end time using python

707
3
Jump to solution
05-11-2023 07:07 AM
MartinRust
New Contributor II

I have a Web Map in ArcGIS online where I manually created a time slider.
This map contains feature layers with measurement data for the 7 last days, and I update them daily using arcgis.gis.GIS and related modules, so every day the period of the measurement data advances by one day (deleting the data older than 7 days).

My goal is to have Python update the time slider start and end date accordingly - which is what I now do manually every day in Map Viewer, Map Properties, Time Slider Options. 

I can retrieve the Web Map from the GIS, it is of type arcgis.gis.Item, with type="Web Map".

I hope I can somehow use the set_time_extend function as documented in https://developers.arcgis.com/python/api-reference/arcgis.widgets.html#mapview

Can I somehow retrieve an arcgis.widgets.MapView object from this Item, or convert it, so that I can call the set_time_extent function, or is there some other way I can update the time slider?

1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Hi @MartinRust 

The below should help you get there. I have commented but let me know if anything needs further clarification. 

from arcgis import GIS

## access AGOL
agol = GIS("home")

## use the WebMap item ID to get the WebMap
wm_item = agol.content.get("WM_ITEM_ID")

## access the WebMap Item JSON Data
wm_data = wm_item.get_data()

## print the current time slider information to screen.
print(wm_data["widgets"]["timeSlider"])

 

The above will let you see the makeup of the Time Slide JSON. In the continued snippet below we will use the printed information and you can alter any of the properties you need to.

from arcgis import GIS

## access AGOL
agol = GIS("home")

## use the WebMap item ID to get the WebMap
wm_item = agol.content.get("WM_ITEM_ID")

## access the WebMap Item JSON Data
wm_data = wm_item.get_data()

## print the current time slider information to screen.
print(wm_data["widgets"]["timeSlider"])

## update the properties in the below dictionary
## you could use variables to inject in start/end time
## check out documentation for propertues information
## for example esriTimeUnitsDays, esriTimeUnitsMonths
update_timeSlider = {
    'properties': {
        'startTime': 1622044320000,
        'endTime': 1663853100000,
        'thumbCount': 2,
        'thumbMovingRate': 2000,
        'timeStopInterval': {
            'interval': 4,
            'units': 'esriTimeUnitsDays'
        }
    }
}

## apply the updates to the timeSlider property in the widgets
wm_data["widgets"]["timeSlider"] = update_timeSlider

## updaate the JSON for the WebMap Item
wm_item_properties = {"text":wm_data}
wm_item.update(item_properties=wm_item_properties)

 

You can run the first snippet to see that the changes have been applied. Or simply open/refresh the WebMap in a browser.

Just to note: I use this workflow for Classic Map Viewer and have not tested on the New Web Map Viewer.

Hope it helps.

 

~ learn.finaldraftmapping.com

View solution in original post

3 Replies
Clubdebambos
Occasional Contributor III

Hi @MartinRust 

The below should help you get there. I have commented but let me know if anything needs further clarification. 

from arcgis import GIS

## access AGOL
agol = GIS("home")

## use the WebMap item ID to get the WebMap
wm_item = agol.content.get("WM_ITEM_ID")

## access the WebMap Item JSON Data
wm_data = wm_item.get_data()

## print the current time slider information to screen.
print(wm_data["widgets"]["timeSlider"])

 

The above will let you see the makeup of the Time Slide JSON. In the continued snippet below we will use the printed information and you can alter any of the properties you need to.

from arcgis import GIS

## access AGOL
agol = GIS("home")

## use the WebMap item ID to get the WebMap
wm_item = agol.content.get("WM_ITEM_ID")

## access the WebMap Item JSON Data
wm_data = wm_item.get_data()

## print the current time slider information to screen.
print(wm_data["widgets"]["timeSlider"])

## update the properties in the below dictionary
## you could use variables to inject in start/end time
## check out documentation for propertues information
## for example esriTimeUnitsDays, esriTimeUnitsMonths
update_timeSlider = {
    'properties': {
        'startTime': 1622044320000,
        'endTime': 1663853100000,
        'thumbCount': 2,
        'thumbMovingRate': 2000,
        'timeStopInterval': {
            'interval': 4,
            'units': 'esriTimeUnitsDays'
        }
    }
}

## apply the updates to the timeSlider property in the widgets
wm_data["widgets"]["timeSlider"] = update_timeSlider

## updaate the JSON for the WebMap Item
wm_item_properties = {"text":wm_data}
wm_item.update(item_properties=wm_item_properties)

 

You can run the first snippet to see that the changes have been applied. Or simply open/refresh the WebMap in a browser.

Just to note: I use this workflow for Classic Map Viewer and have not tested on the New Web Map Viewer.

Hope it helps.

 

~ learn.finaldraftmapping.com
MartinRust
New Contributor II

Thanks, looks very promising, I've got to the point of retrieving the existing time slider properties and so far it behaves as you documented. I'll try the actual update tomorrow, as my test users are currently using the map, but I'm confident it will work 🙂

0 Kudos
MartinRust
New Contributor II

Confirming it works 😊