python-visualization / python-visualization/folium

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

オープン
#1,286 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
7.4k
フォーク
2.3k
平均マージ
17時間 22分
マージ済み PR(30日)
11

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

plugins/search.py と plugin-Search.ipynb の例から始め、続いて提供されている GeoPandas と Folium の再現コードをブラウザで実行します。search:locationfound ハンドラーと報告された e.layer._popup の値を調べます。期待される Leaflet の例と同様に、検索した GeoJSON の州が自動的にポップアップを開けば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, python
領域
data-visualization, frontend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。