Open keewis opened 1 month ago
My 2 cents:
I think it would be nice if we can have a consistent API for all cell geometry extraction methods, i.e., something like this:
# return cell centers as (shapely) point geometries
ds.dggs.cell_centers() -> xr.DataArray
# return cell centers as lat/lon coordinates
ds.dggs.cell_centers_latlon() -> tuple[xr.DataArray, xr.DataArray]
# return cell boundaries as (shapely) polygon geometries
ds.dggs.cell_boundaries() -> xr.DataArray
cell_xxx()
would always return shapely geometriescell_centers_latlon()
as an additional but common special case
_latlon
is also used in other xdggs method namesPersonally I wouldn't mind doing an extra step to assign lat-lon coordinates:
cell_lat, cell_lon = ds.dggs.cell_centers_latlon()
ds.assign_coords(latitude=cell_lat, longitude=cell_lon)
If we want a convenient, functional-style friendly way to achieve this, maybe assign_coords
could also accept Iterable[DataArray]
?
# reuse the names of the two DataArrays returned by ds.dggs.cell_centers_latlon()
ds.assign_coords(ds.dggs.cell_centers_latlon())
Actually, if we can assign coordinates like above the dggs.assign_latlon_coords()
method may not add a lot of value so I'd be +1 to remove it.
Right now,
Dataset.dggs.cell_centers
returns aDataset
containing the geographical coordinates. However, since we return aDataset
we have to use the lengthyds.pipe(lambda ds: ds.merge(ds.dggs.cell_centers()))
to assign the result to the original object (and this won't even work forDataArray
).Another option would be
ds.pipe(lambda ds: ds.assign_coords(ds.dggs.cell_centers().coords))
, which at least works forDataArray
objects, but otherwise is slightly longer than the code withmerge
.Ideally, we'd allow something like this:
For this to work,
ds.dggs.cell_centers()
would have to return aCoordinates
object (or adict
, or we make the geographic coordinates data variables), andassign_coords
would have to accept aCallable[[Dataset | DataArray], dict | Coordinates]
.What do you think, @benbovy?