osgeonepal / geotile

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

`gt.merge_tiles` method is not working as expected! #53

Closed iamtekson closed 6 months ago

iamtekson commented 10 months ago

In merge_tiles method, the current implementation doesn't merge the tiles and generate the correct merged file. Please rewrite the function to fix this bug.

hillsonghimire commented 6 months ago

I suspect the issue might be due to pixel bleeding a little outside the initial input data bounds. image

Potential solution:

  1. Equal padding the input on all side in tile generating step, so that the width or height = (tile_size * stride_x or stride_y)
  2. After merging remove the extra padding.

image

hillsonghimire commented 6 months ago
k=-1
for j in range(y_gen):
    for i in range(x_gen):
        k=k+1
        z[i*stride_x:(i+1)*stride_x, j*stride_y:(j+1)*stride_y]=tile_dta[k]

// WRITE CODE TO UPDATE **height**, and **width** in **self.meta:**
// **width=new_width, height=new_height**, before using in block below:

with rasterio.open('output.tif', "w",  **self.meta) as outds:
        outds.write(z.astype('uint8'))

pyplot.imshow(z)

As suspected the issue is due to width and height not updated in the **self.meta before writing. I was able to regenerate the image with above modification. Also the performance was greatly improved by first reconstructing the numpy array from tile (since both doesn't have coordinate, it makes sense as well), then writing it on the file, instead of for loop in reading and writing operation, which is more resource intensive.

image

Disc: notice slight padding at the bottom and right after the operation.

iamtekson commented 6 months ago

For now, I am simply saving all the generated tiles into a new folder and mosaicing it by using gt.mosaic() function. Yes, you are right, we have some issues in merge_tile method and I feel like we need to rewrite the function. So, were you able to solve the issue? if yes, please send a PR.