Skip to content

Opacity Accumulated with TimestampedGeoJson #2059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghojez opened this issue Dec 21, 2024 · 4 comments
Closed

Opacity Accumulated with TimestampedGeoJson #2059

ghojez opened this issue Dec 21, 2024 · 4 comments

Comments

@ghojez
Copy link

ghojez commented Dec 21, 2024

Hi,

just want to ask if this is actually the expected output with TimestampedGeoJson. The fillopacity feature accumulates over time instead of refreshing or updating the fillOpacity to correspond with the local_date_time? I attached a picture of comparison of fillOpacity in a specific polygon between dates. It's clear that TimestampedGeoJson accumulates the fillOpacity feature so it got darker over time.

    feature = {
        "type": "Feature",
        "geometry": mapping(r["geometry"]),
        "properties": {
            "time": r["local_date_time"].isoformat(),
            "style": {
                "fillColor": colormap(r["trx"]),
                "color": "blue",
                "weight": 0.8,
                "fillOpacity": r['opacity'],

Is that a known limitation or is there anything I could try to prevent the fillOpacity from accumulating?

Thank you

TimestampedGeoJson

@hansthen
Copy link
Collaborator

I am not sure what you mean. Can you give a more complete code example?

@ghojez
Copy link
Author

ghojez commented Dec 22, 2024

Hi, this is the example:

import pandas as pd
import geopandas as gpd
import folium
import branca
from shapely.geometry import Polygon 
from shapely.geometry import mapping
from folium.plugins import TimeSliderChoropleth, TimestampedGeoJson


dx = {
    "local_date_time": ["2024-12-01 10:00", "2024-12-02 10:00"],
    "city": ["Jakarta", "Jakarta"],
    "transaction": [150, 80],
    "rate": [32, 39],
    "opacity": [0.5, 0.1],
    "geometry": [
        Polygon([(106.8, -6.2), (106.85, -6.2), (106.85, -6.25), (106.8, -6.25), (106.8, -6.2)]),
        Polygon([(106.8, -6.2), (106.85, -6.2), (106.85, -6.25), (106.8, -6.25), (106.8, -6.2)]),
    ],
}

df = pd.DataFrame(dx)
df['local_date_time'] = pd.to_datetime(df['local_date_time'])
df = gpd.GeoDataFrame(df, geometry='geometry')

#Colormap
colormap = branca.colormap.LinearColormap(
    colors=['blue','blue'],
    vmin=df['rate'].min(),
    vmax=df['rate'].max(),
    caption='Rate Scale'
)


#Folium
base_template = []
for _, r in df.iterrows():

    feature = {
        "type": "Feature",
        "geometry": mapping(r["geometry"]),
        "properties": {
            "time": r["local_date_time"].isoformat(),
            "tooltip": f"Total Transaction: {r['transaction']}<br>Polygon: {r['geometry']}",
            "style": {
                "fillColor": colormap(r["rate"]),
                "color": "blue",
                "weight": 0.8,
                "fillOpacity": r['opacity'],  # Set opacity based on the calculated value
            },
        },
    }
    base_template.append(feature)

geojson = {
    "type": "FeatureCollection",
    "features": base_template}

# Generate Map
m = folium.Map(location=[-6.1944, 106.8229], zoom_start=10, tiles="CartoDB positron")

# Time Slider
TimestampedGeoJson(
    geojson,
    #transition_time=500,  # Milliseconds between frames
    add_last_point=False,
    period='P1D',
    loop=False,
).add_to(m)

colormap.add_to(m)

m

for the sake of clarity (reproduce the issue), I changed the color scale between df['rate'].min() and df['rate'].max() is blue so the behavior of opacity can be easily noticed. From what I understand, it seems that the previous layer is not removed when the time changes, and hence the double layer created darker appearance. So I want to know whether it's possible to only show layer correspond with the timestamp

@hansthen
Copy link
Collaborator

I see the issue. The TimestampedGeoJson plugin has a duration parameter which determines how long a feature will be displayed on the map. If you keep the default each feature will be displayed until the end of times. You could experiment with that. If you have features that have very strict begin and end times, you could also have a look at the Timeline plugin, which also provides a GeoJson with time feature, but has specific start and end times for each feature.

@ghojez
Copy link
Author

ghojez commented Dec 22, 2024

I see the issue. The TimestampedGeoJson plugin has a duration parameter which determines how long a feature will be displayed on the map. If you keep the default each feature will be displayed until the end of times. You could experiment with that. If you have features that have very strict begin and end times, you could also have a look at the Timeline plugin, which also provides a GeoJson with time feature, but has specific start and end times for each feature.

ah so that's what duration do. Thank you so much Sir, it's really spot on. Now it's working as expected after I added duration

TimestampedGeoJson(
    geojson,
    transition_time=500,  # Milliseconds between frames
    add_last_point=False,
    period='P7D',
    duration='PT1H',
    loop=True,
).add_to(m)

@ghojez ghojez closed this as completed Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants