python-visualization / python-visualization/folium

How to open the popup automatically when a search is found for a GeoJson layer?

Ouverte
#1,286 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug
Langage dominant
Python
Étoiles
7.4k
Forks
2.3k
Merge moyen
17 h 22 min
PR mergées (30 j)
11

Description

Please add a code sample or a nbviewer link, copy-pastable if possible
# Your code here
import folium
import branca
import geopandas
from folium.plugins import Search

print(folium.__version__)

states = geopandas.read_file(
    'https://rawcdn.githack.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json',
    driver='GeoJSON'
)

min, max = states['density'].quantile([0.05,0.95]).apply(lambda x: round(x, 2))

colormap = branca.colormap.LinearColormap(
    colors=['#f2f0f7','#cbc9e2','#9e9ac8','#756bb1','#54278f'],
    index=states['density'].quantile([0.2,0.4,0.6,0.8]),
    vmin=min,
    vmax=max
)

colormap.caption="Population Density in the United States"

m = folium.Map(location=[38,-97], zoom_start=4)


style_function = lambda x: {
    'fillColor': colormap(x['properties']['density']),
    'color': 'black',
    'weight':2,
    'fillOpacity':0.5
}


stategeo = folium.GeoJson(
    states,
    name='US States',
    style_function=style_function,
    popup=folium.GeoJsonPopup(
        fields=['name', 'density'],
        aliases=['State', 'Density'], 
        localize=True
    )
).add_to(m)


statesearch = Search(
    layer=stategeo,
    geom_type='Polygon',
    placeholder='Search for a US State',
    collapsed=False,
    search_label='name'
).add_to(m)

folium.LayerControl().add_to(m)
colormap.add_to(m)

m
Problem description

I was trying to make the popup open automatically when a search is found in a GeoJson layer. My code is modified from an example code "plugin-Search.ipynb". I am using the "master" version of folium on the github. Right now, my code cannot open the popup when a search is found. I checked the source code "search.py" in "plugins" folder. The code is:

{{this.layer.get_name()}}searchControl.on('search:locationfound', function(e) {
{{this.layer.get_name()}}.setStyle(function(feature){
return feature.properties.style
})
{% if this.options %}
e.layer.setStyle({{ this.options|tojson }});
{% endif %}
if(e.layer._popup)
e.layer.openPopup();
})

I think the code should open popup if location is found but it did not.

I manually added console.log(e.layer._popup) in front of if(e.layer._popup) in the html file. When I searched one state on the web page, the console outputs "undefined" for e.layer._popup. It's confusing to me.

Expected Output

I expect to open the popup automatically when a search is found like this leaflet search example https://labs.easyblog.it/maps/leaflet-search/examples/geojson-layer.html.

Output of folium.__version__

0+unknown

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par plugins/search.py et l’exemple plugin-Search.ipynb, puis exécutez dans un navigateur la reproduction fournie avec GeoPandas et Folium. Inspectez le gestionnaire search:locationfound et la valeur e.layer._popup signalée ; le travail est terminé lorsqu’un état GeoJSON recherché ouvre automatiquement son popup, comme dans l’exemple Leaflet attendu.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, python
Domaine
data-visualization, frontend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.