Search code examples
pythongeojsonfolium

TimestampedGeoJson duration parameter causes polygons to disappear


I'm having trouble modifying a code snippet from the second TimestampedGeoJson example in the Plugins example notebook.

The duration parameter is described as the "period of time which the features will be shown on the map after their time has passed. If None, all previous times will be shown."

Take the case below with two polygons

import folium
from folium.plugins import TimestampedGeoJson

m = folium.Map(location=[52.467697, -2.548828], zoom_start=6)

polygon_1 = {
    'type': 'Feature',
    'geometry': {
        'type': 'MultiPolygon',
        'coordinates': [((
             (-2.548828, 51.467697),
             (-0.087891, 51.536086),
             (-1.516113, 53.800651),
             (-6.240234, 53.383328),
        ),)],
    },
    'properties': {
        'style': {
            'color': 'blue',
        },
        'times': ['2015-07-22T00:00:00', '2015-08-22T00:00:00',
                  '2015-09-22T00:00:00', '2015-10-22T00:00:00',
                  '2015-11-22T00:00:00', '2015-12-22T00:00:00']
    }
}

polygon_2 = {
    'type': 'Feature',
    'geometry': {
        'type': 'MultiPolygon',
        'coordinates': [((
             (-3.548828, 50.467697),
             (-1.087891, 50.536086),
             (-2.516113, 52.800651),
             (-7.240234, 52.383328),
        ),)],
    },
    'properties': {
        'style': {
            'color': 'yellow',
        },
        'times': ['2015-07-22T00:00:00', '2015-08-22T00:00:00']
    }
}

TimestampedGeoJson(
    {'type': 'FeatureCollection', 'features': [polygon_1, polygon_2]},
    period='P1M',
    duration='P1M',
    auto_play=False,
    loop=False,
    loop_button=True,
    date_options='YYYY/MM/DD',
).add_to(m)

m

The first polygon is active from July to December so I expect it to be drawn for all time periods; the second polygon is active only in July and August so it should be drawn up until one month after its last month: so July, August and September.

Instead what I see is that both polygons are drawn in the first period, disappear in the second, then the second polygon is drawn in September and disappears again in October. To be clear:

Expected

+-----------+----------+----------+
|   Month   | Polygon1 | Polygon2 |
+-----------+----------+----------+
| July      | X        | X        |
| August    | X        | X        |
| September | X        | X        |
| October   | X        |          |
| November  | X        |          |
| December  | X        |          |
+-----------+----------+----------+

Observed

+-----------+----------+----------+
|   Month   | Polygon1 | Polygon2 |
+-----------+----------+----------+
| July      | X        | X        |
| August    |          |          |
| September | X        |          |
| October   |          |          |
| November  |          |          |
| December  |          |          |
+-----------+----------+----------+

Is this a bug in the duration parameter, or am I missing something?

I'm using folium version 0.6.0 without an ad-blocker. This happens in both Jupyter and html export. The duration parameter was introduced in #894.


Solution

  • Since the same question was asked on GitHub and was also answered there, I'm going to copy-paste the answer of GitHub-user "andy23512" hereafter in order to help out people who don't find the answer by accident on GitHub.

    In the following the quoted answer is stated:

    "According to the corresponding document of leaflet.js, ( https://github.com/socib/Leaflet.TimeDimension/tree/520cb80f645112e242c5160cb44b7d5f2cae380d#ltimedimensionlayergeojson )

    coordTimes, times or linestringTimestamps: array of times that can be associated with a geometry (datestrings or ms). In the case of a LineString, it must have as many items as coordinates in the LineString.

    That means if one want to show the polygon at those 6 timestamps, the length of geometry coordinate list should be 6, in order to specify the polygon to be shown at corresponding timestamp.

    In you case, you want to show the same polygon (polygon1) at those 6 timestamps. You can add * 6 after the coordinate list to make copies.

    So the code that meet your expectation would be:

    import folium
    from folium.plugins import TimestampedGeoJson
    
    m = folium.Map(location=[52.467697, -2.548828], zoom_start=6)
    
    polygon_1 = {
        'type': 'Feature',
        'geometry': {
            'type': 'MultiPolygon',
            'coordinates': [((
                 (-2.548828, 51.467697),
                 (-0.087891, 51.536086),
                 (-1.516113, 53.800651),
                 (-6.240234, 53.383328),
            ),)] * 6, # duplicatation for matching 6 timestamps
        },
        'properties': {
            'style': {
                'color': 'blue',
            },
            'times': ['2015-07-22T00:00:00', '2015-08-22T00:00:00',
                      '2015-09-22T00:00:00', '2015-10-22T00:00:00',
                      '2015-11-22T00:00:00', '2015-12-22T00:00:00']
        }
    }
    
    polygon_2 = {
        'type': 'Feature',
        'geometry': {
            'type': 'MultiPolygon',
            'coordinates': [((
                 (-3.548828, 50.467697),
                 (-1.087891, 50.536086),
                 (-2.516113, 52.800651),
                 (-7.240234, 52.383328),
            ),)] * 2, # duplicatation for matching 2 timestamps
        },
        'properties': {
            'style': {
                'color': 'yellow',
            },
            'times': ['2015-07-22T00:00:00', '2015-08-22T00:00:00']
        }
    }
    
    TimestampedGeoJson(
        {'type': 'FeatureCollection', 'features': [polygon_1, polygon_2]},
        period='P1M',
        duration='P1M',
        auto_play=False,
        loop=False,
        loop_button=True,
        date_options='YYYY/MM/DD',
    ).add_to(m)
    
    m
    

    Hope that helps."