Skip to content

Problem with Folium search plugin (loading Geojosn Data) #1125

Closed

Description

@morteza3000

Please add a code sample or a nbviewer link, copy-pastable if possible

import folium
from folium import plugins

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')

Problem description

I'm facing with below error when running above code.
AssertionError: Search can only index FeatureGroup, MarkerCluster, GeoJson, and TopoJson layers at this time.

Expected Output

Output of folium.__version__

0.8.3

@Conengmo
@ghandic

Activity

  1. Conengmo commented on Apr 7, 2019

    @Conengmo
    Member

    You pass a dictionary to Search, but it needs a FeatureGroup, MarkerCluster, GeoJson or TopoJson object instead. Check out the example notebook:
    https://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/plugin-Search.ipynb

    In the last cell you can see first a folium.GeoJson object is created, which is passed to folium.plugins.Search.

    geojson_obj = folium.GeoJson(points).add_to(m)
    folium.plugins.Search(geojson_obj).add_to(m)
    
  2. morteza3000 commented on Apr 7, 2019

    @morteza3000
    Author

    Thanks a lot @Conengmo, my issue solved with below modification.

    import folium
    from folium import plugins
    from folium.plugins import Search
    
    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
    )
    
    style_one = lambda x: {'fillColor': '#ffdc30'}
    geojson_obj = folium.GeoJson(points, style_function=style_one).add_to(m)
    
    statesearch = Search(layer=geojson_obj,
                         geom_type='Point',
                         placeholder="Search",
                         collapsed=True,
                         search_label='name',
                         search_zoom=14,
                         position='topright'
                        ).add_to(m)
    
    m.save('example.html')
  3. morteza3000 commented on Apr 23, 2020

    @morteza3000
    Author

    @Conengmo , can you please let me know that how can I use custom marker with this folium.GeoJson?

    style_one = lambda x: {'fillColor': '#ffdc30'} doesn't change the color of markers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions