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

Non-standard affine transforms handled incorrectly #98

Open
perrygeo opened this issue Sep 16, 2015 · 8 comments
Open

Non-standard affine transforms handled incorrectly #98

perrygeo opened this issue Sep 16, 2015 · 8 comments
Labels

Comments

@perrygeo
Copy link
Owner

If you have a dataset with inverted y axis (i.e. the affine.e parameter is positive) a lot of assumptions are broken since North is no longer "up".

The bounds_window must return row_start < row_stop due to rasterio design. So why not internally convert to north-up on read? The window_bounds functions always return w,s,e,n and we'd just need a few conditionals to ensure new_array was a north-up array.

Work started in the southup branch

@perrygeo perrygeo added the bug label Sep 16, 2015
@kmoses45
Copy link

Is this what could be causing 'ValueError: negative dimensions are not allowed' in zonal_stats(vector, raster, categorical=True)?

@perrygeo
Copy link
Owner Author

Possibly. Try rio info on the raster and check the transform.

@kmoses45
Copy link

"transform":[1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
Both vector and raster are in EPSG:4326

@perrygeo
Copy link
Owner Author

With that transform, it's likely that your data lacks georeferencing according to rasterio/GDAL. That's the identity transform and is unlikely the true transform for your data unless you have exactly 1 degree cells with the upper left corner at lat 0, lon 0.

EDIT: To your original question re: ValueError: negative dimensions are not allowed - yes that is currently the behavior when the y axis is inverted (i.e. the 5th element of the transform is positive)

@kmoses45
Copy link

Ah, that's it! I was using an .ovr file that was georeferenced in Arc. Converted to .tif, georeferenced, and now it works. Thanks for the help. Rasterstats is an awesome module!

@pierstitus
Copy link

Good to see you know about this problem, it took me a while to find this issue.

It would be nice to do a little check on affine and give a warning when e is positive (or a is negative, even though that's way less common)

For those working with ndarrays, here a little function to flip the raster and affine matrix (assuming coordinates are on center of grid cells):

def flipud(raster, affine):
    raster = np.flipud(raster)
    affine = Affine(affine.a, affine.b, affine.c,
                    affine.d, -1 * affine.e, affine.f + (affine.e * (data.shape[0]-1))
    return raster, affine

Thanks for the good work!

Would you like me to work on this issue?

@perrygeo perrygeo changed the title Inverted Y axis datasets not handled correctly Non-standard affine transforms handled incorrectly Apr 23, 2019
@raybellwaves
Copy link

FYI the above function has a couple typos (missing a bracket and data not defined). here's the fixed version:

def flipud(raster, affine):
    raster = np.flipud(raster)
    affine = Affine(
        affine.a,
        affine.b,
        affine.c,
        affine.d,
        -1 * affine.e,
        affine.f + (affine.e * (raster.shape[0] - 1)),
    )
    return raster, affine

@AJSchiller
Copy link

I have an idea for this. Can anyone share vectors and raster data that do work so that I can make sure I don't break anything?

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

No branches or pull requests

5 participants