I have a pandas df with X/Y and lat/lon coordinates.
I want to convert the data frame using lat/lon columns and store the TIFF image in WGS84 CRS.
Thanks
I have a pandas df with X/Y and lat/lon coordinates.
I want to convert the data frame using lat/lon columns and store the TIFF image in WGS84 CRS.
Thanks
A couple package recommendations for you: xarray
and rioxarray
.
xarray is pydata's solution to labeled N-dimensional arrays (think pandas but in 3+ dimensions, or think numpy ND-arrays but with pandas indices rather than just positional indices).
rioxarray is an extension package combining xarray with rasterio, giving the ability to read and write raster files including GeoTIFFs. rioxarray has docs on converting xarray DataArrays to rasters. See also the API docs for converting RasterArray
and RasterDataset
objects to rasters.
In your case, assuming your orthogonal dimensions are (lat, lon)
and that model_lat
and model_lon
are in fact indexed by both lat and lon (e.g. they're in a 3D projection), and that res
is the band you'd like to encode, your result would look something like this:
import xarray as xr
import rioxarray
da = df.set_index(['lat', 'lon']).to_xarray()
# promote the data variables model lat/long to 2d coordinates
da = da.set_coords(['model_lat', 'model_long'])
da.res.rio.to_raster(filepath)