2

I have 4 separate ASCII grids which all contain the same data:

1 2 3 4
3 4 5 6
4 5 6 8
7 9 5 4

The metadata, however, is different:

ncols 4
nrows 4
xllcorner x
yllcorner y
cellsize 1
nodata_value -9999

Where x and y take on the values (0,0), (4,0), (4,4) and (0,4) repectively. I then Try to run a merge operation as follows using rasterio:

from rasterio.merge import merge
import rasterio
import glob 

def merge_datasets(out_filename, dataset_list, driver):
    print('running merge command and outputting mosaic')
    mosaic, out_trans = merge(dataset_list)

    print('updating metadata')
    out_meta = dataset_list[0].meta.copy()
    out_meta.update({"driver": driver, "height": mosaic.shape[1], "width": mosaic.shape[2], "transform": out_trans})

    print('writing file')
    with rasterio.open(out_filename, 'w', **out_meta) as dest: # YES, NO, IF_NEEDED, IF_SAFER
    dest.write(mosaic)

    out_dataset = rasterio.open(out_filename)
    return out_dataset

if __name__=='__main__':
    tiles_filename_list = glob.glob(r'test_data/*.asc')
    tiles_dataset_list = open_dataset_list(tiles_filename_list)

    merged_dataset = merge_datasets(r'test_data/out.asc', tiles_dataset_list, driver='AAIGrid')

This creates the following incorrect grid:

ncols        8
nrows        8
xllcorner    0.000000000000
yllcorner    0.000000000000
cellsize     1.000000000000
NODATA_value -9999
 1 2 3 4 1 2 2 3
 3 4 5 6 3 4 4 5
 3 4 5 6 3 4 4 5
 4 5 6 8 4 5 5 6
 1 2 3 4 1 2 2 3
 3 4 5 6 3 4 4 5
 4 5 6 8 4 5 5 6
 7 9 5 4 7 9 9 5

Does anyone know why this occurs. I would have expected:

ncols        8
nrows        8
xllcorner    0.000000000000
yllcorner    0.000000000000
cellsize     1.000000000000
NODATA_value -9999
 1 2 3 4 1 2 3 4
 3 4 5 6 3 4 5 6
 3 4 5 6 3 4 5 6
 4 5 6 8 4 5 6 8
 1 2 3 4 1 2 3 4
 3 4 5 6 3 4 5 6
 4 5 6 8 4 5 6 8
 7 9 5 4 7 9 5 4

I tried looking into the different options in the merge method in rasterio but found nothing useful.

1

I've realized that adding the argument precision=50 to the merge call solves the problem. I don't fully understand why, though.

4
  • I have recently come across the same problem (but merging tif tiles that amount to billions of raster cells), and 'precision=50' seems to do the job. Have you actually found out what it exactly means and why it fixes it? (Thanks for tip!) Jan 23 '20 at 12:11
  • Not really, but I logged an issue in github.com/mapbox/rasterio/issues/1837 I think there was a pull request which increased the default precision so in theory the head revision of rasterio should not exhibit this behavior anymore. Good luck.
    – user32882
    Jan 23 '20 at 12:16
  • Ok, thanks for letting me know. I just fail to see why the number of decimal points of precision would affect in such a way the merged raster (basically, the values of a big percentage of cells get shifted one position to the right, as in your example). Out of my understanding. Jan 23 '20 at 12:33
  • I can confirm I am still encountering this issue as of rasterio 1.1.5, which a number of cells get shifted one position to the right.
    – Soupoder
    Jul 27 '20 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.