Skip to content

TimestampedGeoJson not working with MultiPolygons #1714

@Coni63

Description

@Coni63

Describe the bug
When a TimestampedGeoJson is used with basic Polygon, it is working fine but when it is multipolygons, only one sub-polygon is rendered

To Reproduce

import datetime
import numpy as np

import geojson
import folium
from folium.plugins import TimestampedGeoJson

import shapely
import shapely.ops
from shapely.geometry import MultiPolygon, Polygon, LineString

######################
#### Creation of polygons / multipolygons
######################

x1 = np.array(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)))

x2 = x1 * 0.1 + 1

x3 = x1 * 0.1
x3[:, 0] += 1
x3[:, 1] -= 0.1

x4 = x1 * 0.1
x4[:, 0] -= 0.1
x4[:, 1] -= 0.1

x5 = x1 * 0.1
x5[:, 1] += 1
x5[:, 0] -= 0.1

c1 = Polygon(x)
c2 = Polygon(x2)
c3 = Polygon(x3)
c4 = Polygon(x4)
c5 = Polygon(x5)

p1 = shapely.ops.unary_union([c1, c2, c3, c4, c5])
p2 = shapely.affinity.translate(p1, xoff=2, yoff=0)
p3 = shapely.affinity.translate(p1, xoff=0, yoff=2)

t1 = datetime.datetime(2023, 1, 1, 0, 0, 0)
t2 = datetime.datetime(2023, 1, 1, 6, 0, 0)
t3 = datetime.datetime(2023, 1, 1, 12, 0, 0)

data = [
    (p1, t1),
    (p2, t2),
    (p3, t3),
    (p1, t1),  # just to loop and have all 3 polygons
]

######################
#### Creation of geojson
######################

def create_geojson(data):
    features = []
    
    for (shp1, dt1), (shp2, dt2) in zip(data[:-1], data[1:]):
        feat = geojson.Feature(
            geometry=shapely.geometry.mapping(shp1),
            properties={
                "times": [1000*datetime.datetime.timestamp(dt1), 1000*datetime.datetime.timestamp(dt2)],
            }
        )
        
        features.append(feat)
        
        center1 = shp1.centroid
        center2 = shp2.centroid
        line = LineString([center1, center2])
        feat = geojson.Feature(
            geometry=shapely.geometry.mapping(line),
            properties={
                "times": [1000*datetime.datetime.timestamp(dt1), 1000*datetime.datetime.timestamp(dt2)],
            }
        )
        
        features.append(feat)
        
    return geojson.FeatureCollection(features)
    
geo = create_geojson(data)

######################
#### Rendering
######################

m = folium.Map(location=[1, 1], zoom_start=7)

folium.GeoJson(geo).add_to(m)  # works well
TimestampedGeoJson(geo, period="PT1H").add_to(m) # not working with multipolygons

m

Geojson

image

TimestampedGeoJson

image

Expected behavior
Instead of one sub-polygon, the complete multipolygon shoudl be rendered leading to the same result as folium.GeoJson

Environment (please complete the following information):

  • Browser : Chrome - Windows
  • Jupyter Notebook
  • Python version : 3.9.13
  • folium version : 0.14.0
  • branca version : 0.6.0

Additional context
Add any other context about the problem here.

Possible solutions
List any solutions you may have come up with.

folium is maintained by volunteers. Can you help making a fix for this issue?

I'll try, it's a good exercice :D

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