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

Example documentation in open_rasterio #5005

Closed
gabriel-abrahao opened this issue Mar 5, 2021 · 2 comments · Fixed by #5021
Closed

Example documentation in open_rasterio #5005

gabriel-abrahao opened this issue Mar 5, 2021 · 2 comments · Fixed by #5021

Comments

@gabriel-abrahao
Copy link
Contributor

gabriel-abrahao commented Mar 5, 2021

This is my first time submitting an issue, so sorry if doing this wrong.

In the documentation for open_rasterio there's an example on how to generate 2D coordinates from a GeoTiff file using it:

from affine import Affine
da = xr.open_rasterio('path_to_file.tif')
transform = Affine.from_gdal(*da.attrs['transform'])
nx, ny = da.sizes['x'], da.sizes['y']
x, y = np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) * transform

However, this method does not really work because the da.attrs['transform'] attribute is not coded in the GDAL standard, but as affine.Affine expects directly (maybe it wasn't so in a previous version. So, at least for me (v0.16.2), getting the affine transformation right is done by simply replacing that line with the simpler:

transform = Affine(*da.attrs['transform'])

Again, sorry if this is not how this should be done, I just took a while to figure this out for my application and thought other people might benefit from it. If I can do something to fix it (if it's really broken) please let me know.

@scottyhq
Copy link
Contributor

scottyhq commented Mar 10, 2021

Thanks @gabriel-abrahao good catch! the docs are definitely outdated here and need to be changed. This has been brought up before here #3185 (comment)

You can either do what you suggested or simply transform = Affine(*da.transform)

actual_transform = Affine(*da.transform)

A PR to update the documentation would be welcome. It'd be just a one-line fix here:

transform = Affine.from_gdal(*da.attrs['transform'])

cc @snowman2 the docstring for open_rasterio has the same issue currently in rioxarray https://github.com/corteva/rioxarray/blob/63ce87f92835897d46aaa270f5ec07aca4e72cda/rioxarray/_io.py#L653

@snowman2
Copy link
Contributor

Thanks for mentioning that. For rioxarray, the fix is:

transform = da.rio.transform()

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.

3 participants