2

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?

2
  • 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. May 10, 2019 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, 2019 at 4:55

2 Answers 2

2

A common idiom is to use a with statement when opening a file so that it is automatically closed when the with block is complete. To adapt the existing answer:

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

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

    rasterio.plot.show(raster_vrt)
1

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()

4
  • 5
    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 May 10, 2019 at 5:14
  • 1
    Yup definitely that will be an another option as well.... :) Thanks @RichardLaw
    – RRSC NGP
    May 10, 2019 at 6:25
  • @alphabetasoup please turn that into an answer so that we can vote it higher than this one ;) Mar 3, 2020 at 14:19
  • 1
    @bugmenot123 done Mar 3, 2020 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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