Search code examples
pythongisdeck.gl

Animating TripsLayer in deck.gl with python


I have seen the TripsLayer example in the deck.gl website (this one) and it looks really cool. I would like to accomplish the same but using pydeck, the python bindings for deck.gl. The example in pydeck's webpage (this one) is not animated and I am not sure how should I do it to get a smooth animation as shown in the javascript example. I have tried multiple things (passing lists, functions, variables with changing value etc.) but non of them have worked and I can't find any example with pydeck.

Thanks!


Solution

  • It's true that the example should include more trips. Here is how to achieve the animation of multiple trips in a jupyter notebook.

    import time
    import pandas as pd
    import pydeck as pdk
    
    data = '[{"agent_id":0,"path":[[-0.63968,50.83091,0.0],[-0.78175,50.83205,0.0]],"time":[65100,65520],"color":[228,87,86]},{"agent_id":1,"path":[[-0.63968,50.83091,0.0],[-0.78175,50.83205,0.0]],"time":[65940,66420],"color":[178,121,162]},{"agent_id":2,"path":[[-0.63968,50.83091,0.0],[-0.37617,50.8185,0.0]],"time":[65340,66360],"color":[157,117,93]},{"agent_id":3,"path":[[-0.63968,50.83091,0.0],[-0.78175,50.83205,0.0]],"time":[65940,66420],"color":[238,202,59]},{"agent_id":4,"path":[[-0.63968,50.83091,0.0],[-0.78175,50.83205,0.0]],"time":[67740,68160],"color":[157,117,93]}]'
    
    df = pd.read_json(data)
    view = {"bearing": 0, "latitude": 50.85, "longitude": -0.16, "pitch": 0, "zoom": 9}
    
    time_min = 65_000
    time_max = 80_000
    
    layer = pdk.Layer(
        "TripsLayer",
        df,
        get_path='path',
        get_timestamps='time',
        get_color='color',
        opacity=0.8,
        width_min_pixels=3,
        rounded=True,
        trail_length=900,
        current_time=0
    )
    
    # Render
    r = pdk.Deck(layers=[layer], initial_view_state=view, map_style='dark_no_labels')
    
    r.show()
    
    # Animate
    for ct in range(time_min, time_max, 100):
        layer.current_time = ct
        r.update()
        time.sleep(0.1)