[GIS] Convert TIFF file with multiple bands into a single CSV file

csvgdalgdal-translategeotiff-tiff

I have a GeoTIFF file which contains 13 bands and I want to create one .csv file using gdal_translate, but by default GDAL generates one CSV with one band's data

I want this kind of CSV:

 lat     longi  band1 band2 band3 band4
 2.565   45.545  45    65    34    34   

I want to know if it is possible using GDAL.

gdal_translate -of XYZ -b 4 -b 8 subset_S2A_OPER_MTD_SAFL1C_PDMC_20161017T172323_R019_V20161016T050802_20161016T051302_resampled_b4_b8_reprojected.tif subset_S2A_OPER_MTD_SAFL1C_PDMC_20161017T172323_R019_V20161016T050802_20161016T051302_resampled_b4_b8_reprojected.csv

Here only one band is selected ……

Best Answer

AFAIK gdal_translate is able to convert only one band into XYZ format, so I suggest the following workflow:

  1. use gdal_translate to convert each band into a CSV file;
  2. use ogr2ogr to append each CSV file to an SQLite file;
  3. use ogrinfo to create the index on the ogc_fid field of each SQLite table in order to make the last step fast;
  4. finally, join all the tables and get the desired result.

This is a sample batch file where the above steps are implemented for only three bands (change the FILENAME variable and adapt the join query according to your input data):

set FILENAME=test
if exist %FILENAME%.sqlite del %FILENAME%.sqlite
for /L %%i in (1,1,3) do (
gdal_translate -b %%i -of XYZ %FILENAME%.jpg b%%i.csv -co ADD_HEADER_LINE=YES
ogr2ogr -update -append -f SQLite %FILENAME%.sqlite -nln b%%i b%%i.csv -dsco METADATA=NO -dsco INIT_WITH_EPSG=NO
ogrinfo %FILENAME%.sqlite -sql "CREATE INDEX b%%i_ogc_fid_index ON b%%i(ogc_fid)"
)
ogr2ogr -f CSV -sql "SELECT b1.x AS lon, b1.y AS lat, b1.z AS band1, b2.z AS band2, b3.z AS band3 FROM b1 LEFT JOIN b2 ON b1.ogc_fid = b2.ogc_fid LEFT JOIN b3 ON b1.ogc_fid = b3.ogc_fid" %FILENAME%_XYZ.csv %FILENAME%.sqlite -lco SEPARATOR=SPACE