Skip to content

GeoPandas

openlayers.geopandas.OLAccessor

Create a new OLAccessor instance

Parameters:

Name Type Description Default
gdf GeoDataFrame

The GeoDataFrame, to which the openlayers extension is added

required
Source code in openlayers/geopandas.py
@pd.api.extensions.register_dataframe_accessor("ol")
class OLAccessor:
    """Create a new `OLAccessor` instance

    Args:
        gdf (gpd.GeoDataFrame): The `GeoDataFrame`,
            to which the `openlayers` extension is added
    """

    def __init__(self, gdf: gpd.GeoDataFrame) -> None:
        self._gdf = gdf
        self._default_style = default_style()

    def to_source(self) -> VectorSource:
        """Return data frame as OL vector source

        Returns:
            An OL vector source
        """
        feature_collection = gdf_to_geojson(self._gdf)
        source = VectorSource(geojson=feature_collection)
        return source

    def to_layer(
        self,
        style: FlatStyle | dict = None,
        layer_id: str = "geopandas",
        fit_bounds: bool = True,
        webgl: bool = True,
        **kwargs,
    ) -> VectorLayer | WebGLVectorLayer:
        """Return data frame as OL vector layer

        Returns:
            An OL vector layer
        """
        style = style or self._default_style
        layer_class = WebGLVectorLayer if webgl else VectorLayer
        layer = layer_class(
            id=layer_id,
            style=style,
            fit_bounds=fit_bounds,
            source=self.to_source(),
            **kwargs,
        )
        return layer

    def explore(
        self,
        style: FlatStyle | dict = None,
        tooltip: bool | str = True,
        controls: list[ControlT | dict] = None,
        layer_id: str = "geopandas",
        fit_bounds: bool = True,
        webgl: bool = True,
        **kwargs,
    ) -> MapWidget:
        """Create a vector layer and add it to a new `Map` instance

        Args:
            style (FlatStyle | dict): The style applied to the layer.
                If `None`, a default style is used
            tooltip (bool | str): Whether a tooltip should be added to the map.
                Either `True` to add a default tooltip,
                or a mustache template string to add a custom tooltip
            controls (list[ControlT | dict]): Controls initially added to the map
            layer_id (str): The ID of the layer
            fit_bounds (bool): Whether to fit bounds
            webgl (bool): Whether the layer should be added to the map
                as `WebGLVectorLayer` or as `VectorLayer`

        Returns:
            A new `MapWidget` instance
        """
        layer = self.to_layer(
            style=style, layer_id=layer_id, fit_bounds=fit_bounds, webgl=webgl
        )

        m = MapWidget(controls=controls, **kwargs)
        m.add_layer(layer)
        if isinstance(tooltip, str):
            m.add_tooltip(tooltip)
        elif tooltip:
            m.add_default_tooltip()

        return m

    def color_category(self, column: str) -> Self:
        self._gdf[COLOR_COLUMN] = Color.random_hex_colors_by_category(self._gdf[column])
        self._default_style.fill_color = ["get", COLOR_COLUMN]
        self._default_style.circle_fill_color = ["get", COLOR_COLUMN]
        return self

    def icon(self, icon_src: str, icon_scale: float = 1, **kwargs) -> Self:
        # TODO: Use style model instead
        self._default_style = {"icon-src": icon_src, "icon-scale": icon_scale} | kwargs
        return self

explore(style=None, tooltip=True, controls=None, layer_id='geopandas', fit_bounds=True, webgl=True, **kwargs)

Create a vector layer and add it to a new Map instance

Parameters:

Name Type Description Default
style FlatStyle | dict

The style applied to the layer. If None, a default style is used

None
tooltip bool | str

Whether a tooltip should be added to the map. Either True to add a default tooltip, or a mustache template string to add a custom tooltip

True
controls list[ControlT | dict]

Controls initially added to the map

None
layer_id str

The ID of the layer

'geopandas'
fit_bounds bool

Whether to fit bounds

True
webgl bool

Whether the layer should be added to the map as WebGLVectorLayer or as VectorLayer

True

Returns:

Type Description
MapWidget

A new MapWidget instance

Source code in openlayers/geopandas.py
def explore(
    self,
    style: FlatStyle | dict = None,
    tooltip: bool | str = True,
    controls: list[ControlT | dict] = None,
    layer_id: str = "geopandas",
    fit_bounds: bool = True,
    webgl: bool = True,
    **kwargs,
) -> MapWidget:
    """Create a vector layer and add it to a new `Map` instance

    Args:
        style (FlatStyle | dict): The style applied to the layer.
            If `None`, a default style is used
        tooltip (bool | str): Whether a tooltip should be added to the map.
            Either `True` to add a default tooltip,
            or a mustache template string to add a custom tooltip
        controls (list[ControlT | dict]): Controls initially added to the map
        layer_id (str): The ID of the layer
        fit_bounds (bool): Whether to fit bounds
        webgl (bool): Whether the layer should be added to the map
            as `WebGLVectorLayer` or as `VectorLayer`

    Returns:
        A new `MapWidget` instance
    """
    layer = self.to_layer(
        style=style, layer_id=layer_id, fit_bounds=fit_bounds, webgl=webgl
    )

    m = MapWidget(controls=controls, **kwargs)
    m.add_layer(layer)
    if isinstance(tooltip, str):
        m.add_tooltip(tooltip)
    elif tooltip:
        m.add_default_tooltip()

    return m

to_layer(style=None, layer_id='geopandas', fit_bounds=True, webgl=True, **kwargs)

Return data frame as OL vector layer

Returns:

Type Description
VectorLayer | WebGLVectorLayer

An OL vector layer

Source code in openlayers/geopandas.py
def to_layer(
    self,
    style: FlatStyle | dict = None,
    layer_id: str = "geopandas",
    fit_bounds: bool = True,
    webgl: bool = True,
    **kwargs,
) -> VectorLayer | WebGLVectorLayer:
    """Return data frame as OL vector layer

    Returns:
        An OL vector layer
    """
    style = style or self._default_style
    layer_class = WebGLVectorLayer if webgl else VectorLayer
    layer = layer_class(
        id=layer_id,
        style=style,
        fit_bounds=fit_bounds,
        source=self.to_source(),
        **kwargs,
    )
    return layer

to_source()

Return data frame as OL vector source

Returns:

Type Description
VectorSource

An OL vector source

Source code in openlayers/geopandas.py
def to_source(self) -> VectorSource:
    """Return data frame as OL vector source

    Returns:
        An OL vector source
    """
    feature_collection = gdf_to_geojson(self._gdf)
    source = VectorSource(geojson=feature_collection)
    return source

openlayers.geopandas.GeoDataFrame

Bases: GeoDataFrame

Source code in openlayers/geopandas.py
class GeoDataFrame(gpd.GeoDataFrame):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.ol = OLAccessor(self)