3

I have a directory containing multiple .tif files which I want to reclassify and save to a new directory. The following code already worked for a single raster. Unfortunately, I don't know how to use driver.CreateCopy to write multiple files.

 in_directory = 'D:/Python/Copied'
    file = glob.glob(os.path.join(in_directory, '*.tif'))
    for data_path in file:
        raster = gdal.Open(data_path, gdal.GA_ReadOnly)
        myarray = raster.GetRasterBand(1).ReadAsArray()
        # modify numpy array to mask values
        myarray[myarray == 0] = 1
        myarray[myarray > 1] = 0


        # open output dataset, which is a copy of original
        driver = gdal.GetDriverByName('GTiff')
        ds_out = driver.CreateCopy("D:/Python/Copied/reclass/neu.tif", raster)
        # write the modified array to the raster
        ds_out.GetRasterBand(1).WriteArray(myarray)
        # set the NoData metadata flag
        ds_out.GetRasterBand(1).SetNoDataValue(0)
        # clear the buffer, and ensure file is written
        ds_out.FlushCache()

closed as off-topic by alphabetasoup, Fran Raga, Taras, user2856, Jochen Schwarze Jul 1 at 5:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – alphabetasoup, Fran Raga, Taras, user2856, Jochen Schwarze
If this question can be reworded to fit the rules in the help center, please edit the question.

2

Your code looks reasonable as is, with one exception: you're using the same output file name for every output when looping. Change this line: ds_out = driver.CreateCopy("D:/Python/Copied/reclass/neu.tif", raster) to use a file name that is different each iteration. It can be as simple as:

in_directory = 'D:/Python/Copied'
files = glob.glob(os.path.join(in_directory, '*.tif'))
for i, data_path in enumerate(files):
   raster = gdal.Open(data_path, gdal.GA_ReadOnly)
   output_path = f'D:/Python/Copied/reclass/neu.{i}.tif'
   myarray = raster.GetRasterBand(1).ReadAsArray()
   myarray[myarray == 0] = 1
   myarray[myarray > 1] = 0
   driver = gdal.GetDriverByName('GTiff')
   ds_out = driver.CreateCopy(output_path, raster)
   ds_out.GetRasterBand(1).WriteArray(myarray)
   ds_out.GetRasterBand(1).SetNoDataValue(0)
   ds_out.FlushCache()

This uses an f-String to insert the iteration number (i) into the file name. You probably want to refine that so it uses part of data_path in the name so you have an idea of the provenance. I don't know enough about what your intention is to make a better decision.

  • Thank you, awesome. Worked like a charm. – remotesatellite Jun 29 at 11:29
  • Do you have any idea how to retain the filenames from the input folder? – remotesatellite Jun 29 at 12:16
  • Found it out by myself: test = os.path.splitext(os.path.split(data_path)[1])[0] output_path = f'D:/Python/Copied/reclass/{test}.tif' – remotesatellite Jun 29 at 15:51

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