Skip to content
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

Add Search Box on Folium map #464

Closed
sangyh opened this issue Jul 15, 2016 · 62 comments
Closed

Add Search Box on Folium map #464

sangyh opened this issue Jul 15, 2016 · 62 comments
Labels
feature request Feature request or idea about how to make folium better plugin This issue/PR is about an existing or new plugin

Comments

@sangyh
Copy link

sangyh commented Jul 15, 2016

Can anybody point me towards how to add a Search box on Folium?
I know Leaflet has this functionality but couldn't find any documentation on Folium.
Thanks in advance.

@ocefpaf
Copy link
Member

ocefpaf commented Aug 25, 2017

There is no search box on folium. One could add a plugin for the https://github.com/stefanocudini/leaflet-search and implement it.

PRs welcome 😄

@ocefpaf ocefpaf added feature request Feature request or idea about how to make folium better plugin This issue/PR is about an existing or new plugin labels Aug 25, 2017
@ghandic
Copy link
Contributor

ghandic commented Oct 16, 2017

@sangyh

I have started to work on the leaflet-search plugin, it is awaiting a Pull Request, if you're still interested in this plugin a peer review would be great.

@morteza3000
Copy link

@ocefpaf
can you please help more about mentioned plugin, how to install or add to a folium project?

@ghandic
Copy link
Contributor

ghandic commented Oct 17, 2017 via email

@morteza3000
Copy link

No I didn't, can you please give me the name of that example notebook?

@ghandic
Copy link
Contributor

ghandic commented Oct 17, 2017 via email

@morteza3000
Copy link

I'll really appreciate if you can share it.

@ghandic
Copy link
Contributor

ghandic commented Oct 17, 2017

PR: #747

https://github.com/ghandic/folium/blob/search-features/examples/Plugins-Search.ipynb

(all of the data is self contained in the notebook - this will be changed shortly. This search feature is on my feature-seach branch

@ocefpaf
Copy link
Member

ocefpaf commented Oct 17, 2017 via email

@morteza3000
Copy link

@ghandic
Thanks a lot but I faced with below error when I wanted to run notebook.


AttributeError Traceback (most recent call last)
in ()
9 )
10
---> 11 plugins.Search(states, search_zoom=6, geom_type="Polygon").add_to(m)
12
13 m.save(os.path.join('results', 'Plugins_search_polygon.html'))

AttributeError: 'module' object has no attribute 'Search'

@ghandic
Copy link
Contributor

ghandic commented Oct 18, 2017 via email

@morteza3000
Copy link

sorry I'm totally new to folium, I've just install folium by pip and add your search.py file to "C:\ProgramData\Anaconda2\Lib\site-packages\folium\plugins". is it write?
can you help what to do step by step?

@ghandic
Copy link
Contributor

ghandic commented Oct 18, 2017

run this:

pip uninstall -y folium
pip install git+https://github.com/ghandic/folium.git@search-features

You will then have the latest development that has not been merged into master.

@morteza3000
Copy link

for second command I got below error:

C:\Windows\system32>pip install git+https://github.com/ghandic/folium.git@search
-features
Collecting git+https://github.com/ghandic/folium.git@search-features
Cloning https://github.com/ghandic/folium.git (to search-features) to c:\users
\morteza.has\appdata\local\temp\pip-ktrr6c-build
Error [Error 2] The system cannot find the file specified while executing comm
and git clone -q https://github.com/ghandic/folium.git c:\users\morteza.has\appd
ata\local\temp\pip-ktrr6c-build
Cannot find command 'git'

@ghandic
Copy link
Contributor

ghandic commented Oct 18, 2017

Install git...

@morteza3000
Copy link

Thanks a lot, it works now. :)

image

@morteza3000
Copy link

I want to search based on popup labels, is it possible?
here is my code:

import folium
from folium import plugins

coordinates = [
[42.3581, -71.0636],
[42.82995815, -74.78991444],
[39.17929819, -78.56603306]]

m = folium.Map(location=[41.9, -97.3], zoom_start=4, tiles="cartodbpositron")
my_Circle1 = folium.CircleMarker(location=[42.3581, -71.0636], radius= 6, popup=str("one"), color='red', fill_opacity=0.7)
my_Circle2 = folium.CircleMarker(location=[42.82995815, -74.78991444], radius= 6, popup=str("two"), color='red', fill_opacity=0.7)
my_Circle3 = folium.CircleMarker(location=[39.17929819, -78.56603306], radius= 6, popup=str("three"), color='red', fill_opacity=0.7)
plugins.Search(coordinates, search_zoom=6, geom_type="Polygon").add_to(m)

m.add_child(my_Circle1)
m.add_child(my_Circle2)
m.add_child(my_Circle3)

m.save('example.html')

@ghandic
Copy link
Contributor

ghandic commented Oct 18, 2017

