1

I have a NetCDF with geographical information. I'd like to obtain a GeoTIFF file with rasterio. The problem is how to translate the geographical information from CF standard to something that GeoTIFF can understand.

In the netCDF I have this variable:

int projection_lambert ;
    projection_lambert:grid_mapping_name = "lambert_conformal_conic" ;
    projection_lambert:latitude_of_projection_origin = 63. ;
    projection_lambert:longitude_of_central_meridian = 15. ;
    projection_lambert:standard_parallel = 63., 63. ;
    projection_lambert:proj4 = "+proj=lcc +lat_0=63 +lon_0=15 +lat_1=63 +lat_2=63 +no_defs +R=6.371e+06" ;

Therefore I can create the CRS with:

crs = CRS.from_proj4("+proj=lcc +lat_0=63 +lon_0=15 +lat_1=63 +lat_2=63 +no_defs +R=6.371e+06")

OK, I know the projection, but what is missing is the Affine transformation. This information is implicitly stored in the lat and lon variables the NetCDF also includes, but I'm not sure how to go from there, to the Affine transformation.

1

2 Answers 2

1

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

https://corteva.github.io/rioxarray/stable/examples/convert_to_raster.html


import rioxarray

rds = rioxarray.open_rasterio(...)
rds.rio.to_raster(...)
0
import xarray as xr

var_name = 'name'
nc_file = xr.open_dataset('/path/to/nc/file.nc')
var = nc_file[var_name]

# Get affine as affine.Affine object
aff = var.rio.transform()
# Get x_min, y_min, x_max, y_max bounds
bou = var.rio.bounds()

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.