From e134af9c50f2ccd741019b6423063bcfebcfc50e Mon Sep 17 00:00:00 2001 From: snowman2 Date: Tue, 8 Oct 2024 08:33:36 -0500 Subject: [PATCH] REF: Make kwargs required --- geocube/api/core.py | 5 ++++- geocube/cli/commands/make_geocube.py | 1 + geocube/geo_utils/geobox.py | 2 ++ geocube/rasterize.py | 9 ++++++--- geocube/vector_to_cube.py | 27 ++++++++++++++++++--------- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/geocube/api/core.py b/geocube/api/core.py index 2b9fe5e..0f5e7b4 100644 --- a/geocube/api/core.py +++ b/geocube/api/core.py @@ -17,6 +17,7 @@ def make_geocube( vector_data: Union[str, os.PathLike, geopandas.GeoDataFrame], + *, measurements: Optional[list[str]] = None, datetime_measurements: Optional[list[str]] = None, output_crs: Any = None, @@ -94,7 +95,9 @@ def make_geocube( Requested data in a :obj:`xarray.Dataset`. """ - geobox_maker = GeoBoxMaker(output_crs, resolution, align, geom, like) + geobox_maker = GeoBoxMaker( + output_crs=output_crs, resolution=resolution, align=align, geom=geom, like=like + ) return VectorToCube( vector_data=vector_data, diff --git a/geocube/cli/commands/make_geocube.py b/geocube/cli/commands/make_geocube.py index 4eb9cb7..c07f41d 100644 --- a/geocube/cli/commands/make_geocube.py +++ b/geocube/cli/commands/make_geocube.py @@ -83,6 +83,7 @@ @click.argument("vector_data", type=click.Path(exists=True), required=True) @click.argument("output_file", required=True) def make_geocube( + *, output_file, vector_data, measurements, diff --git a/geocube/geo_utils/geobox.py b/geocube/geo_utils/geobox.py index 2a1d4d4..f088569 100644 --- a/geocube/geo_utils/geobox.py +++ b/geocube/geo_utils/geobox.py @@ -47,6 +47,7 @@ def geobox_from_rio(xds: Union[xarray.Dataset, xarray.DataArray]) -> GeoBox: def load_vector_data( vector_data: Union[str, os.PathLike, geopandas.GeoDataFrame], + *, measurements: Optional[list[str]] = None, ) -> geopandas.GeoDataFrame: """ @@ -105,6 +106,7 @@ class GeoBoxMaker: def __init__( self, + *, output_crs: Any, resolution: Optional[Union[float, Iterable[float]]], align: Optional[tuple[float, float]], diff --git a/geocube/rasterize.py b/geocube/rasterize.py index a54138f..d33c9ef 100644 --- a/geocube/rasterize.py +++ b/geocube/rasterize.py @@ -42,7 +42,7 @@ def _remove_missing_data( return data_values, geometry_array -def _minimize_dtype(dtype: numpy.dtype, fill: float) -> numpy.dtype: +def _minimize_dtype(*, dtype: numpy.dtype, fill: float) -> numpy.dtype: """ If int64, convert to float64: https://github.com/OSGeo/gdal/issues/3325 @@ -64,6 +64,7 @@ def _minimize_dtype(dtype: numpy.dtype, fill: float) -> numpy.dtype: def rasterize_image( + *, geometry_array: geopandas.GeoSeries, data_values: Union[NDArray, pandas.arrays.IntegerArray], geobox: odc.geo.geobox.GeoBox, @@ -112,7 +113,7 @@ def rasterize_image( if isinstance(data_values, pandas.arrays.IntegerArray): data_values = data_values.to_numpy( - dtype=_minimize_dtype(data_values.dtype.numpy_dtype, fill), + dtype=_minimize_dtype(dtype=data_values.dtype.numpy_dtype, fill=fill), na_value=fill, ) @@ -126,12 +127,13 @@ def rasterize_image( fill=fill, all_touched=all_touched, merge_alg=merge_alg, - dtype=_minimize_dtype(data_values.dtype, fill), + dtype=_minimize_dtype(dtype=data_values.dtype, fill=fill), ) return image def rasterize_points_griddata( + *, geometry_array: geopandas.GeoSeries, data_values: NDArray, grid_coords: dict[str, NDArray], @@ -189,6 +191,7 @@ def rasterize_points_griddata( def rasterize_points_radial( + *, geometry_array: geopandas.GeoSeries, data_values: NDArray, grid_coords: dict[str, NDArray], diff --git a/geocube/vector_to_cube.py b/geocube/vector_to_cube.py index 0ef842d..0ccf659 100644 --- a/geocube/vector_to_cube.py +++ b/geocube/vector_to_cube.py @@ -52,6 +52,7 @@ class VectorToCube: def __init__( self, + *, vector_data: Union[str, os.PathLike, geopandas.GeoDataFrame], geobox_maker: GeoBoxMaker, fill: float, @@ -103,7 +104,9 @@ def __init__( ) self._geobox = geobox_maker.from_vector(self._vector_data) self._grid_coords = affine_to_coords( - self._geobox.affine, self._geobox.width, self._geobox.height + affine=self._geobox.affine, + width=self._geobox.width, + height=self._geobox.height, ) if self._geobox.crs is not None: self._vector_data = self._vector_data.to_crs(self._geobox.crs) @@ -159,6 +162,7 @@ def __init__( def make_geocube( self, + *, interpolate_na_method: Optional[Literal["linear", "nearest", "cubic"]] = None, rasterize_function: Optional[Callable[..., Optional[NDArray]]] = None, ) -> xarray.Dataset: @@ -193,7 +197,7 @@ def make_geocube( @staticmethod def _get_attrs( - measurement_name: str, fill_value: float + *, measurement_name: str, fill_value: float ) -> dict[str, Union[str, float]]: """ Get attributes for data array. @@ -215,7 +219,7 @@ def _get_attrs( "_FillValue": fill_value, } - def _update_time_attrs(self, attrs: dict[str, Any], image_data: NDArray) -> None: + def _update_time_attrs(self, *, attrs: dict[str, Any], image_data: NDArray) -> None: """ Update attributes and nodata values for time grid. @@ -262,7 +266,7 @@ def _get_dataset( ) else: grid_array = self._get_grid( - self._vector_data[[measurement, "geometry"]], + dataframe=self._vector_data[[measurement, "geometry"]], measurement_name=measurement, ) if grid_array is not None: @@ -291,6 +295,7 @@ def _get_dataset( def _get_grouped_grid( self, grouped_dataframe: geopandas.GeoDataFrame, + *, measurement_name: str, ) -> Optional[tuple]: """Retrieve the variable data to append to the ssurgo :obj:`xarray.Dataset`. @@ -337,11 +342,13 @@ def _get_grouped_grid( image_data.append(image) - attrs = self._get_attrs(measurement_name, fill_value) + attrs = self._get_attrs( + measurement_name=measurement_name, fill_value=fill_value + ) image_data = numpy.array(image_data) # it was converted to numeric date value if df_group is not None and "datetime" in str(df_group[measurement_name].dtype): - self._update_time_attrs(attrs, image_data) + self._update_time_attrs(attrs=attrs, image_data=image_data) return ( (self._group_by, "y", "x"), @@ -351,7 +358,7 @@ def _get_grouped_grid( ) def _get_grid( - self, dataframe: geopandas.GeoDataFrame, measurement_name: str + self, dataframe: geopandas.GeoDataFrame, *, measurement_name: str ) -> Optional[tuple]: """Retrieve the variable data to append to the ssurgo :obj:`xarray.Dataset` from a regular :obj:`geopandas.GeoDataFrame`. @@ -387,11 +394,13 @@ def _get_grid( ) return None - attrs = self._get_attrs(measurement_name, fill_value) + attrs = self._get_attrs( + measurement_name=measurement_name, fill_value=fill_value + ) # it was converted to numeric date value if "datetime" in str(dataframe[measurement_name].dtype): - self._update_time_attrs(attrs, image_data) + self._update_time_attrs(attrs=attrs, image_data=image_data) return ( ("y", "x"),