geemap/geemap.py
def add_gdf(
        self,
        gdf,
        layer_name="Untitled",
        style={},
        hover_style={},
        style_callback=None,
        fill_colors=["black"],
        info_mode="on_hover",
        zoom_to_layer=True,
    ):
        """Adds a GeoDataFrame to the map.

        Args:
            gdf (GeoDataFrame): A GeoPandas GeoDataFrame.
            layer_name (str, optional): The layer name to be used.. Defaults to "Untitled".
            style (dict, optional): A dictionary specifying the style to be used. Defaults to {}.
            hover_style (dict, optional): Hover style dictionary. Defaults to {}.
            style_callback (function, optional): Styling function that is called for each feature, and should return the feature style. This styling function takes the feature as argument. Defaults to None.
            fill_colors (list, optional): The random colors to use for filling polygons. Defaults to ["black"].
            info_mode (str, optional): Displays the attributes by either on_hover or on_click. Any value other than "on_hover" or "on_click" will be treated as None. Defaults to "on_hover".
            zoom_to_layer (bool, optional): Whether to zoom to the layer.
        """
        import random

        data = gdf_to_geojson(gdf, epsg="4326")

        if not style:
            style = {
                # "stroke": True,
                "color": "#000000",
                "weight": 1,
                "opacity": 1,
                # "fill": True,
                # "fillColor": "#ffffff",
                "fillOpacity": 0.1,
                # "dashArray": "9"
                # "clickable": True,
            }
        elif "weight" not in style:
            style["weight"] = 1

        if not hover_style:
            hover_style = {"weight": style["weight"] + 1, "fillOpacity": 0.5}

        def random_color(feature):
            return {
                "color": "black",
                "fillColor": random.choice(fill_colors),
            }

        toolbar_button = widgets.ToggleButton(
            value=True,
            tooltip="Toolbar",
            icon="info",
            layout=widgets.Layout(
                width="28px", height="28px", padding="0px 0px 0px 4px"
            ),
        )

        close_button = widgets.ToggleButton(
            value=False,
            tooltip="Close the tool",
            icon="times",
            # button_style="primary",
            layout=widgets.Layout(
                height="28px", width="28px", padding="0px 0px 0px 4px"
            ),
        )

        html = widgets.HTML()
        html.layout.margin = "0px 10px 0px 10px"
        html.layout.max_height = "250px"
        html.layout.max_width = "250px"

        output_widget = widgets.VBox(
            [widgets.HBox([toolbar_button, close_button]), html]
        )
        info_control = ipyleaflet.WidgetControl(
            widget=output_widget, position="bottomright"
        )

        if info_mode in ["on_hover", "on_click"]:
            self.add_control(info_control)

        def toolbar_btn_click(change):
            if change["new"]:
                close_button.value = False
                output_widget.children = [
                    widgets.VBox([widgets.HBox([toolbar_button, close_button]), html])
                ]
            else:
                output_widget.children = [widgets.HBox([toolbar_button, close_button])]

        toolbar_button.observe(toolbar_btn_click, "value")

        def close_btn_click(change):
            if change["new"]:
                toolbar_button.value = False
                if info_control in self.controls:
                    self.remove_control(info_control)
                output_widget.close()

        close_button.observe(close_btn_click, "value")

        def update_html(feature, **kwargs):

            value = [
                "<h5><b>{}: </b>{}</h5>".format(prop, feature["properties"][prop])
                for prop in feature["properties"].keys()
            ][:-1]

            value = """{}""".format("".join(value))
            html.value = value

        if style_callback is None:
            style_callback = random_color

        geojson = ipyleaflet.GeoJSON(
            data=data,
            style=style,
            hover_style=hover_style,
            style_callback=style_callback,
            name=layer_name,
        )

        if info_mode == "on_hover":
            geojson.on_hover(update_html)
        elif info_mode == "on_click":
            geojson.on_click(update_html)

        self.add_layer(geojson)

        if zoom_to_layer:
            import numpy as np

            bounds = gdf.to_crs(epsg="4326").bounds
            west = np.min(bounds["minx"])
            south = np.min(bounds["miny"])
            east = np.max(bounds["maxx"])
            north = np.max(bounds["maxy"])
            self.fit_bounds([[south, east], [north, west]])
Adds a GeoDataFrame to the map.

        Args:
            gdf (GeoDataFrame): A GeoPandas GeoDataFrame.
            layer_name (str, optional): The layer name to be used.. Defaults to "Untitled".
            style (dict, optional): A dictionary specifying the style to be used. Defaults to {}.
            hover_style (dict, optional): Hover style dictionary. Defaults to {}.
            style_callback (function, optional): Styling function that is called for each feature, and should return the feature style. This styling function takes the feature as argument. Defaults to None.
            fill_colors (list, optional): The random colors to use for filling polygons. Defaults to ["black"].
            info_mode (str, optional): Displays the attributes by either on_hover or on_click. Any value other than "on_hover" or "on_click" will be treated as None. Defaults to "on_hover".
            zoom_to_layer (bool, optional): Whether to zoom to the layer.
        
Adds a GeoDataFrame to the map.
Adds a GeoDataFrame to the map .
def add_gdf(
        self,
        gdf,
        layer_name="Untitled",
        style={},
        hover_style={},
        style_callback=None,
        fill_colors=["black"],
        info_mode="on_hover",
        zoom_to_layer=True,
    ):
        import random
        data = gdf_to_geojson(gdf, epsg="4326")
        if not style:
            style = {
                "color": "#000000",
                "weight": 1,
                "opacity": 1,
                "fillOpacity": 0.1,
            }
        elif "weight" not in style:
            style["weight"] = 1
        if not hover_style:
            hover_style = {"weight": style["weight"] + 1, "fillOpacity": 0.5}
        def random_color(feature):
            return {
                "color": "black",
                "fillColor": random.choice(fill_colors),
            }
        toolbar_button = widgets.ToggleButton(
            value=True,
            tooltip="Toolbar",
            icon="info",
            layout=widgets.Layout(
                width="28px", height="28px", padding="0px 0px 0px 4px"
            ),
        )
        close_button = widgets.ToggleButton(
            value=False,
            tooltip="Close the tool",
            icon="times",
            layout=widgets.Layout(
                height="28px", width="28px", padding="0px 0px 0px 4px"
            ),
        )
        html = widgets.HTML()
        html.layout.margin = "0px 10px 0px 10px"
        html.layout.max_height = "250px"
        html.layout.max_width = "250px"
        output_widget = widgets.VBox(
            [widgets.HBox([toolbar_button, close_button]), html]
        )
        info_control = ipyleaflet.WidgetControl(
            widget=output_widget, position="bottomright"
        )
        if info_mode in ["on_hover", "on_click"]:
            self.add_control(info_control)
        def toolbar_btn_click(change):
            if change["new"]:
                close_button.value = False
                output_widget.children = [
                    widgets.VBox([widgets.HBox([toolbar_button, close_button]), html])
                ]
            else:
                output_widget.children = [widgets.HBox([toolbar_button, close_button])]
        toolbar_button.observe(toolbar_btn_click, "value")
        def close_btn_click(change):
            if change["new"]:
                toolbar_button.value = False
                if info_control in self.controls:
                    self.remove_control(info_control)
                output_widget.close()
        close_button.observe(close_btn_click, "value")
        def update_html(feature, **kwargs):
            value = [
                "<h5><b>{}: </b>{}</h5>".format(prop, feature["properties"][prop])
                for prop in feature["properties"].keys()
            ][:-1]
            value = """{}""".format("".join(value))
            html.value = value
        if style_callback is None:
            style_callback = random_color
        geojson = ipyleaflet.GeoJSON(
            data=data,
            style=style,
            hover_style=hover_style,
            style_callback=style_callback,
            name=layer_name,
        )
        if info_mode == "on_hover":
            geojson.on_hover(update_html)
        elif info_mode == "on_click":
            geojson.on_click(update_html)
        self.add_layer(geojson)
        if zoom_to_layer:
            import numpy as np
            bounds = gdf.to_crs(epsg="4326").bounds
            west = np.min(bounds["minx"])
            south = np.min(bounds["miny"])
            east = np.max(bounds["maxx"])
            north = np.max(bounds["maxy"])
            self.fit_bounds([[south, east], [north, west]])
