1

I am looking for a way to convert GeoTIFF raster file into NetCDF file using Python Jupyter notebook. I have tried function gdal.translate and xarray (.to_netcdf) (as below), but both of them gave me the results with changing the Affine Transformation. I also tried with QGIS by function/button <Right click on GeoTIFF raster file in QGIS --> Export --> Save as --> Choose NetCDF>, and this way works perfectly file. However, I need to code the process rather than do it manually with only QGIS as there will be more than one GeoTIFF file. Do you know how to convert from GeoTIFF raster file into NetCDF without changing any information (including Affine transformation values) using gdal, xarray or any other packages? Or do you know how I could code the process "saving as netcdf file" of QGIS in Python Jupyter notebook (not from Python Console in QGIS)? Here below is my code with gdal.translate() and xarray.

  • With gdal.translate:
from osgeo import gdal

input = "SP27GTIF_rotate.tiff"
output = "SP27GTIF_rotate.nc"

file = gdal.Translate(output, input, format='NetCDF')

This code I found from here https://nsidc.org/support/faq/how-can-i-convert-geotiff-netcdf

  • With xarray
import xarray as xr

tiff_file = xr.open("SP27GTIF_rotate.tiff")
tiff_file.to_netcdf("SP27GTIF_rotate.nc")

This code I found from here Write projected array to netcdf file, best practice

The GeoTiff file can be downloaded from here https://ufile.io/aed4xjvf (then choose Free Download --> Slow Speed)

This question can be a next question from what I asked for help before Rotating raster using Python

1 Answer 1

1

https://github.com/corteva/rioxarray/

import rioxarray

tiff_file = rioxarray.open_rasterio("SP27GTIF_rotate.tiff")
tiff_file.to_netcdf("SP27GTIF_rotate.nc")
0

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.