Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoBox.zoom_to with a resolution #79

Closed
keewis opened this issue Nov 24, 2022 · 8 comments · Fixed by #86
Closed

GeoBox.zoom_to with a resolution #79

keewis opened this issue Nov 24, 2022 · 8 comments · Fixed by #86

Comments

@keewis
Copy link

keewis commented Nov 24, 2022

I've been trying to use odc-geo to define the target grid for regridding with xesmf. This works great so far, but one of the features I've been missing so far is creating a Dataset from the GeoBox.

The naive version I've been using so far

def to_dataset(geobox):
    def to_variable(dim, coord):
        data = coord.values
        units = coord.units
        resolution = coord.resolution
        if resolution < 0:
            data = data[::-1]
            resolution *= -1
        return xr.Variable(
            dims=dim, data=data, attrs={"units": units, "resolution": resolution}
        )

    coords = {
        name: to_variable(name, odc_coord)
        for name, odc_coord in geobox.coordinates.items()
    }
    return xr.Dataset(coords=coords)

which obviously lacks support for affine transformations, assumes 1D coordinates, and the resolution trick should probably configurable (and off by default?)

Did I miss anything? Do you know of a better way to do this? If not, would you be open to adding something like this to odc.geo.xr?

@Kirill888
Copy link
Member

those produce DataArray, or coordinates for it, you can then wrap dataset around that if needed

@keewis
Copy link
Author

keewis commented Nov 24, 2022

xr_coords is sufficiently close, I totally missed that. I'd still have to change the resolution, but that's easy enough.

Thanks a lot for the quick reply!

@Kirill888
Copy link
Member

I'd still have to change the resolution, but that's easy enough.

like this? https://odc-geo.readthedocs.io/en/latest/_api/odc.geo.geobox.GeoBox.flipy.html

@keewis
Copy link
Author

keewis commented Nov 24, 2022

I'm not sure. This helps with doing the actual transformation, but I'd still have to decide when to call it (and switch between flipx and flipy depending on the coordinate). Basically, what I want is to normalize the resolution to always be positive, since that's the way the grids I'm working with are oriented, and xarray's default indexes require the orientation to be identical (although with custom indexes that might change).

@keewis
Copy link
Author

keewis commented Nov 24, 2022

actually, since that particular geobox was created by rescaling a existing geobox I could just pass a Resolution object instead of just a float.

Which leads to a different question: is there a simple way to create a geobox from an existing one, but with changed resolution? Basically, what I'm looking for is something similar to zoom_out or zoom_to, but with a absolute step size instead of a zoom factor or a new shape. I've been using

new_geobox = GeoBox.from_bbox(geobox.boundingbox, resolution=0.01)

but that feels clunky, and that's also where the negative resolution is from.

@keewis keewis changed the title Feature request: convert GeoBox to Dataset convert GeoBox to Dataset Nov 24, 2022
@Kirill888
Copy link
Member

so like geobox.zoom_to(resolution=0.01)? not a thing currently, but makes sense to have

you can use GeoBox.from_bbox(geobox.boundingbox, resolution=odc.geo.resxy_(0.01, 0.01)) to force non-flipped y-axis at construction time.

@keewis keewis changed the title convert GeoBox to Dataset GeoBox.zoom_to with a resolution Nov 24, 2022
@keewis
Copy link
Author

keewis commented Nov 24, 2022

thanks. I'll probably use odc.geo.Resolution(x=0.01, y=0.01) since that's a bit more explicit, but I'm happy with the result.

Edit: I'll leave this open to track the geobox.zoom_to(resolution=...) suggestion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants