1

I have 3 raster files (.tiff files) which are geographically 3 consecutive tiles of a particular area. I am trying to make a .vrt file of 2 out of these 3 files using the gdalbuildvrt python command. The issue I am facing is suppose if I am making a .vrt file (named test.vrt) of the first 2 files (say file 1 & 2) and opening it with rasterio python, it's opening correctly. Subsequently, I want to create a .vrt file of the new file combinations (say file 1 & 3) and overwrite the old .vrt file (test.vrt) with this file combination and try to open it with rasterio python. The problem I am facing now is it's still displaying the the .vrt file with the old file combinations (having files 1 & 2 and not files 1 & 3). The code which I used till now is:

import rasterio 
import rasterio.plot

from osgeo import gdal

vrt_options = gdal.BuildVRTOptions(resampleAlg='cubic', addAlpha=True)
test_vrt = gdal.BuildVRT(r'D:\Test TIFF\test.vrt', [r'D:\Test TIFF\Tile_58_LULC250k_2015_16_revised.tif', r'D:\Test TIFF\Tile_60_LULC250k_2015_16_revised.tif'], options=vrt_options)

test_vrt = None

raster_vrt = rasterio.open(r'D:\Test TIFF\test.vrt')

rasterio.plot.show(raster_vrt)

Also, if I am opening this new .vrt file with QGIS its displaying the correct file combinations (file 1 & 3). So what I understood is the .vrt file has changed after running the gdalbuildvrt command but there is some problem while opening it with rasterio python because it's displaying old .vrt file (file 1 & 2).

Can someone help me figure out what is the problem?

  • Could be a race condition, or some kind of file cache. To confirm, try give each combination of files a different VRT file name, rather than re-using "D:\Test TIFF\test.vrt". If that's the case, then work on a different strategy for creating your VRTs, one that emphasises atomicity. e.g. write to a temporary file, then move (mv in Linux) the temporary file once it has been written and closed. See for example: stackoverflow.com/questions/2333872/… This is to avoid prematurely reading a file that hasn't actually been (over-)written. – Richard Law May 10 at 4:29
  • Thanks @RichardLaw for your kind suggestion. I just added raster_vrt.close() at the end of the program and the issue got resolved. Now I am able to overwrite the same vrt file with different file combinations. – RRSC NGP May 10 at 4:55
0

With kind suggestion from @Richard Law, adding raster_vrt.close() at the end of the program solved the issue....

import rasterio 
import rasterio.plot

from osgeo import gdal

vrt_options = gdal.BuildVRTOptions(resampleAlg='cubic', addAlpha=True)
test_vrt = gdal.BuildVRT(r'D:\Test TIFF\test.vrt', [r'D:\Test TIFF\Tile_58_LULC250k_2015_16_revised.tif', r'D:\Test TIFF\Tile_60_LULC250k_2015_16_revised.tif'], options=vrt_options)

test_vrt = None

raster_vrt = rasterio.open(r'D:\Test TIFF\test.vrt')

rasterio.plot.show(raster_vrt)

raster_vrt.close()

  • 3
    You could also do with rasterio.open('D:\Test TIFF\test.vrt') as vrt: which would implicitly close the file when appropriate. This is a very common pattern in Python. effbot.org/zone/python-with-statement.htm – Richard Law May 10 at 5:14
  • Yup definitely that will be an another option as well.... :) Thanks @RichardLaw – RRSC NGP May 10 at 6:25

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.