opendatacube / odc-geo

GeoBox and geometry utilities extracted from datacube-core
https://odc-geo.readthedocs.io/en/latest/
Apache License 2.0
80 stars 12 forks source link

negative size shape is allowed #136

Closed mdsumner closed 5 months ago

mdsumner commented 6 months ago

I noticed that values of shape can be negative, this doesn't seem desirable, it has the effect of flipping the transform as expected.

from odc.geo.geobox import GeoBox
ex = [-120, -100, 35, 55]

g = GeoBox.from_bbox([140, -43, 160, -23],
   "EPSG:4326", 
   shape = [-1, 1])
g
#GeoBox((-1, 1), Affine(20.0, 0.0, 140.0,
#       0.0, 20.0, -60.0), CRS('EPSG:4326'))

I only noticed because I wanted to do the GDAL thing of setting one of the dimensions to zero, whereby you only need to use the shape of one side and it helpfully calculates the other from the aspect ratio of the extent. This is nice because it gives a very short heuristic for "about this detailed" that matches how images work, rather than having to specify a specific resolution or do gymnastics to work out the ratio.

So, maybe negative shape should be disallowed, and if it's of interest I could PR the shape = [0, n] or shape = [n, 0].

Thanks!

Kirill888 commented 6 months ago

Thanks Michael, we should probably raise ValueError on such inputs, PR is welcome. 0 should be allowed though, I would not clip to 0 however, raise an error when negative dimensions are requested, or whatever numpy.zeros would do in such case , which is ValueError: negative dimensions are not allowed.

mdsumner commented 5 months ago

sorry, is that typo about allowing 0?

I can't tell if you mean

or something else I'm not understanding (?)

Kirill888 commented 5 months ago

0 is ok, less than 0 is not

mdsumner commented 5 months ago

I was just getting into this ... thanks! haha