python-visualization / python-visualization/folium

FeatureGroupSubGroup: nth level subgroups

Open
#1,758 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

More info needed
Dominant language
Python
Stars
7.4k
Forks
2.3k
Avg merge
17h 22m
Merged PRs (30d)
11

Description

Describe the bug
I am using quite a few levels of recursion to generate a map that has subgroups within subgroups, and im not sure how to fix the issue, or if there is a better method.

To Reproduce
theres alot going on here... the repeated for loops are to keep the buttons ordered by hierarchy.

import folium
from folium import plugins

#####################################################################################
coord = df['Location'].apply(fetch_lat_lon)
df['Lat'] = coord.apply(lambda x: x[0] if isinstance(x, tuple) else None)
df['Long'] = coord.apply(lambda x: x[1] if isinstance(x, tuple) else None)

df['Result'] = (~df['Impounded'].isna().astype(int)).apply(lambda x: _results[x])
df['Interval'] = (datetime.now() - df.index).days
df['Weekday'] = df.index.weekday.map(lambda x: _days[x])
df['Time'] = df.index.hour

df = df[~df['ID'].isna()]                                                           
#####################################################################################
m = folium.Map(location=[0, 0], zoom_start=14)


results = {}
intervals = {}
days = {}
times = {}

_results = ['Complete', 'Canceled']
_intervals = [7, 30, 90, 365] ## days before now
_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
_times = range(24)

for result in _results:

    results[result] = folium.FeatureGroup(name = result)
    m.add_child(results[result])

for result in _results:
    for interval in _intervals: 

        intervals[f'{result}_{interval}'] = plugins.FeatureGroupSubGroup(results[result], f'{interval} Days')
        m.add_child(intervals[f'{result}_{interval}'])

for result in _results:
    for interval in _intervals: ## days before now
        for day in _days:

            days[f'{result}_{interval}_{day}'] = plugins.FeatureGroupSubGroup(intervals[f'{result}_{interval}'], day)
            m.add_child(days[f'{result}_{interval}_{day}'])

for result in _results:
    for interval in _intervals: ## days before now
        for day in _days:
            for time in _times:
                
                if time < 12:
                    if time !=0:
                        times[f'{result}_{interval}_{day}_{time}'] = plugins.FeatureGroupSubGroup(days[f'{result}_{interval}_{day}'], f'{time} AM')
                        m.add_child(times[f'{result}_{interval}_{day}_{time}'])
                    else: 
                        times[f'{result}_{interval}_{day}_{time}'] = plugins.FeatureGroupSubGroup(days[f'{result}_{interval}_{day}'], f'{time + 12} AM')
                        m.add_child(times[f'{result}_{interval}_{day}_{time}'])
                elif time > 12:
                    times[f'{result}_{interval}_{day}_{time}'] = plugins.FeatureGroupSubGroup(days[f'{result}_{interval}_{day}'], f'{time - 12} PM')
                    m.add_child(times[f'{result}_{interval}_{day}_{time}'])
                else:
                    times[f'{result}_{interval}_{day}_{time}'] = plugins.FeatureGroupSubGroup(days[f'{result}_{interval}_{day}'], f'{time} PM')
                    m.add_child(times[f'{result}_{interval}_{day}_{time}'])

for result in _results:
    for interval in _intervals: ## days before now
        for day in _days:
            for time in _times:

                group = times[f'{result}_{interval}_{day}_{time}']

                condition = ((df['Result'] == result) &
                             (df['Interval'] <= interval) &
                             (df['Weekday'] == day) &
                             (df['Time'] == time)
                )

                _df = df[condition].dropna()

                for i in range(len(_df)):
                    lat = _df['Lat'].iloc[i] - _df['Lat'].mean()
                    long = _df['Long'].iloc[i] - _df['Long'].mean()

                    folium.Marker([lat, long]).add_to(group)
            
folium.LayerControl(collapsed=True).add_to(m)

m.save('TESTING.html')

Expected behavior
When all button conditions are met, points will display,

Environment (please complete the following information):

  • chrome
  • jupyter notebooks
  • vs code

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided FeatureGroupSubGroup construction and the generated TESTING.html in the Jupyter/VS Code reproduction; verify whether nested subgroups beyond one level are represented and toggled as expected. Define the desired nth-level behavior and confirm it with a minimal map reproduction, including the stated point-display condition.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, python
Domain
data-visualization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.