telespazio-tim / karios

Python application based on KLT Algorithm for Registration of Images from Observing Systems (KARIOS)
Apache License 2.0
10 stars 5 forks source link

Exception if `ref_img` is not larger than `mon_img` in box axes #2

Closed DFEvans closed 1 month ago

DFEvans commented 4 months ago

If ref_img is smaller than the mon_img in either X or Y, line 255 in matcher.py fails to read any data, as x_off+x_size and y_off+y_size exceed the raster bounds:

ref_box = ref_img.read(1, x_off, y_off, x_size, y_size)

ref_box is returned as None, and an exception is raised on line 234, because this is an invalid operation on None:

[...] & np.isfinite(ref_box) & [...]

The documentation never states that ref_img must be larger.

If this is a bug, a quick fix would be to modify the x_size and y_size calculations to always refer to only the valid area across both rasters:

        max_x = min(mon_img.x_size, ref_img.x_size)
        max_y = min(mon_img.y_size, ref_img.y_size)
        x_size = (
            self._conf.tile_size
            if x_off + self._conf.tile_size < max_x
            else max_x - x_off
        )
        y_size = (
            self._conf.tile_size
            if y_off + self._conf.tile_size < max_y
            else max_y - y_off
        )

but an ideal solution might be to read the maximum area from each raster, if the tiles can be of different sizes.

Separately, I note that Python exceptions are not enabled in GDAL (by calling gdal.UseExceptions()), and the code does not explicitly check that GDAL calls succeeded. This is what leads to the ref_img.read call failing silently, and the unhelpful exception being raised later. GDAL is already emitting log warnings about this, and best practice would be to enable exceptions.

telespazio-tim commented 2 months ago

I think this issue was badly reported in #5

telespazio-tim commented 1 month ago

Closed with a7c2ba0