Skip to content

Add TimestampedGeoJson to FeatureGroup #2175

Closed

Description

@dbpodrasky

Feature request to enable adding a TimestampedGeoJson object to a folium FeatureGroup. This would allow different feature collections to be plotted, selected, deselected on the same map canvas. Works great for GeoJson, TimestampedGeoJson would be much improved if this was added.

Activity

  1. hansthen commented on Sep 7, 2025

    @hansthen
    Collaborator

    @dbpodrasky Doesn't this "just work" out of the box? What happens if you try this?

  2. dbpodrasky commented on Sep 8, 2025

    @dbpodrasky
    Author

    Does folium.plugins.TimestampedGeoJson() have a feature_group optional argument? When using GeoJson objects I can add them to an existing FeatureGroup, but this doesn't work with TimestampedGeoJson as these can only be added to a map instance.

  3. dbpodrasky commented on Sep 8, 2025

    @dbpodrasky
    Author

    TypeError: TimestampedGeoJson.__init__() got an unexpected keyword argument 'feature_group'

  4. hansthen commented on Sep 8, 2025

    @hansthen
    Collaborator

    I think I see the issue. Thanks for clarifying.

  5. dbpodrasky commented on Sep 8, 2025

    @dbpodrasky
    Author

    Yes, of course. Let me know if I can provide anything else to assist like sample code, build nums, etc.? TimestampedGeoJson is a really nice tool and I'm able to visualize coordinate-mapped dataset really well with it. But, the FeatureGroup selection tool that works with GeoJson is very useful indeed. As a workaround I just make separate maps/HTMLs for each of the collections I would aspire to add to a FeatureGroup.

  6. hansthen commented on Sep 8, 2025

    @hansthen
    Collaborator

    I checked the code, but I do not see an easy way to fix this in the current implementation of TimestampedGeoJson. FeatureGroup objects require Layer objects on the map. However, TimestampedGeoJson combines a Leaflet Control and a Leaflet Layer object, but really is neither. I'd need to do a major refactor of the plugin to fix this.

    As a workaround you could have a look at the timeline plugin. This provides the Timeline class which is a GeoJson with timestamps. It has a separate TimelineControl class. I did not test this myself yet, but it should just work out of the box. (And if it does not it may be simpler to fix.) The specifics on how times are added to the GeoJson are very different, so it may not suit your use case.

    Let me know if timeline works for you.

  7. dbpodrasky commented on Sep 8, 2025

    @dbpodrasky
    Author

    I will look into whether Timeline would be workable for my project. Thanks for the advice!

  8. dbpodrasky commented on Sep 10, 2025

    @dbpodrasky
    Author

    Sorry, Hans. I can't make any sense of Timeline from the available docs. I give up on layer control for timed geodata.

  9. hansthen commented on Sep 10, 2025

    @hansthen
    Collaborator

    @dbpodrasky It feels to early to give up! Are there specific areas where we can improve the documentation? If you send me your geojson, perhaps I can help you re-implement the required functionality using the Timeline class.

  10. dbpodrasky commented on Sep 11, 2025

    @dbpodrasky
    Author

    Okay, let's give it a try. Give me a few days to anonymize an input dataset? Then I can send as a GeoJson.

  11. hansthen commented on Sep 11, 2025

    @hansthen
    Collaborator

    Looking forward to it.

  12. dbpodrasky commented on Sep 12, 2025

    @dbpodrasky
    Author

    Hans, please see the git repo containing some input data in a CSV with two scripts: one plots the data using GeoJson and includes the desired things like layer control, etc. the other uses TimestampedGeoJson to plot the same data animated by timestamps. Hoping to use these two to create a script that will plot time-aware data and also facilitate layer control using FeatureGroups.

    https://github.com/dbpodrasky/FoliumTimestampToTimeline/

  13. hansthen commented on Sep 14, 2025

    @hansthen
    Collaborator

    Will have a look.

  14. hansthen commented on Sep 20, 2025

    @hansthen
    Collaborator

    I got your code to work with Timeline and LayerControl. It is still a bit rough around the edges. Here is the diff:

    diff --git a/TimestampedGeoJson.py b/TimestampedGeoJson.py
    index b62fe7b..ca05a20 100644
    --- a/TimestampedGeoJson.py
    +++ b/TimestampedGeoJson.py
    @@ -1,6 +1,8 @@
     import folium
     from folium.plugins import MeasureControl, MousePosition
    -from folium.plugins import TimestampedGeoJson
    +from folium.plugins import Timeline, TimelineSlider
    +from folium import LayerControl
    +
     import json
     import pandas as pd
     import geopandas
    @@ -70,7 +72,7 @@ for index, tileName in enumerate(names):
     lastTile = list(map._children.items())[-1][0]
     map._children[lastTile].show = True
     
    -folium.plugins.TimestampedGeoJson(
    +timeline = Timeline(
         {
             "type": "FeatureCollection",
             "features": [
    @@ -81,7 +83,8 @@ folium.plugins.TimestampedGeoJson(
                     "coordinates": item[1]
                     },
                 "properties": {
    -                "times": item[0],
    +                "start": item[0][0],
    +                "end": item[0][1],
                     "popup": item[2],
                     "icon": "circle",
                     "iconstyle": {"color": colors[item[3]], "fill": "true", "fillOpacity": 1.0, "radius": 5},
    @@ -90,12 +93,15 @@ folium.plugins.TimestampedGeoJson(
             for item in zip(times, coords, popups, data)
             ]
         },
    -    period="P1D",
    -    duration="P0D",
    -    min_speed=0.3,
    -    max_speed=3.0,
    -    transition_time=1000,
    -    auto_play=False,
     ).add_to(map)
     
    -map.save('TimestampedGeoJson.html')
    +TimelineSlider(
    +    auto_play=False,
    +    show_ticks=True,
    +    enable_keyboard_controls=True,
    +    playback_duration=30000,
    +).add_timelines(timeline).add_to(map)
    +
    +LayerControl().add_to(map)
    +
    +map.save('UseTimeLine.html')
    
  15. hansthen commented on Sep 20, 2025

    @hansthen
    Collaborator

    What is missing is using a circle for the icon. You can add that to the Timeline constructor by adding a point_to_layer function.

    Something like this: point_to_layer=JsCode("(f, latlng) => { return L.circleMarker(latlng, {radius: 8, fillOpacity: 0.2})}"). You can find more examples in the documentation of the Realtime plugin.

    Also, the layer name is now weird. This can be remedied by wrapping Timeline in a FeatureGroup. Let me know if this helps you further.

  16. dbpodrasky commented on Sep 22, 2025

    @dbpodrasky
    Author

    Wow! Looking forward to testing this diff.
    Thanks, Hans.

  17. dbpodrasky commented on Oct 1, 2025

    @dbpodrasky
    Author

    Coming back to this now and it is working well so far. I need to research the JS stuff a bit, but I am familiar with FeatureGroups.

  18. dbpodrasky commented on Oct 3, 2025

    @dbpodrasky
    Author

    It's the JsCode wrapper that I can't get to work. If I try to call point_to_layer in TimelineSlider the map breaks, while if I try to pass my Timeline instance to Realtime it also breaks. I would guess that Timeline is incompatible with Realtime and that point_to_layer can't be used with TimelineSlider.

    I need to be able to color each point based on data run through a colormap.

  19. hansthen commented on Oct 4, 2025

    @hansthen
    Collaborator

    Can you show me your code? Is your code in the repo you sent? Realtime is not really applicable to your use case. You should add point_to_layer to your Timeline instance (not the TimeLineSlider).

    Probably the easiest way is to add a color property to each of the features in your geojson data and then create a JsCode like this:

    Timeline(
        point_to_layer=JsCode("(f, latlng) => { return L.circleMarker(latlng, {color: f.properties.color})}"),
    )
    
  20. dbpodrasky commented on Oct 6, 2025

    @dbpodrasky
    Author

    Pushed the latest version of my working branch to https://github.com/dbpodrasky/FoliumTimestampToTimeline/blob/Timestamp-to-Timeline/
    As you can see, using list comprehension to create the Timeline() object. Is point_to_layer compatible with the list comprehension?

  21. dbpodrasky commented on Oct 8, 2025

    @dbpodrasky
    Author

    It's working!
    https://github.com/dbpodrasky/FoliumTimestampToTimeline on branch main.
    Is it worthwhile to make an example entry in the docs for Timeline to demonstrate point_to_layer and adding Timeline objects to FeatureGroups?

  22. hansthen commented on Oct 8, 2025

    @hansthen
    Collaborator

    Ah cool. Happy that it works. I was just studying your code and I was afraid I had to confess I did not understand some of the pandas code. Now I can just claim that I understood everything :-).

    As for documentation is that something you are willing to contribute? Our documentation is always in need of improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions