geemap/geemap.py
def add_labels(
        self,
        data,
        column,
        font_size="12pt",
        font_color="black",
        font_family="arial",
        font_weight="normal",
        x="longitude",
        y="latitude",
        draggable=True,
        layer_name="Labels",
        **kwargs,
    ):
        """Adds a label layer to the map. Reference: https://ipyleaflet.readthedocs.io/en/latest/api_reference/divicon.html

        Args:
            data (pd.DataFrame | ee.FeatureCollection): The input data to label.
            column (str): The column name of the data to label.
            font_size (str, optional): The font size of the labels. Defaults to "12pt".
            font_color (str, optional): The font color of the labels. Defaults to "black".
            font_family (str, optional): The font family of the labels. Defaults to "arial".
            font_weight (str, optional): The font weight of the labels, can be normal, bold. Defaults to "normal".
            x (str, optional): The column name of the longitude. Defaults to "longitude".
            y (str, optional): The column name of the latitude. Defaults to "latitude".
            draggable (bool, optional): Whether the labels are draggable. Defaults to True.
            layer_name (str, optional): Layer name to use. Defaults to "Labels".

        """
        import warnings
        import pandas as pd

        warnings.filterwarnings("ignore")

        if isinstance(data, ee.FeatureCollection):
            centroids = vector_centroids(data)
            df = ee_to_df(centroids)
        elif isinstance(data, pd.DataFrame):
            df = data
        elif isinstance(data, str):
            ext = os.path.splitext(data)[1]
            if ext == ".csv":
                df = pd.read_csv(data)
            elif ext in [".geojson", ".json", ".shp", ".gpkg"]:
                try:
                    import geopandas as gpd

                    df = gpd.read_file(data)
                    df[x] = df.centroid.x
                    df[y] = df.centroid.y
                except Exception as _:
                    print("geopandas is required to read geojson.")
                    return

        else:
            raise ValueError("data must be a DataFrame or an ee.FeatureCollection.")

        if column not in df.columns:
            raise ValueError(f"column must be one of {', '.join(df.columns)}.")
        if x not in df.columns:
            raise ValueError(f"column must be one of {', '.join(df.columns)}.")
        if y not in df.columns:
            raise ValueError(f"column must be one of {', '.join(df.columns)}.")

        try:
            size = int(font_size.replace("pt", ""))
        except:
            raise ValueError("font_size must be something like '10pt'")

        labels = []
        for index in df.index:
            html = f'<div style="font-size: {font_size};color:{font_color};font-family:{font_family};font-weight: {font_weight}">{df[column][index]}</div>'
            marker = ipyleaflet.Marker(
                location=[df[y][index], df[x][index]],
                icon=ipyleaflet.DivIcon(
                    icon_size=(1, 1),
                    icon_anchor=(size, size),
                    html=html,
                    **kwargs,
                ),
                draggable=draggable,
            )
            labels.append(marker)
        layer_group = ipyleaflet.LayerGroup(layers=labels, name=layer_name)
        self.add_layer(layer_group)
        self.labels = layer_group
Adds a label layer to the map. Reference: https://ipyleaflet.readthedocs.io/en/latest/api_reference/divicon.html

        Args:
            data (pd.DataFrame | ee.FeatureCollection): The input data to label.
            column (str): The column name of the data to label.
            font_size (str, optional): The font size of the labels. Defaults to "12pt".
            font_color (str, optional): The font color of the labels. Defaults to "black".
            font_family (str, optional): The font family of the labels. Defaults to "arial".
            font_weight (str, optional): The font weight of the labels, can be normal, bold. Defaults to "normal".
            x (str, optional): The column name of the longitude. Defaults to "longitude".
            y (str, optional): The column name of the latitude. Defaults to "latitude".
            draggable (bool, optional): Whether the labels are draggable. Defaults to True.
            layer_name (str, optional): Layer name to use. Defaults to "Labels".

        
Adds a label layer to the map.
Adds a label layer to the map .
def add_labels(
        self,
        data,
        column,
        font_size="12pt",
        font_color="black",
        font_family="arial",
        font_weight="normal",
        x="longitude",
        y="latitude",
        draggable=True,
        layer_name="Labels",
        **kwargs,
    ):
        import warnings
        import pandas as pd
        warnings.filterwarnings("ignore")
        if isinstance(data, ee.FeatureCollection):
            centroids = vector_centroids(data)
            df = ee_to_df(centroids)
        elif isinstance(data, pd.DataFrame):
            df = data
        elif isinstance(data, str):
            ext = os.path.splitext(data)[1]
            if ext == ".csv":
                df = pd.read_csv(data)
            elif ext in [".geojson", ".json", ".shp", ".gpkg"]:
                try:
                    import geopandas as gpd
                    df = gpd.read_file(data)
                    df[x] = df.centroid.x
                    df[y] = df.centroid.y
                except Exception as _:
                    print("geopandas is required to read geojson.")
                    return
        else:
            raise ValueError("data must be a DataFrame or an ee.FeatureCollection.")
        if column not in df.columns:
            raise ValueError(f"column must be one of {', '.join(df.columns)}.")
        if x not in df.columns:
            raise ValueError(f"column must be one of {', '.join(df.columns)}.")
        if y not in df.columns:
            raise ValueError(f"column must be one of {', '.join(df.columns)}.")
        try:
            size = int(font_size.replace("pt", ""))
        except:
            raise ValueError("font_size must be something like '10pt'")
        labels = []
        for index in df.index:
            html = f'<div style="font-size: {font_size};color:{font_color};font-family:{font_family};font-weight: {font_weight}">{df[column][index]}</div>'
            marker = ipyleaflet.Marker(
                location=[df[y][index], df[x][index]],
                icon=ipyleaflet.DivIcon(
                    icon_size=(1, 1),
                    icon_anchor=(size, size),
                    html=html,
                    **kwargs,
                ),
                draggable=draggable,
            )
            labels.append(marker)
        layer_group = ipyleaflet.LayerGroup(layers=labels, name=layer_name)
        self.add_layer(layer_group)
        self.labels = layer_group
