Date / Datetime within GeoJSONDataSource

Hi fellow Bokeh-er,

I’m trying to figure out how to use date/datetime embedded within a GeoJSONDataSource. My desire to use the GeoJSONDataSource is to keep data-binding between map objects and the features.

Toy example:

#load data
geojson = """
{"type": "FeatureCollection", 
"features": [
    {"id": "1", 
    "type": "Feature", 
    "properties": 
        {
            "dates": ["2020-01-22T00:00:00", "2020-01-23T00:00:00"], 
            "x": [0,1],
            "y": [0,1]
        },
    "geometry": {"type":"Point","coordinates":[0,0]}
    },
    {"id": "2", 
    "type": "Feature", 
    "properties": 
        {
            "dates": ["2020-01-22T00:00:00", "2020-01-23T00:00:00"], 
            "x": [0,1],
            "y": [1,1]
        },
    "geometry": {"type":"Point","coordinates":[0,0]}
    }
]}
"""

geosource = GeoJSONDataSource(geojson=geojson)

# Try without dates works
p1 = figure(plot_width=400, plot_height=400)

p1.multi_line(xs='x',ys='y',source = geosource)
show(p1)


# Trying with dates does not work
p2 = figure(plot_width=400, plot_height=400, x_axis_type="datetime")

p2.multi_line(xs='dates',ys='y',source = geosource)

show(p2)

Thanks for any input :slight_smile:

In Bokeh, datetime is represented by POSIX timestamps. Try with this:

def convert_dates(gj):
    from datetime import datetime
    import json

    d = json.loads(gj)
    for f in d['features']:
        ps = f.get('properties')
        if ps:
            ds = ps.get('dates')
            if ds:
                ps['dates'] = [datetime.fromisoformat(d).timestamp() for d in ds]

    return json.dumps(d)

geojson = convert_dates(geojson)
1 Like

Thanks for the quick response @p-himik

It works perfectly :slight_smile:

P.S. I had to make one small modification datetime.strptime(d, "%Y-%m-%d").timestamp()*1000

1 Like