def add_gdf ( self , gdf , layer_name = "Untitled" , style = { } , hover_style = { } , style_callback = None , fill_colors = [ "black" ] , info_mode = "on_hover" , zoom_to_layer = True , ) : import random data = gdf_to_geojson ( gdf , epsg = "4326" ) if not style : style = { "color" : "#000000" , "weight" : 1 , "opacity" : 1 , "fillOpacity" : 0.1 , } elif "weight" not in style : style [ "weight" ] = 1 if not hover_style : hover_style = { "weight" : style [ "weight" ] + 1 , "fillOpacity" : 0.5 } def random_color ( feature ) : return { "color" : "black" , "fillColor" : random . choice ( fill_colors ) , } toolbar_button = widgets . ToggleButton ( value = True , tooltip = "Toolbar" , icon = "info" , layout = widgets . Layout ( width = "28px" , height = "28px" , padding = "0px 0px 0px 4px" ) , ) close_button = widgets . ToggleButton ( value = False , tooltip = "Close the tool" , icon = "times" , layout = widgets . Layout ( height = "28px" , width = "28px" , padding = "0px 0px 0px 4px" ) , ) html = widgets . HTML ( ) html . layout . margin = "0px 10px 0px 10px" html . layout . max_height = "250px" html . layout . max_width = "250px" output_widget = widgets . VBox ( [ widgets . HBox ( [ toolbar_button , close_button ] ) , html ] ) info_control = ipyleaflet . WidgetControl ( widget = output_widget , position = "bottomright" ) if info_mode in [ "on_hover" , "on_click" ] : self . add_control ( info_control ) def toolbar_btn_click ( change ) : if change [ "new" ] : close_button . value = False output_widget . children = [ widgets . VBox ( [ widgets . HBox ( [ toolbar_button , close_button ] ) , html ] ) ] else : output_widget . children = [ widgets . HBox ( [ toolbar_button , close_button ] ) ] toolbar_button . observe ( toolbar_btn_click , "value" ) def close_btn_click ( change ) : if change [ "new" ] : toolbar_button . value = False if info_control in self . controls : self . remove_control ( info_control ) output_widget . close ( ) close_button . observe ( close_btn_click , "value" ) def update_html ( feature , ** kwargs ) : value = [ "<h5><b>{}: </b>{}</h5>" . format ( prop , feature [ "properties" ] [ prop ] ) for prop in feature [ "properties" ] . keys ( ) ] [ : - 1 ] value = """{}""" . format ( "" . join ( value ) ) html . value = value if style_callback is None : style_callback = random_color geojson = ipyleaflet . GeoJSON ( data = data , style = style , hover_style = hover_style , style_callback = style_callback , name = layer_name , ) if info_mode == "on_hover" : geojson . on_hover ( update_html ) elif info_mode == "on_click" : geojson . on_click ( update_html ) self . add_layer ( geojson ) if zoom_to_layer : import numpy as np bounds = gdf . to_crs ( epsg = "4326" ) . bounds west = np . min ( bounds [ "minx" ] ) south = np . min ( bounds [ "miny" ] ) east = np . max ( bounds [ "maxx" ] ) north = np . max ( bounds [ "maxy" ] ) self . fit_bounds ( [ [ south , east ] , [ north , west ] ] )
Adds a GeoDataFrame to the map.
Adds a GeoDataFrame to the map .
"""Adds a GeoDataFrame to the map.

        Args:
            gdf (GeoDataFrame): A GeoPandas GeoDataFrame.
            layer_name (str, optional): The layer name to be used.. Defaults to "Untitled".
            style (dict, optional): A dictionary specifying the style to be used. Defaults to {}.
            hover_style (dict, optional): Hover style dictionary. Defaults to {}.
            style_callback (function, optional): Styling function that is called for each feature, and should return the feature style. This styling function takes the feature as argument. Defaults to None.
            fill_colors (list, optional): The random colors to use for filling polygons. Defaults to ["black"].
            info_mode (str, optional): Displays the attributes by either on_hover or on_click. Any value other than "on_hover" or "on_click" will be treated as None. Defaults to "on_hover".
            zoom_to_layer (bool, optional): Whether to zoom to the layer.
        """ # "stroke": True,
 # "fill": True,
 # "fillColor": "#ffffff",
 # "dashArray": "9"
 # "clickable": True,
 # button_style="primary",

self
gdf
A GeoPandas GeoDataFrame.
A GeoPandas GeoDataFrame .
layer_name
The layer name to be used.. Defaults to "Untitled".
The layer name to be used .. Defaults to " Untitled " .
"Untitled"
style
A dictionary specifying the style to be used. Defaults to {}.
A dictionary specifying the style to be used . Defaults to {} .
{}
hover_style
Hover style dictionary. Defaults to {}.
Hover style dictionary . Defaults to {} .
{}
style_callback
Styling function that is called for each feature, and should return the feature style. This styling function takes the feature as argument. Defaults to None.
Styling function that is called for each feature and should return the feature style . This styling function takes the feature as argument . Defaults to None .
None
fill_colors
The random colors to use for filling polygons. Defaults to ["black"].
The random colors to use for filling polygons . Defaults to [ " black " ] .
["black"]
info_mode
Displays the attributes by either on_hover or on_click. Any value other than "on_hover" or "on_click" will be treated as None. Defaults to "on_hover".
Displays the attributes by either on_hover or on_click . Any value other than " on_hover " or " on_click " will be treated as None . Defaults to " on_hover " .
"on_hover"
zoom_to_layer
Whether to zoom to the layer.
Whether to zoom to the layer .