This is how it was meant to be done:

points = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "one"
},
"geometry": {
"type": "Point",
"coordinates": [-71.0636, 42.3581]
}
},
{
"type": "Feature",
"properties": {
"name": "two"
},
"geometry": {
"type": "Point",
"coordinates": [-74.78991444, 42.82995815]
}
},
{
"type": "Feature",
"properties": {
"name": "three"
},
"geometry": {
"type": "Point",
"coordinates": [-78.56603306, 39.17929819]
}
}
]
}

m = folium.Map(
location=[42.82995815, -74.78991444],
tiles = 'cartodbpositron',
zoom_start=4
)

plugins.Search(points, search_zoom=20, ).add_to(m)

m.save('example.html')

@ghandic
Copy link
Contributor

ghandic commented Oct 18, 2017

It needs a layer to search through and you haven't supplied it by just passing coordinates. This will be added in with assertations into the next commit - as well as being able to use GeoJsonCss to add popups and styling rather than the default marker - this would allow you to use a circle marker then

@ghandic
Copy link
Contributor

ghandic commented Oct 18, 2017

@morteza3000 I have implemented GeoJsonCss now, so re install folium as you did before if you want to use it

Here is your example with popups and a custom Icon:

points = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "One"
},
"geometry": {
"type": "Point",
"coordinates": [-71.0636, 42.3581]
},
"style": {
"icon": {
"iconUrl": "http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png",
"iconSize": [32, 32],
"iconAnchor": [16, 16]
}
},
"popupTemplate": "{name}"
},
{
"type": "Feature",
"properties": {
"name": "Two"
},
"geometry": {
"type": "Point",
"coordinates": [-74.78991444, 42.82995815]
},
"style": {
"icon": {
"iconUrl": "http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png",
"iconSize": [32, 32],
"iconAnchor": [16, 16]
}
},
"popupTemplate": "{name}"
},
{
"type": "Feature",
"properties": {
"name": "Three"
},
"geometry": {
"type": "Point",
"coordinates": [-78.56603306, 39.17929819]
},
"style": {
"icon": {
"iconUrl": "http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png",
"iconSize": [32, 32],
"iconAnchor": [16, 16]
}
},
"popupTemplate": "{name}"
}
]
}

m = folium.Map(
location=[42.82995815, -74.78991444],
tiles = 'cartodbpositron',
zoom_start=4
)

plugins.Search(points, search_zoom=20).add_to(m)

m.save('example.html')

@morteza3000
Copy link

I installed new one and it is working perfectly, Thanks.

@morteza3000
Copy link

@ghandic can you please help me with another issue? I want to have name as plain text instead of icon, is it possible?

@ghandic
Copy link
Contributor

ghandic commented Nov 12, 2017 via email

@sonerss
Copy link

sonerss commented Nov 23, 2017

Hi @ghandic, How can I install git? Can you help me?

@HSAG-2018
Copy link

Hi i could not run this

pip install git+https://github.com/ghandic/folium.git@search-features

can you send an updated link please

thxs

@ghandic
Copy link
Contributor

ghandic commented Jan 17, 2018

@HSAG-2018
pip install folium
as it is now in the master branch :)

@HSAG-2018
Copy link

still not working not sure if from server side or my laptop...
C:\UseCase3>pip install git+https://github.com/python-visualization/folium.git
Collecting git+https://github.com/python-visualization/folium.git
Cloning https://github.com/python-visualization/folium.git to c:\users\h\appdata\local\temp\pip-iqccyg6z-build
fatal: unable to access 'https://github.com/python-visualization/folium.git/': SSL certificate problem: unable to get local issuer certificate Command "git clone -q https://github.com/python-visualization/folium.git C:\Users\h\AppData\Local\Temp\pip-iqccyg6z-build" failed with error code 128 in None

@ghandic
Copy link
Contributor

ghandic commented Jan 17, 2018 via email

@HSAG-2018
Copy link

the cloning does not work so i tried to download the zip save it locally then run setup.py but this has no effect...shld replacing the whole existing folium directory with the new dowloaded one work? thxs

@ocefpaf
Copy link
Member

ocefpaf commented Jan 18, 2018

@ocefpaf how are we managing the Pypi repo?

There will be a new release once I can allocate some time to review and merge the open PRs.

@javaUtilScammer
Copy link

@ghandic Hello sorry to bother. I also want to add search functionality on a group of markers and display a pop up so I tried running the sample code with GeoJsonCss implemented which you provided above but it doesn't seem to be working. To be more specific, I can't get the pop ups to appear.

I've pip installed from the git (pip install git+https://github.com/python-visualization/folium.git) and am running on python 3.6.3.

Hope I can get it to work soon :) Thanks!

image

@ghandic
Copy link
Contributor

ghandic commented Mar 6, 2018

Hi @javaUtilScammer can you add your source code? I’ll take a look at this this morning if you can

@javaUtilScammer
Copy link

Hi @ghandic , didn't expect you to reply so fast but big thank you :) I wanted to implement the search functionality on another map I made using Folium a while back but I can't get the popups to appear on a sample.

It's pretty much the code you commented a while back plus imports. Thanks!

import folium
from folium import plugins
from folium import GeoJson

points = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "One"
},
"geometry": {
"type": "Point",
"coordinates": [-71.0636, 42.3581]
},
"style": {
"icon": {
"iconUrl": "http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png",
"iconSize": [32, 32],
"iconAnchor": [16, 16]
}
},
"popupTemplate": "{name}"
},
{
"type": "Feature",
"properties": {
"name": "Two"
},
"geometry": {
"type": "Point",
"coordinates": [-74.78991444, 42.82995815]
},
"style": {
"icon": {
"iconUrl": "http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png",
"iconSize": [32, 32],
"iconAnchor": [16, 16]
}
},
"popupTemplate": "{name}"
},
{
"type": "Feature",
"properties": {
"name": "Three"
},
"geometry": {
"type": "Point",
"coordinates": [-78.56603306, 39.17929819]
},
"style": {
"icon": {
"iconUrl": "http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png",
"iconSize": [32, 32],
"iconAnchor": [16, 16]
}
},
"popupTemplate": "{name}"
}
]
}

m = folium.Map(
location=[42.82995815, -74.78991444],
tiles = 'cartodbpositron',
zoom_start=4
)

plugins.Search(data=points, search_zoom=20,position="topright").add_to(m)
m.save('example.html')

@ghandic
Copy link
Contributor

ghandic commented Mar 6, 2018

@javaUtilScammer the GeoJsonCss part has not been merged into master on this repo, I just had a look at my fork vs this repo. @ocefpaf was there some problems with the GeoJsonCSS?

@ocefpaf
Copy link
Member

ocefpaf commented Mar 6, 2018

@ocefpaf was there some problems with the GeoJsonCSS?

My time to finish reviewing it. I do recall some issues when testing against the notebook gallery, but I cannot get back to that this month 😒

@ghandic
Copy link
Contributor

ghandic commented Mar 6, 2018

No problem @ocefpaf I saw some of the modifications you did and it simplifies it a lot, I'm happy to test some of those notebooks out.

Meanwhile @javaUtilScammer if you really need this purely as an output and not for long term you could use my forked repo https://github.com/ghandic/folium.git

Check back soon and I'm sure we'll be a few steps closer to having this up and running!

@ocefpaf
Copy link
Member

ocefpaf commented Mar 6, 2018

No problem @ocefpaf I saw some of the modifications you did and it simplifies it a lot, I'm happy to test some of those notebooks out.

Our testing is a little time consuming, we need to run all the notebooks from example folder and inspect them visually. If you could do that and fix the remaining issues that would be awesome!

@ghandic
Copy link
Contributor

ghandic commented Mar 6, 2018

@ocefpaf Just found a great little package which speeds that up, called nbmerge

# cd into the project folder
pip install nbmerge
# Merges all of the notebooks into one inside examples/_merged.ipynb
nbmerge --recursive -i -p ".*" -o examples/_merged.ipynb

@javaUtilScammer
Copy link

Meanwhile @javaUtilScammer if you really need this purely as an output and not for long term you could use my forked repo https://github.com/ghandic/folium.git

@ghandic will do that for now but will definitely come back when the merge is complete! Big thanks goes to you and @ocefpaf for the quick action :)

@ghandic
Copy link
Contributor

ghandic commented Mar 6, 2018

@ocefpaf

I spun up a docker container running jupyter notebook, cloned the repo and installed folium from your last commit hash on the pull request for #762

Then I merged all the notebooks and ran all cells, all of which seems to work perfectly. I haven't done any other testing than that, just checked the outputs and nothing errored

Docker-compose.yml

version: "2"
services:
  jupyter-notebook:
    build: .
    image: challisa/folium-notebook
    container_name: folium-notebook
    ports:
      - "8888:8888"
    volumes:
      - ~:/home/jovyan/work
    entrypoint: start-notebook.sh --NotebookApp.token='' --NotebookApp.iopub_data_rate_limit=1000000000000000 

Dockerfile

FROM jupyter/minimal-notebook

USER root

# libav-tools for matplotlib anim
RUN apt-get update && \
    apt-get install -y --no-install-recommends libav-tools && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Cartopy
RUN conda install --yes -c conda-forge cartopy

USER $NB_USER

RUN pip install  \
    'matplotlib' \
    'mplleaflet' \
    'geographiclib' \
    'gpxpy' \
    'altair' \
    'vincent' \
    'geopandas' \
    'pandas' \
    'widgetsnbextension' && \
    # Activate ipywidgets extension in the environment that runs the notebook server
    jupyter nbextension enable --py widgetsnbextension --sys-prefix 

