Skip to content

Commit

Permalink
REF: Make kwargs required (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 authored Oct 8, 2024
1 parent 0e21a69 commit 4307244
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
5 changes: 4 additions & 1 deletion geocube/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions geocube/cli/commands/make_geocube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions geocube/geo_utils/geobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -105,6 +106,7 @@ class GeoBoxMaker:

def __init__(
self,
*,
output_crs: Any,
resolution: Optional[Union[float, Iterable[float]]],
align: Optional[tuple[float, float]],
Expand Down
9 changes: 6 additions & 3 deletions geocube/rasterize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
)

Expand All @@ -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],
Expand Down Expand Up @@ -189,6 +191,7 @@ def rasterize_points_griddata(


def rasterize_points_radial(
*,
geometry_array: geopandas.GeoSeries,
data_values: NDArray,
grid_coords: dict[str, NDArray],
Expand Down
27 changes: 18 additions & 9 deletions geocube/vector_to_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class VectorToCube:

def __init__(
self,
*,
vector_data: Union[str, os.PathLike, geopandas.GeoDataFrame],
geobox_maker: GeoBoxMaker,
fill: float,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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"),
Expand All @@ -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`.
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 4307244

Please sign in to comment.