geemap/geemap.py
def add_geojson(
        self,
        in_geojson,
        layer_name="Untitled",
        style={},
        hover_style={},
        style_callback=None,
        fill_colors=["black"],
        info_mode="on_hover",
    ):
        """Adds a GeoJSON file to the map.

        Args:
            in_geojson (str | dict): The file path or http URL to the input GeoJSON or a dictionary containing the geojson.
            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".
        Raises:
            FileNotFoundError: The provided GeoJSON file could not be found.
        """
        import json
        import random
        import requests

        style_callback_only = False

        if len(style) == 0 and style_callback is not None:
            style_callback_only = True

        try:

            if isinstance(in_geojson, str):

                if in_geojson.startswith("http"):
                    data = requests.get(in_geojson).json()
                else:
                    in_geojson = os.path.abspath(in_geojson)
                    if not os.path.exists(in_geojson):
                        raise FileNotFoundError(
                            "The provided GeoJSON file could not be found."
                        )

                    with open(in_geojson, encoding="utf-8") as f:
                        data = json.load(f)
            elif isinstance(in_geojson, dict):
                data = in_geojson
            else:
                raise TypeError("The input geojson must be a type of str or dict.")
        except Exception as e:
            raise Exception(e)

        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

        if style_callback_only:
            geojson = ipyleaflet.GeoJSON(
                data=data,
                hover_style=hover_style,
                style_callback=style_callback,
                name=layer_name,
            )
        else:
            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)
Adds a GeoJSON file to the map.

        Args:
            in_geojson (str | dict): The file path or http URL to the input GeoJSON or a dictionary containing the geojson.
            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".
        Raises:
            FileNotFoundError: The provided GeoJSON file could not be found.
        
Adds a GeoJSON file to the map.
Adds a GeoJSON file to the map .
def add_geojson(
        self,
        in_geojson,
        layer_name="Untitled",
        style={},
        hover_style={},
        style_callback=None,
        fill_colors=["black"],
        info_mode="on_hover",
    ):
        import json
        import random
        import requests
        style_callback_only = False
        if len(style) == 0 and style_callback is not None:
            style_callback_only = True
        try:
            if isinstance(in_geojson, str):
                if in_geojson.startswith("http"):
                    data = requests.get(in_geojson).json()
                else:
                    in_geojson = os.path.abspath(in_geojson)
                    if not os.path.exists(in_geojson):
                        raise FileNotFoundError(
                            "The provided GeoJSON file could not be found."
                        )
                    with open(in_geojson, encoding="utf-8") as f:
                        data = json.load(f)
            elif isinstance(in_geojson, dict):
                data = in_geojson
            else:
                raise TypeError("The input geojson must be a type of str or dict.")
        except Exception as e:
            raise Exception(e)
        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
        if style_callback_only:
            geojson = ipyleaflet.GeoJSON(
                data=data,
                hover_style=hover_style,
                style_callback=style_callback,
                name=layer_name,
            )
        else:
            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)
def add_geojson ( self , in_geojson , layer_name = "Untitled" , style = { } , hover_style = { } , style_callback = None , fill_colors = [ "black" ] , info_mode = "on_hover" , ) : import json import random import requests style_callback_only = False if len ( style ) == 0 and style_callback is not None : style_callback_only = True try : if isinstance ( in_geojson , str ) : if in_geojson . startswith ( "http" ) : data = requests . get ( in_geojson ) . json ( ) else : in_geojson = os . path . abspath ( in_geojson ) if not os . path . exists ( in_geojson ) : raise FileNotFoundError ( "The provided GeoJSON file could not be found." ) with open ( in_geojson , encoding = "utf-8" ) as f : data = json . load ( f ) elif isinstance ( in_geojson , dict ) : data = in_geojson else : raise TypeError ( "The input geojson must be a type of str or dict." ) except Exception as e : raise Exception ( e ) 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 if style_callback_only : geojson = ipyleaflet . GeoJSON ( data = data , hover_style = hover_style , style_callback = style_callback , name = layer_name , ) else : 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 )
Adds a GeoJSON file to the map.
Adds a GeoJSON file to the map .
"""Adds a GeoJSON file to the map.

        Args:
            in_geojson (str | dict): The file path or http URL to the input GeoJSON or a dictionary containing the geojson.
            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".
        Raises:
            FileNotFoundError: The provided GeoJSON file could not be found.
        """ # "stroke": True,
 # "fill": True,
 # "fillColor": "#ffffff",
 # "dashArray": "9"
 # "clickable": True,
 # button_style="primary",

The provided GeoJSON file could not be found.
The provided GeoJSON file could not be found .
FileNotFoundError
self
in_geojson
The file path or http URL to the input GeoJSON or a dictionary containing the geojson.
The file path or http URL to the input GeoJSON or a dictionary containing the geojson .
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"