Skip to content

Commit

Permalink
refactor!: remove GridGenerator (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsmrynk authored Feb 19, 2025
1 parent 09df890 commit c205083
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 401 deletions.
24 changes: 23 additions & 1 deletion aviary/_functional/geodata/coordinates_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import geopandas as gpd
import numpy as np
from numpy import typing as npt
from shapely.geometry import (
Polygon,
box,
)

from aviary._functional.geodata.grid_generator import _generate_tiles
from aviary.core.enums import (
GeospatialFilterMode,
SetFilterMode,
Expand Down Expand Up @@ -130,6 +133,25 @@ def _generate_grid(
)


def _generate_tiles(
coordinates: CoordinatesSet,
tile_size: TileSize,
) -> list[Polygon]:
"""Generates a list of tiles from the coordinates.
Parameters:
coordinates: coordinates (x_min, y_min) of each tile
tile_size: tile size in meters
Returns:
list of tiles
"""
return [
box(x_min, y_min, x_min + tile_size, y_min + tile_size)
for x_min, y_min in coordinates
]


def _geospatial_filter_difference(
coordinates: CoordinatesSet,
grid: gpd.GeoDataFrame,
Expand Down
96 changes: 0 additions & 96 deletions aviary/_functional/geodata/grid_generator.py

This file was deleted.

8 changes: 8 additions & 0 deletions aviary/core/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def from_gdf(
Raises:
AviaryUserError: Invalid `gdf` (the geodataframe contains no geometries)
AviaryUserError: Invalid `gdf` (the geodataframe contains geometries other than polygons)
"""
if gdf.empty:
message = (
Expand All @@ -132,6 +133,13 @@ def from_gdf(
)
raise AviaryUserError(message)

if not all(gdf.geometry.geom_type.isin(['Polygon', 'MultiPolygon'])):
message = (
'Invalid gdf! '
'The geodataframe must contain only polygons.'
)
raise AviaryUserError(message)

x_min, y_min, x_max, y_max = gdf.total_bounds
x_min = floor(x_min)
y_min = floor(y_min)
Expand Down
39 changes: 33 additions & 6 deletions aviary/core/process_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
geospatial_filter,
set_filter,
)

# noinspection PyProtectedMember
from aviary._functional.geodata.grid_generator import compute_coordinates
from aviary.core.bounding_box import BoundingBox
from aviary.core.enums import (
GeospatialFilterMode,
Expand Down Expand Up @@ -192,7 +189,7 @@ def from_bounding_box(
)
raise AviaryUserError(message)

coordinates = compute_coordinates(
coordinates = cls._compute_coordinates(
bounding_box=bounding_box,
tile_size=tile_size,
quantize=quantize,
Expand Down Expand Up @@ -231,7 +228,7 @@ def from_gdf(
)
raise AviaryUserError(message)

if not all(gdf.geometry.geom_type == 'Polygon'):
if not all(gdf.geometry.geom_type.isin(['Polygon', 'MultiPolygon'])):
message = (
'Invalid gdf! '
'The geodataframe must contain only polygons.'
Expand All @@ -246,7 +243,7 @@ def from_gdf(
raise AviaryUserError(message)

bounding_box = BoundingBox.from_gdf(gdf=gdf)
coordinates = compute_coordinates(
coordinates = cls._compute_coordinates(
bounding_box=bounding_box,
tile_size=tile_size,
quantize=quantize,
Expand All @@ -262,6 +259,36 @@ def from_gdf(
tile_size=tile_size,
)

@staticmethod
def _compute_coordinates(
bounding_box: BoundingBox,
tile_size: TileSize,
quantize: bool = True,
) -> CoordinatesSet:
"""Computes the coordinates of the bottom left corner of each tile.
Parameters:
bounding_box: Bounding box
tile_size: Tile size in meters
quantize: If True, the bounding box is quantized to `tile_size`
Returns:
Coordinates (x_min, y_min) of each tile in meters
"""
if quantize:
bounding_box = bounding_box.quantize(
value=tile_size,
inplace=False,
)

coordinates_range_x = np.arange(bounding_box.x_min, bounding_box.x_max, tile_size)
coordinates_range_y = np.arange(bounding_box.y_min, bounding_box.y_max, tile_size)
coordinates_x, coordinates_y = np.meshgrid(coordinates_range_x, coordinates_range_y)

coordinates_x = coordinates_x.reshape(-1)[..., np.newaxis]
coordinates_y = coordinates_y.reshape(-1)[..., np.newaxis]
return np.concatenate((coordinates_x, coordinates_y), axis=-1).astype(np.int32)

@classmethod
def from_json(
cls,
Expand Down
2 changes: 0 additions & 2 deletions aviary/geodata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
ValuePostprocessor,
ValuePostprocessorConfig,
)
from .grid_generator import GridGenerator

__all__ = [
'ClipPostprocessor',
Expand All @@ -41,7 +40,6 @@
'GeodataPostprocessor',
'GeodataPostprocessorConfig',
'GeospatialFilter',
'GridGenerator',
'MaskFilter',
'SetFilter',
'SievePostprocessor',
Expand Down
74 changes: 0 additions & 74 deletions aviary/geodata/grid_generator.py

This file was deleted.

1 change: 0 additions & 1 deletion dev/mkdocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ nav:
- SievePostprocessor: api_reference/geodata/geodata_postprocessor/sieve_postprocessor.md
- SimplifyPostprocessor: api_reference/geodata/geodata_postprocessor/simplify_postprocessor.md
- ValuePostprocessor: api_reference/geodata/geodata_postprocessor/value_postprocessor.md
- GridGenerator: api_reference/geodata/grid_generator.md
- aviary.inference:
- Exporter:
- Exporter: api_reference/inference/exporter/exporter.md
Expand Down
1 change: 0 additions & 1 deletion docs/api_reference/geodata/grid_generator.md

This file was deleted.

34 changes: 0 additions & 34 deletions tests/_functional/geodata/data/data_test_grid_generator.py

This file was deleted.

Loading

0 comments on commit c205083

Please # to comment.