Code Monkey home page Code Monkey logo

Comments (6)

gjoseph92 avatar gjoseph92 commented on July 28, 2024

Thanks for the great issue @maawoo. I don't think you're doing anything wrong here; this is probably a bug. Which version are you using, btw? 1) I noticed #60 hasn't been released yet and 2) #35 from >= 0.2.0 is slightly related (though I highly doubt relevant here). Would it be possible for you to try with pip install git+https://github.com/gjoseph92/stackstac.git@main to see if #60 fixes it?

One other thing to be aware of is that I believe rioxarray follows xarray conventions (rather than typical raster conventions) and expects coordinates to refer to pixel centers. So I'd think (but haven't tried) that xy_coords='center' would actually give you (0, 0) offset in your output GeoTIFF, and xy_coords='topleft' would give you (0.5, 0.5).

from stackstac.

maawoo avatar maawoo commented on July 28, 2024

Hi Gabe,
I was using 0.2.1 installed via pip install stackstac. Forgot to check for any relevant changes that might not have been released yet. Thanks for the reminder to do that in the future!

Unfortunately the problem persists after installing from git and #60 doesn't fix it, which also means that xy_coords='center' still results in an unexpected offset.

I did the following quick test regarding rioxarray with in.tif being the same raster file as described above:

test = rioxarray.open_rasterio('./in.tif')
test.rio.to_raster('./out.tif')

The output is still perfectly aligned afterwards, so I assume rioxarray/rasterio somehow handle the conversion between raster and xarray conventions internally. I would've been very suprised if not tbh.

I think that the error lies somewhere here because there's no offset problem with xy_coords=False, which skips the code block that I linked. Unfortunately I don't have much time to spare at the moment to look into this myself.

from stackstac.

gjoseph92 avatar gjoseph92 commented on July 28, 2024

Yup, something is definitely wrong with the xy_coords logic. I'm really sorry about that; that's not the sort of thing you should have to question or debug as a user!

I also don't have a ton of time to look into this, but hopefully I can next week. If you could provide a full copy-pasteable reproducer, that would help a ton. What's your stac_obj?

from stackstac.

maawoo avatar maawoo commented on July 28, 2024

No problem and as a user I'm always happy to contribute to projects that I have a personal interest in!

You can find a stripped-down example scene here:
https://upload.uni-jena.de/data/612dde1b411da9.40050697/xycoords.7z

I tested it with the following code, except that the asset href in the STAC Item needs to be changed to absolute to work with stackstac of course.

from pystac import Collection
import stackstac
import rioxarray

collection_path = '/path/to/collection.json'
stac_obj = Collection.from_file(collection_path)

scene_stack = stackstac.stack(items=stac_obj, chunksize=5120, xy_coords='center', dtype='float32', rescale=False)
vh = scene_stack.sel(band='vh')

vh.rio.to_raster('/.../out.tif')

Hope that helps!

from stackstac.

Kirill888 avatar Kirill888 commented on July 28, 2024

I think I'm observing something that is related: xy_coords='center' mode of operation computes Y coordinates incorrectly when Y resolution is negative. What I'm observing is that as I switch from default to `'center`` both X and Y are shifted by half a pixel away from 0, but Y needs to move towards 0 as Y axis is inverted.

from stackstac.

Kirill888 avatar Kirill888 commented on July 28, 2024

@maawoo I'm pretty sure .rio expects coordinates to be in center mode. But see my comment about offset going wrong way for Y axis when using center. You can check for inconsistency by comparing output of .rio.transform() with attribute reported by stackstac.

This is what I'm observing:

xx.attrs['transform'], xx.rio.transform()
(Affine(10.0, 0.0, 499980.0,
        0.0, -10.0, 9200020.0),
 Affine(10.0, 0.0, 499980.0,
        0.0, -10.0, 9200030.0))

which is consistent with your reports.

But if I apply a fix:

xx.y.data[:] = xx.y.data[:] - float(xx.resolution)

then I get consistent output from .rio.transform(). If you can try applying that fix and check if you get data round-trip without an offset then?

  1. Load with center into xx
  2. xx.y.data[:] = xx.y.data[:] - float(xx.resolution)
  3. Save with .rio, load back compare.

from stackstac.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.