I'm trying to merge some rasters with gdalbuildvrt in a python script:

rasters = glob.glob('/{0}/ndvi_masked/*.tif'.format(path))

gdal_cmd = 'gdalbuildvrt -q -vrtnodata "0" -overwrite {0}.vrt'.format(ndvi_name)

for pix in rasters:
    gdal_cmd += ' {0}'.format(pix)

subprocess.call(gdal_cmd, shell=True)

gdal_cmd = 'gdal_translate -of GTiff %s.vrt  %s_clip.tif' % (ndvi_name, ndvi_name)

subprocess.call(gdal_cmd, shell=True)

But the result is really weird, cause I losted some pixels as you can see on the 2nde image below.

All displayed rasters All displayed rasters

Merge raster Merge raster

  • I tried with gdal_merge.py and the result is the same. Does somebody have an idea? – Tim C. Aug 7 '18 at 13:31
  • 3
    Could you share some small dataset for testing? – user30184 Aug 7 '18 at 14:47
  • Perhaps you need to specify -srcnodata rather than -vrtnodata – mikewatt Aug 7 '18 at 22:37

I found a solution who works perfectly. I added this piece of code before my first code:

rasters = glob.glob('/{0}/ndvi_clip/*.tif'.format(path))

for pix in rasters:
    gdal_cmd = 'gdal_translate -a_nodata nan {0} {1}/{2}_{3}.tif'.format(pix, noNaN_path, ndvi_name, rasters.index(pix))
    subprocess.call(gdal_cmd, shell=True)

I don't know why, but nan values in my raster weren't real nodata. With this command, I convert nan to nodata.

Here's the doc of this command: gdal_translate

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.