osgeonepal / geotile

GeoTile is the python library for tiling the geographic raster data (eg. Tiff etc)
https://geotile.readthedocs.io
MIT License
75 stars 9 forks source link

mosaic raster issue: Bounds and transform are inconsistent #59

Open iamtekson opened 10 months ago

iamtekson commented 10 months ago
---------------------------------------------------------------------------
WindowError                               Traceback (most recent call last)
/mnt/d/tek/sm/code/prediction_merge.ipynb Cell 24 line 3
      1 from geotile import mosaic
----> 3 mosaic(f'{PLACE_NAME}_S2_{YM}', f'../data/img&pred/s2_rivers/pred/{PLACE_NAME}_S2_{YM}_pred.tif')

File ~/miniconda3/envs/tf212/lib/python3.9/site-packages/geotile/utils.py:49, in mosaic(input_folder, output_file, image_format, **kwargs)
     46     src_files_to_mosaic.append(src)
     48 # Merge the rasters
---> 49 mosaic, out_trans = merge(src_files_to_mosaic, **kwargs)
     51 # update the metadata
     52 meta = src.meta.copy()

File ~/miniconda3/envs/tf212/lib/python3.9/site-packages/rasterio/merge.py:344, in merge(datasets, bounds, res, nodata, dtype, precision, indexes, output_count, resampling, method, target_aligned_pixels, dst_path, dst_kwds)
    341 src_window = windows.from_bounds(int_w, int_s, int_e, int_n, src.transform)
    343 # 3. Compute the destination window
--> 344 dst_window = windows.from_bounds(
    345     int_w, int_s, int_e, int_n, output_transform
    346 )
    348 # 4. Read data in source window into temp
    349 src_window_rnd_shp = src_window.round_lengths()

File ~/miniconda3/envs/tf212/lib/python3.9/site-packages/rasterio/windows.py:324, in from_bounds(left, bottom, right, top, transform, height, width, precision)
    322     raise WindowError("Bounds and transform are inconsistent")
    323 if (bottom - top) / transform.e < 0:
--> 324     raise WindowError("Bounds and transform are inconsistent")
    326 rows, cols = rowcol(
    327     transform,
    328     [left, right, right, left],
    329     [top, top, bottom, bottom],
    330     op=float,
    331 )
    332 row_start, row_stop = min(rows), max(rows)

WindowError: Bounds and transform are inconsistent
iamtekson commented 10 months ago

Possible solution

Here are a few steps you can take to troubleshoot this issue:

  1. Check Coordinate Systems: Verify that all the datasets or images involved in the mosaic operation share the same coordinate system. Even a slight difference could cause inconsistency.

  2. Confirm Bounds: Ensure that the bounding coordinates (left, bottom, right, top) are correctly defined and consistent with the coordinate system used.

  3. Transformation Information: Double-check the transformation or georeferencing information associated with your raster datasets. Ensure they are accurate and consistent.

  4. Data Integrity: Check the integrity of your raster data files. Sometimes corrupt or incomplete files can cause issues with their transformation information.

iamtekson commented 10 months ago

This might be an issue from rasterio library: https://github.com/rasterio/rasterio/issues/2976

iamtekson commented 10 months ago

The workaround for this issue would be manually remove below if condition in rasterio.windows.py file,

if (right - left) / transform.a < 0:
    raise WindowError("Bounds and transform are inconsistent")
if (bottom - top) / transform.e < 0:
    raise WindowError("Bounds and transform are inconsistent")
iamtekson commented 10 months ago

By doing so many tests, I found that if the raster have float64 dtype, it create the problem. When I check, I found that, in float64 dtype raster, generate_tiles start creating the tiles from bottom left of the raster moving in the top and right direction while for other dtype raster, it start creating the tiles from top left moving in the bottom and right direction. It is still kind of strange.