0

I have a tiff file with an existing raster attribute table. I am adding a column in from a csv file. From the print statements, it seems to be adding it correctly; however, it's not saving for some reason. I don't see it when I load it in ArcMap.

On the first run of the script, I expect the first print statement to produce an error and the second print statement to work. Both of which happens as expected. On the second run, I would expect both print statements to work but the first one still doesn't work indicating that it's not saving.

This is gdal 2.4.2.

edit: I put a print statement after the "band.SetDefaultRAT(rat)" line and that isn't printing so I assume that line is producing some sort of error killing the script. But it's not showing me any error in the terminal either. Not sure what I'm doing wrong.

edit2: I tried removing basically everything in between getting the rat and setting the rat (with no changes in between) and it still seems to not do anything past that band.SetDefaultRAT(rat) line. So that line seems to be causing me issues. Not sure what the issue is though.

import gdal
import csv
import numpy

csv_file = "location"
tiff_file = "location"
col_fields = numpy.loadtxt(csv_file, dtype=str, delimiter=';', usecols=(6))
ds = gdal.Open(tiff_file, gdal.GA_Update)
band = ds.GetRasterBand(1)
rat = band.GetDefaultRAT()
print(rat.GetValueAsString(15,4)) #gives error for not existing (theoretically should work on 2nd run but it doesn't)
rat.CreateColumn("Name", gdal.GFT_String, gdal.GFU_Generic)
for index in range(rat.GetRowCount()):
    rat.SetValueAsString(index, 4, str(col_fields[index]))
print(rat.GetValueAsString(15,4)) #successfully prints
band.SetDefaultRAT(rat)
ds = None
5
  • Python stops running when you get a error so the rest of your script is not executed
    – Ian Turton
    Commented Apr 14, 2021 at 9:54
  • I thought that might be it but even when I remove the print statements, which then I don't get any errors, it still seems to not be saving.
    – BlueCoop
    Commented Apr 14, 2021 at 9:57
  • I put a print statement after the "band.SetDefaultRAT(rat)" line and that isn't printing. So I think that's where the error is. But it's not showing me any error in the terminal either. Not sure what I'm doing wrong.
    – BlueCoop
    Commented Apr 14, 2021 at 10:06
  • Do you need a rat.setRowCount() anywhere? Try printing rat.getRowCount(). Maybe it's zero.
    – wingnut
    Commented Apr 14, 2021 at 11:04
  • GetRowCount() gets me the expected value and it's the same as the length of col_fields as expected. I also checked GetColumnCount() and that seems to be adding correctly. I tried removing basically everything in between getting the rat and setting the rat (with no changes in between) and it still seems to not do anything past that band.SetDefaultRAT(rat) line. So that line seems to be causing me issues. Not sure if I'm using the wrong syntax or something?
    – BlueCoop
    Commented Apr 14, 2021 at 11:16

1 Answer 1

0

Still not sure what the issue was, but I was able to work around it by creating new RasterAttributeTable object and copying everything over from the current one along with the new column.

Then using the new RasterAttributeTable object as argument for SetDefaultRAT.

Your Answer

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

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