python-visualization / python-visualization/folium

Features not displayed with style_function

Open
#1,782 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug
When loading a set of GeoJSON LineString features, they are not displayed on the Folium map in my Jupyter Notebook (GColab, as well) when using style_function. If style_function=style_functions is removed from the following code, the features are displayed correctly, even though without styling.

To Reproduce

import folium
import json

# Load the GeoJSON file
with open('demo.json') as f:
    data = json.load(f)

# Create a Folium map centered on a location of your choice
m = folium.Map(location=[37.982759260869564, 23.901986296376812], zoom_start=18)

# Define a style function that changes the color of LineString features
def style_functions(feature):
    color = 'red'  # Change the color to your desired color or use a custom logic
    return {
        'color': 'blue',
        'weight': 3,
        'opacity': 0.7,
        'dashArray': [5, 10]
    }

# Add the GeoJSON data to the map with the style function applied
folium.GeoJson(data, style_function=style_functions).add_to(m)

# Display the map
m

Demo GeoJSON has been attached. Please remove the .txt extension for it to load.
demo.json.txt

Expected behavior

style_function should not hide the features, but display them with the styling specified.

Environment (please complete the following information):

  • Browser [e.g. firefox]
  • Jupyter Lab & Google Colab
  • Python 3.10.12
  • folium 0.14.0
  • branca 0.6.0

Additional context
In a similar test, PointString features were correctly displayed, with or without the style_function argument.

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 by running the issue's GeoJSON reproduction in Jupyter Notebook with and without style_function, using the attached demo.json data. Trace the Folium GeoJson styling path and verify completion when LineString features render with the requested color, weight, opacity, and dashArray.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.