def add_labels ( self , data , column , font_size = "12pt" , font_color = "black" , font_family = "arial" , font_weight = "normal" , x = "longitude" , y = "latitude" , draggable = True , layer_name = "Labels" , ** kwargs , ) : import warnings import pandas as pd warnings . filterwarnings ( "ignore" ) if isinstance ( data , ee . FeatureCollection ) : centroids = vector_centroids ( data ) df = ee_to_df ( centroids ) elif isinstance ( data , pd . DataFrame ) : df = data elif isinstance ( data , str ) : ext = os . path . splitext ( data ) [ 1 ] if ext == ".csv" : df = pd . read_csv ( data ) elif ext in [ ".geojson" , ".json" , ".shp" , ".gpkg" ] : try : import geopandas as gpd df = gpd . read_file ( data ) df [ x ] = df . centroid . x df [ y ] = df . centroid . y except Exception as _ : print ( "geopandas is required to read geojson." ) return else : raise ValueError ( "data must be a DataFrame or an ee.FeatureCollection." ) if column not in df . columns : raise ValueError ( f"column must be one of {', '.join(df.columns)}." ) if x not in df . columns : raise ValueError ( f"column must be one of {', '.join(df.columns)}." ) if y not in df . columns : raise ValueError ( f"column must be one of {', '.join(df.columns)}." ) try : size = int ( font_size . replace ( "pt" , "" ) ) except : raise ValueError ( "font_size must be something like '10pt'" ) labels = [ ] for index in df . index : html = f'<div style="font-size: {font_size};color:{font_color};font-family:{font_family};font-weight: {font_weight}">{df[column][index]}</div>' marker = ipyleaflet . Marker ( location = [ df [ y ] [ index ] , df [ x ] [ index ] ] , icon = ipyleaflet . DivIcon ( icon_size = ( 1 , 1 ) , icon_anchor = ( size , size ) , html = html , ** kwargs , ) , draggable = draggable , ) labels . append ( marker ) layer_group = ipyleaflet . LayerGroup ( layers = labels , name = layer_name ) self . add_layer ( layer_group ) self . labels = layer_group
Adds a label layer to the map.
Adds a label layer to the map .
"""Adds a label layer to the map. Reference: https://ipyleaflet.readthedocs.io/en/latest/api_reference/divicon.html

        Args:
            data (pd.DataFrame | ee.FeatureCollection): The input data to label.
            column (str): The column name of the data to label.
            font_size (str, optional): The font size of the labels. Defaults to "12pt".
            font_color (str, optional): The font color of the labels. Defaults to "black".
            font_family (str, optional): The font family of the labels. Defaults to "arial".
            font_weight (str, optional): The font weight of the labels, can be normal, bold. Defaults to "normal".
            x (str, optional): The column name of the longitude. Defaults to "longitude".
            y (str, optional): The column name of the latitude. Defaults to "latitude".
            draggable (bool, optional): Whether the labels are draggable. Defaults to True.
            layer_name (str, optional): Layer name to use. Defaults to "Labels".

        """
self
data
The input data to label.
The input data to label .
column
The column name of the data to label.
The column name of the data to label .
font_size
The font size of the labels. Defaults to "12pt".
The font size of the labels . Defaults to " 12pt " .
"12pt"
font_color
The font color of the labels. Defaults to "black".
The font color of the labels . Defaults to " black " .
"black"
font_family
The font family of the labels. Defaults to "arial".
The font family of the labels . Defaults to " arial " .
"arial"
font_weight
The font weight of the labels, can be normal, bold. Defaults to "normal".
The font weight of the labels can be normal bold . Defaults to " normal " .
"normal"
x
The column name of the longitude. Defaults to "longitude".
The column name of the longitude . Defaults to " longitude " .
"longitude"
y
The column name of the latitude. Defaults to "latitude".
The column name of the latitude . Defaults to " latitude " .
"latitude"
draggable
Whether the labels are draggable. Defaults to True.
Whether the labels are draggable . Defaults to True .
True
layer_name
Layer name to use. Defaults to "Labels".
Layer name to use . Defaults to " Labels " .
"Labels"