Skip to content

SlippyMapRegionalizer

srai.regionalizers.SlippyMapRegionalizer(zoom)

Bases: Regionalizer

SlippyMapRegionalizer class.

PARAMETER DESCRIPTION
zoom

zoom level

TYPE: int

RAISES DESCRIPTION
ValueError

if zoom is not in [0, 19]

Source code in srai/regionalizers/slippy_map_regionalizer.py
def __init__(self, zoom: int) -> None:
    """
    Initialize SlippyMapRegionalizer.

    Args:
        zoom (int): zoom level

    Raises:
        ValueError: if zoom is not in [0, 19]
    """
    if not 0 <= zoom <= 19:
        raise ValueError
    self.zoom = zoom
    super().__init__()

transform(gdf)

Regionalize a given GeoDataFrame.

PARAMETER DESCRIPTION
gdf

GeoDataFrame to be regionalized.

TYPE: gpd.GeoDataFrame

RETURNS DESCRIPTION
gpd.GeoDataFrame

gpd.GeoDataFrame: GeoDataFrame with regionalized geometries.

RAISES DESCRIPTION
ValueError

If provided GeoDataFrame has no crs defined.

Source code in srai/regionalizers/slippy_map_regionalizer.py
def transform(self, gdf: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    """
    Regionalize a given GeoDataFrame.

    Args:
        gdf (gpd.GeoDataFrame): GeoDataFrame to be regionalized.

    Returns:
        gpd.GeoDataFrame: GeoDataFrame with regionalized geometries.

    Raises:
        ValueError: If provided GeoDataFrame has no crs defined.
    """
    gdf_wgs84 = gdf.to_crs(crs=WGS84_CRS)
    gdf_exploded = self._explode_multipolygons(gdf_wgs84)

    values = (
        seq(gdf_exploded[GEOMETRY_COLUMN])
        .map(self._to_cells)
        .flatten()
        .map(
            lambda item: (
                item
                | {
                    REGIONS_INDEX: f"{item['x']}_{item['y']}_{self.zoom}",
                    "z": self.zoom,
                }
            )
        )
        .to_list()
    )

    gdf = gpd.GeoDataFrame(values, geometry=GEOMETRY_COLUMN, crs=WGS84_CRS).set_index(
        REGIONS_INDEX
    )
    return gdf.drop_duplicates()