USER $NB_USER

@Alcampopiano
Copy link

@ghandic Hi Andy, am I right that the popup functionality has not yet been merged with master?

@ms77grz
Copy link

ms77grz commented Mar 28, 2019

Hi guys! Can anyone help me with search function? How can I find a marker by typing "one" in searchbar?

from folium import plugins, Map, Marker, FeatureGroup, Popup, LayerControl
import webbrowser

m = Map(location=[43.25, 45.83333 ], zoom_start=10, zoom_control=False)

fg = FeatureGroup(name='VTK')

g1 = plugins.FeatureGroupSubGroup(fg, 'FTTB')
g2 = plugins.FeatureGroupSubGroup(fg, 'FTTH')
g3 = plugins.FeatureGroupSubGroup(fg, 'GPON')
Marker([43.119749, 45.562305], popup=Popup('one')).add_to(g1)
Marker([43.143048, 45.534987], popup=Popup('two')).add_to(g2)
Marker([43.131022, 45.546844], popup=Popup('three')).add_to(g3)
m.add_child(fg)
m.add_child(g1)
m.add_child(g2)
m.add_child(g3)

m.add_child(plugins.Search(fg))
m.add_child(LayerControl(collapsed=False))
m.save('test_map2.html')
webbrowser.open('test_map2.html')

@morteza3000
Copy link

@HSAG-2018
pip install folium
as it is now in the master branch :)

@ghandic , when I install it with (> pip install folium) below error appears for same examples.
can you please help?

AssertionError: Search can only index FeatureGroup, MarkerCluster, GeoJson, and TopoJson layers at this time.

@ghandic
Copy link
Contributor

ghandic commented Apr 6, 2019 via email

@morteza3000
Copy link

Yes, your example notebooks also don't work with provided data.

@ghandic
Copy link
Contributor

ghandic commented Apr 7, 2019 via email

@Conengmo
Copy link
Member

Conengmo commented Apr 7, 2019

@morteza3000 please open a new issue for your problem and include:

  • a stand-alone code snippet that results in the error message
  • the full error message
  • your folium version: print(folium.__version__)

@morteza3000
Copy link

@Conengmo , please be informed that below issue has been raised.
Problem with Folium search plugin (loading Geojosn Data) #1125

@chillinshunsui
Copy link

Thanks a lot, it works now. :)

image

hey, can you share the code

@ghandic
Copy link
Contributor

ghandic commented Mar 22, 2020

@GinoVillalpando
Copy link

is there a search plugin that works for FastMarkerCluster? I can't use MarkerCluster because it hinders the load performance because I'm using over 11,000 markers.

@morteza3000
Copy link

Check out the example notebooks https://github.com/python-visualization/folium/blob/master/examples/plugin-Search.ipynb

@ghandic , How we can style points in your example?

citygeo = folium.GeoJson(
pop_ranked_cities,
name='US Cities',
tooltip=folium.GeoJsonTooltip(
fields=['nameascii','pop_max'],
aliases=['','Population Max'],
localize=True)
).add_to(m)

how to add style_function to this in order to change points color or use custom icon?

@zoesn89
Copy link

zoesn89 commented Jun 3, 2020

Hey everyone,

as the docs say, the search plugin can index FeatureGroup, MarkerCluster, GeoJson and TopoJson layers.
It only worked for me on GeoJson layers so far.

Could you please provide an example for FeatureGroup or MarkerCluster without GeoJson?

Thanks in advance ;)

@PhilippMDoerner
Copy link

PhilippMDoerner commented Jun 25, 2020

Looking at the Search plugins python file, search is currently only implemented for GeoJson and TopoJson. I'm not entirely sure as to why the documentation states that it also implements the search for FeatureGroup and MarkerClusters. Maybe there were/are plans down the line for it (?)

@asemrbehat
Copy link

asemrbehat commented Mar 14, 2021

Check out the example notebooks https://github.com/python-visualization/folium/blob/master/examples/plugin-Search.ipynb

@ghandic , How we can style points in your example?

citygeo = folium.GeoJson(
pop_ranked_cities,
name='US Cities',
tooltip=folium.GeoJsonTooltip(
fields=['nameascii','pop_max'],
aliases=['','Population Max'],
localize=True)
).add_to(m)

how to add style_function to this in order to change points color or use custom icon?

Hi, is there any way to do it by HTML ?
I cant link the search box with the markers in order to search for them </>

@AtharvaShekatkar
Copy link

@ghandic hey, could you help me with using the search plugin for FeatureGroup layers? I've not been able to find a working solution for that anywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature request or idea about how to make folium better plugin This issue/PR is about an existing or new plugin
Projects
None yet
Development

No branches or pull requests