Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Convert Between GeoTIFF and CSV

3 Options For Converting Between GeoTIFF and CSV

In geospatial data, two formats often stand out due to their ubiquity and versatility: GeoTIFF and CSV. While GeoTIFF is celebrated for its ability to store raster images with embedded geographic metadata, CSV is known for its simplicity and widespread use in data analysis tools.

But how does one bridge the gap between these two formats? Whether you’re a GIS professional aiming to manipulate raster values in a spreadsheet or a data analyst venturing into the geospatial domain, understanding the intricacies of converting between GeoTIFF and CSV is invaluable.

If you are reading this … You should be listening to our podcast!

Convert GeoTIFF to CSV Using GDAL in Python

  1. Import Necessary Libraries:
   import gdal
   import pandas as pd
  1. Open the GeoTIFF File:
   dataset = gdal.Open('path_to_geotiff_file.tiff')
  1. Convert GeoTIFF to XYZ Format:
   gdal.Translate('output.xyz', dataset, format='XYZ')
  1. Read XYZ into Pandas DataFrame:
   df = pd.read_csv('output.xyz', sep=' ', header=None, names=['x', 'y', 'value'])
  1. Save DataFrame to CSV:
   df.to_csv('output.csv', index=False)

Convert CSV to GeoTIFF Using GDAL in Python

  1. Import Necessary Libraries:
   import gdal
   import pandas as pd
  1. Read CSV into Pandas DataFrame:
   df = pd.read_csv('path_to_csv_file.csv')
  1. Write DataFrame to XYZ Format:
   df.to_csv('input.xyz', sep=' ', header=False, index=False)
  1. Create a VRT File:
  • Create a .vrt file (e.g., input.vrt) with the following content:
    <OGRVRTDataSource> <OGRVRTLayer name="input"> <SrcDataSource>input.xyz</SrcDataSource> <GeometryType>wkbPoint</GeometryType> <LayerSRS>EPSG:your_epsg_code</LayerSRS> <GeometryField encoding="PointFromColumns" x="x" y="y" z="value"/> </OGRVRTLayer> </OGRVRTDataSource>
  1. Convert XYZ to GeoTIFF:
   gdal.Grid('output.tiff', 'input.vrt')

Note: Ensure you have the GDAL library installed and set up in your Python environment. Adjust the paths and filenames as per your requirements. The EPSG code in the VRT file should match the spatial reference system of your data.

Converting between GeoTIFF and CSV in QGIS is straightforward. Here’s a step-by-step guide:

Convert GeoTIFF to CSV Using QGIS

  1. Open QGIS and load your GeoTIFF raster file by going to Layer > Add Layer > Add Raster Layer.
  2. Once the raster is loaded, go to Raster > Conversion > Translate (Convert format).
  3. In the Translate window:
  • For Input layer, select your GeoTIFF file.
  • For Output file, provide a path and name for the XYZ file.
  • In the Output data type dropdown, select XYZ.
  • Click Run.
  1. You’ll now have an XYZ file. To convert this to CSV, you can simply rename the file extension from .xyz to .csv or use any text editor or spreadsheet software to save it as a CSV.

Convert CSV to GeoTIFF Using QGIS

  1. Prepare the CSV: Ensure your CSV has columns named ‘x’, ‘y’, and ‘z’ (or similar) representing the coordinates and the raster value, respectively.
  2. Open QGIS and go to Layer > Add Layer > Add Delimited Text Layer.
  3. Browse and select your CSV file. Ensure the correct columns are set for X and Y fields. Click Add.
  4. Once the points are loaded, go to Processing > Toolbox. In the search bar of the toolbox, type “grid” and select the Grid (Interpolation) tool.
  5. In the Grid window:
  • For Input layer, select your CSV points layer.
  • For Interpolation attribute, select the column representing the raster value (e.g., ‘z’).
  • For Output raster, provide a path and name for the GeoTIFF file.
  • Adjust other parameters as needed (e.g., Interpolation method, Cell size).
  • Click Run.
  1. The GeoTIFF will be generated and loaded into QGIS.

Note: The conversion from CSV to GeoTIFF involves interpolation, so the resulting raster might not be identical to the original, especially if the CSV data is sparse. Adjust the interpolation method and parameters as needed.

Using GDAL command-line utilities, you can convert between GeoTIFF and CSV. Here’s a step-by-step guide:

Convert GeoTIFF to CSV Using GDAL Command-line Utilities

  1. Convert GeoTIFF to XYZ:
   gdal_translate -of XYZ input.tif output.xyz
  1. Convert XYZ to CSV:
    The output from the above command is essentially a CSV with spaces. If you specifically want commas, you can use a tool like awk or sed:
   sed 's/ /,/g' output.xyz > output.csv

Convert CSV to GeoTIFF Using GDAL Command-line Utilities

  1. Prepare a VRT file:
    Before converting the CSV to GeoTIFF, you need a VRT file to describe the data structure. Create a file named input.vrt with the following content:
   <OGRVRTDataSource>
       <OGRVRTLayer name="input">
           <SrcDataSource>input.csv</SrcDataSource>
           <GeometryType>wkbPoint</GeometryType>
           <LayerSRS>EPSG:your_epsg_code</LayerSRS>
           <GeometryField encoding="PointFromColumns" x="x" y="y" z="value"/>
       </OGRVRTLayer>
   </OGRVRTDataSource>

Replace your_epsg_code with the appropriate EPSG code for your data.

  1. Convert CSV to GeoTIFF:
   gdal_grid -a method=invdist:power=2.0:smoothing=1.0 -zfield "value" -of GTiff -ot Float32 -l input input.vrt output.tif

Adjust the method and parameters as needed. The example uses inverse distance weighting for interpolation.

Note: Ensure you have the GDAL tools installed on your system and accessible from the command line. Adjust the paths and filenames as per your requirements. The EPSG code in the VRT file should match the spatial reference system of your data.

Frequently asked questions (FAQs) about the process of converting between GeoTIFF and CSV:

What is a GeoTIFF?

Why would I want to convert a GeoTIFF to CSV?

  • Converting to CSV can make it easier to view or manipulate the raster data in spreadsheet software or to use the data in statistical analysis software.

How do I handle NoData values during the conversion?

  • Depending on the software or method used, you might need to specify how to handle NoData values, either by omitting them, setting a specific value, or handling them in post-processing.

Can I convert a multi-band GeoTIFF to CSV?

  • Yes, but each band will typically be represented as a separate column in the CSV, or you might need to create separate CSV files for each band.

How do I convert back from CSV to GeoTIFF without losing spatial information?

  • It’s crucial to ensure that the CSV contains the necessary x and y coordinate columns. When converting back to GeoTIFF, you’ll also need to specify the spatial reference system if it wasn’t preserved in the CSV.

Will the conversion process affect the quality of my data?

  • Converting to CSV and back to GeoTIFF should not inherently degrade the data quality, but rounding or data type conversion might introduce minor changes.

How can I handle large GeoTIFF files when converting to CSV?

  • Large GeoTIFFs can result in massive CSV files. It might be beneficial to subset or resample the raster data before conversion, especially if the CSV becomes too large to manage.
About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.

Leave a Reply