geographer.cutters package¶
Submodules¶
geographer.cutters.cut_every_raster_to_grid module¶
Cut rasters in the source dataset to a grid of rasters.
Dataset cutter that cuts rasters in the source dataset to a grid of rasters.
- geographer.cutters.cut_every_raster_to_grid.get_cutter_every_raster_to_grid(source_data_dir, target_data_dir, name, new_raster_size=512, raster_filter_predicate=None)[source]¶
Return dataset cutter that cuts every raster to a grid.
Return dataset cutter that cuts every raster in the source dataset to a grid of rasters.
- Return type:
- Parameters:
source_data_dir (Path) – source data dir
target_data_dir (Path) – target data dir
name (str) – name of cutter, used when saving the cutter
new_raster_size (int | Tuple[int, int]) – size of new rasters. Defaults to 512.
raster_filter_predicate (RasterFilterPredicate | None) – raster filter predicate to select rasters. Defaults to None (i.e. cut all rasters that have not been previously cut).
- Returns:
dataset cutter
- Return type:
geographer.cutters.cut_iter_over_rasters module¶
Dataset cutter that iterates over rasters.
Implements a general-purpose higher order function to create or update datasets of GeoTiffs from existing ones by iterating over rasters.
- class geographer.cutters.cut_iter_over_rasters.DSCutterIterOverRasters(**data)[source]¶
Bases:
DSCreatorFromSourceWithBandsDataset cutter that iterates over rasters.
Implements a general-purpose higher order function to create or update datasets of GeoTiffs from existing ones by iterating over rasters.
- Parameters:
source_data_dir (Path)
target_data_dir (Path)
name (str)
bands (dict[str, list[int] | None] | None)
raster_cutter (SingleRasterCutter)
raster_filter_predicate (RasterFilterPredicate)
label_maker (LabelMaker | None)
cut_rasters (list[str])
- Return type:
None
- field bands: Optional[dict[str, Optional[list[int]]]] = None¶
keys: raster directory names, values: list of band indices to keep, starting with 1
- Validated by:
validate_connectors
-
field cut_rasters:
list[str] [Optional]¶ Names of cut rasters in source_data_dir. Usually not to be set by hand!
- Validated by:
validate_connectors
-
field label_maker:
Optional[LabelMaker] = None¶ Optional label maker. If given, will be used to recompute labels when necessary. Defaults to None
- Validated by:
validate_connectors
- field name: str [Required]¶
Name of dataset creator. Used as part of filename when saving.
- Validated by:
validate_connectors
-
field raster_cutter:
SingleRasterCutter[Required]¶ - Validated by:
validate_connectors
-
field raster_filter_predicate:
RasterFilterPredicate= AlwaysTrue()¶ - Validated by:
validate_connectors
- field source_data_dir: Path [Required]¶
- Validated by:
validate_connectorsvalidate_source_data_dir
- field target_data_dir: Path [Required]¶
- Validated by:
validate_connectors
- create_or_update()[source]¶
Create or update target dataset.
- Return type:
- Returns:
connector of target dataset
- cut()[source]¶
Cut a dataset.
Alternate name for the create method. See create_or_update for docstring.
- Return type:
- classmethod from_json_file(json_file_path, constructor_symbol_table=None)¶
Load and return saved BaseModel.
- Return type:
Any- Parameters:
json_file_path (Path | str)
constructor_symbol_table (dict[str, Any] | None)
- save()¶
Save to update folder in source_data_dir.
- property source_connector¶
Connector in source_data_dir.
- property target_connector¶
Connector in target_data_dir.
geographer.cutters.cut_iter_over_vectors module¶
Dataset cutter that iterates over vector features.
Implements a general-purpose higher order function to create or update datasets of GeoTiffs from existing ones by iterating over vector features.
- class geographer.cutters.cut_iter_over_vectors.DSCutterIterOverVectors(**data)[source]¶
Bases:
DSCreatorFromSourceWithBandsDataset cutter that iterates over vector features.
Implements a general-purpose higher order function to create or update datasets of GeoTiffs from existing ones by iterating over vector features: Adds all features in the source dataset to the target dataset and iterate over all features in the target dataset. For each vector feature if the vector_filter_predicate is met uses the raster_selector to select a subset of the rasters in the source dataset for which no rasters for this vector feature have previously been cut from. Each of the rasters is then cut using the raster_cutter, and the new rasters are added to the target dataset/connector.
- Parameters:
source_data_dir (Path)
target_data_dir (Path)
name (str)
bands (dict[str, list[int] | None] | None)
vector_filter_predicate (VectorFilterPredicate)
raster_selector (RasterSelector)
raster_cutter (SingleRasterCutter)
label_maker (LabelMaker | None)
cut_rasters (dict[str, list[str]])
- Return type:
None
- field bands: Optional[dict[str, Optional[list[int]]]] = None¶
keys: raster directory names, values: list of band indices to keep, starting with 1
- Validated by:
validate_connectors
- field cut_rasters: dict[str, list[str]] [Optional]¶
Normally, should not be set by hand! Dict with vector features as keys and lists of rasters cut for each vector feature as values
- Validated by:
validate_connectors
- field label_maker: Optional[LabelMaker] = None¶
Optional label maker. If given, will be used to recompute labels when necessary. Defaults to None
- Validated by:
validate_connectors
- field name: str [Required]¶
Name of dataset creator. Used as part of filename when saving.
- Validated by:
validate_connectors
- field raster_cutter: SingleRasterCutter [Required]¶
- Validated by:
validate_connectors
- field raster_selector: RasterSelector [Required]¶
Selects rasters from source to cut for a given vector feature
- Validated by:
validate_connectors
- field source_data_dir: Path [Required]¶
- Validated by:
validate_connectorsvalidate_source_data_dir
- field target_data_dir: Path [Required]¶
- Validated by:
validate_connectors
- field vector_filter_predicate: VectorFilterPredicate [Optional]¶
Filters vector features to be cut
- Validated by:
validate_connectors
- create_or_update()[source]¶
Create or update target dataset.
- Return type:
- Returns:
connector of target dataset
- cut()[source]¶
Cut a dataset.
Alternate name for the create method. See create_or_update for docstring.
- classmethod from_json_file(json_file_path, constructor_symbol_table=None)¶
Load and return saved BaseModel.
- Return type:
Any- Parameters:
json_file_path (Path | str)
constructor_symbol_table (dict[str, Any] | None)
- save()¶
Save to update folder in source_data_dir.
- property source_connector¶
Connector in source_data_dir.
- property target_connector¶
Connector in target_data_dir.
geographer.cutters.cut_rasters_around_every_vector module¶
Dataset cutter that cuts out rasters around vector features.
- geographer.cutters.cut_rasters_around_every_vector.get_cutter_rasters_around_every_vector(source_data_dir, target_data_dir, name, mode='random', new_raster_size=512, min_new_raster_size=None, scaling_factor=None, target_raster_count=1, bands=None, random_seed=10)[source]¶
Return dataset cutter that creates cutouts around vector features.
In “random” or “centered” all cutouts will be of the same size. In “variable” mode the size of a cutout is the maximum of scaling_factor*size_of_bounding_box or min_new_raster_size
- Return type:
- Parameters:
source_data_dir (Path | str) – source data dir
target_data_dir (Path | str) – target data dir
name (str) – name
new_raster_size (int | Tuple[int, int] | None) – new raster size for “random” and “centered” modes. Defaults to 512.
min_new_raster_size (int | Tuple[int, int] | None) – lower bound on raster size for “variable” mode. Defaults to None.
scaling_factor (float | None) – scaling factor for “variable” mode. Defaults to None.
target_raster_count (int) – targeted number of rasters per vector feature to create. Defaults to 1.
mode (Literal['random', 'centered', 'variable']) – On. Defaults to “random”.
bands (dict | None) – bands dict. Defaults to None.
random_seed (int) – random seed. Defaults to 10.
- Returns:
dataset cutter
- Return type:
geographer.cutters.raster_filter_predicates module¶
ABC for predicates for filtering rasters.
Used in cutting functions.
- class geographer.cutters.raster_filter_predicates.AlwaysTrue(**data)[source]¶
Bases:
RasterFilterPredicateDefault raster filter predicate that always returns True.
Used when filtering is not desired.
- Return type:
None
- save(json_path)¶
Save the predicate.
- Return type:
None- Parameters:
json_path (Path)
- class geographer.cutters.raster_filter_predicates.RasterFilterPredicate(**data)[source]¶
Bases:
ABC,Callable,BaseModelABC for predicates used to filter rasters in cutting functions.
Subclasses should implement a __call__method that has the arguments and behavior given below.
- Return type:
None
- class geographer.cutters.raster_filter_predicates.RasterFilterRowCondition(row_series_predicate)[source]¶
Bases:
RasterFilterPredicateSimple RasterFilter based on row condition.
Applies a given predicate to the row in source_connector.rasters corresponding to the raster name in question.
- Parameters:
row_series_predicate (RowSeriesPredicate)
- Return type:
None
- field row_series_predicate: RowSeriesPredicate [Required]¶
- save(json_path)¶
Save the predicate.
- Return type:
None- Parameters:
json_path (Path)
- class geographer.cutters.raster_filter_predicates.RastersNotPreviouslyCutOnly(**data)[source]¶
Bases:
RasterFilterPredicateSelect rasters not previously cut.
- Return type:
None
- save(json_path)¶
Save the predicate.
- Return type:
None- Parameters:
json_path (Path)
- class geographer.cutters.raster_filter_predicates.RowSeriesPredicate(**data)[source]¶
Bases:
ABC,BaseModelRow series predicate.
Apply to series, i.e. single row.
- Return type:
None
geographer.cutters.raster_selectors module¶
Callable classes for selecting a sublist from a list of rasters.
Used by cutting functions.
- class geographer.cutters.raster_selectors.RandomRasterSelector(**data)[source]¶
Bases:
RasterSelectorRasterSelector that randomly selects randomly from a list of rasters.
- Parameters:
target_raster_count (int)
- Return type:
None
- field target_raster_count: int = 1¶
- class geographer.cutters.raster_selectors.RasterSelector(**data)[source]¶
Bases:
Callable,BaseModelABC for selecting from a list of rasters.
Subclasses are used by DSCutterIterOverVectors.
Subclasses should implement a __call__method that has the arguments and behavior given below.
- Return type:
None
geographer.cutters.single_raster_cutter_around_vector module¶
SingleRasterCutter that creates a cutout around a vector feature.
- class geographer.cutters.single_raster_cutter_around_vector.SingleRasterCutterAroundVector(mode, new_raster_size=None, scaling_factor=1.2, min_new_raster_size=None, random_seed=42, **kwargs)[source]¶
Bases:
SingleRasterCutterSingleRasterCutter that creates a cutout around a vector feature.
SingleRasterCutter that cuts a small raster (or several contiguous such rasters if the vector feature does not fit into a single one) around each vector feature in the raster accepted by the feature filter predicate.
- Parameters:
mode (Literal['random', 'centered', 'variable'])
new_raster_size (int | Tuple[int, int] | None)
scaling_factor (float | None)
min_new_raster_size (int | Tuple[int, int] | None)
random_seed (int)
- Return type:
None
- field min_new_raster_size: Optional[RasterSize] = None¶
- field mode: Literal['random', 'centered', 'variable'] [Required]¶
- field new_raster_size: Optional[RasterSize] = None¶
- field random_seed: int = 42¶
- field scaling_factor: Optional[float] = 1.2¶
geographer.cutters.single_raster_cutter_base module¶
Abstract base class for single raster cutters.
- class geographer.cutters.single_raster_cutter_base.SingleRasterCutter(**data)[source]¶
Bases:
ABC,BaseModel,RasterBandsGetterMixInBase class for SingleRasterCUtter.
- Return type:
None
geographer.cutters.single_raster_cutter_bbox module¶
SingleRasterCutter that extracts pre defined bboxes from a raster.
- class geographer.cutters.single_raster_cutter_bbox.SingleRasterCutterFromBBoxes(**data)[source]¶
Bases:
SingleRasterCutterSingleRasterCutter that extracts pre defined bboxes from a raster.
The new size of the rasters must be specified as it is used to ensure a standardised output.
- Parameters:
new_raster_size (int | Tuple[int, int])
bbox_geojson_path (Path)
- Return type:
None
- field bbox_geojson_path: Path [Required]¶
- Validated by:
path_points_to_geojson
- field new_raster_size: RasterSize [Required]¶
- Validated by:
new_raster_size_side_lengths_must_be_positivenew_raster_size_type_correctness
- property new_raster_size_cols: int¶
Return number of columns of new raster size.
- property new_raster_size_rows: int¶
Return number of rows of new raster size.
geographer.cutters.single_raster_cutter_grid module¶
SingleRasterCutter that cuts a raster to a grid of rasters.
- class geographer.cutters.single_raster_cutter_grid.SingleRasterCutterToGrid(**data)[source]¶
Bases:
SingleRasterCutterSingleRasterCutter that cuts a raster into a grid of rasters.
- Parameters:
new_raster_size (int | Tuple[int, int])
- Return type:
None
- field new_raster_size: RasterSize [Required]¶
- Validated by:
new_raster_size_side_lengths_must_be_positivenew_raster_size_type_correctness
- property new_raster_size_cols: int¶
Return number of columns of new raster size.
- property new_raster_size_rows: int¶
Return number of rows of new raster size.
geographer.cutters.type_aliases module¶
Type aliases.
geographer.cutters.vector_filter_predicates module¶
Predicates for filtering vector features.
- class geographer.cutters.vector_filter_predicates.AlwaysTrue(**data)[source]¶
Bases:
VectorFilterPredicateSimple vector feature filter predicate that always returns True.
- Return type:
None
- class geographer.cutters.vector_filter_predicates.FilterVectorByRowCondition(row_series_predicate, mode)[source]¶
Bases:
VectorFilterPredicateSimple GeomFilterPredicate that uses a predicate on rows.
Applies a predicate to the row in the source or target vectors corresponding to the vector feature name in question.
- Parameters:
row_series_predicate (collections.abc.Callable[[GeoSeries | Series], bool])
mode (Literal['source', 'target'])
- Return type:
None
- class geographer.cutters.vector_filter_predicates.IsVectorMissingRasters(**data)[source]¶
Bases:
VectorFilterPredicateVectorFilterPredicate that uses raster counts as criterion.
Checks whether a vector feature has fewer rasters than a specified target raster count.
- Parameters:
target_raster_count (int)
- Return type:
None
- field target_raster_count: int = 1¶
- class geographer.cutters.vector_filter_predicates.OnlyThisVector(this_vector_name)[source]¶
Bases:
VectorFilterPredicateFilter out all vector features except a given one.
Simple vector feature filter initialized with a vector feature this_vector_name.
Returns True if and only if the vector feature under consideration is equal to this_vector_name.
- Parameters:
this_vector_name (str | int)
- Return type:
None
- class geographer.cutters.vector_filter_predicates.VectorFilterPredicate(**data)[source]¶
Bases:
BaseModel,CallableABC for predicates for filtering vector features.
To be used in cutting functions.
Subclasses should implement a __call__method that has the arguments and behavior given below.
- Return type:
None