Save projected raster as a variable and not as a file Python

1.1k views Asked by At

I would like to reproject a raster and keep working on that reprojected raster instead of loading it again from a file.

To project a raster I use either gdal:

# Source
src = gdal.Open(vv_path, gdalconst.GA_ReadOnly)
src_proj = src.GetProjection()
src_geotrans = src.GetGeoTransform()

# We want a section of source that matches this:
match_ds = gdal.Open(sn2_red_path, gdalconst.GA_ReadOnly)
match_proj = match_ds.GetProjection()
match_geotrans = match_ds.GetGeoTransform()
wide = match_ds.RasterXSize
high = match_ds.RasterYSize

# Output / destination
dst_filename = os.path.join(sn1_processed_path,'vv.tif')
dst = gdal.GetDriverByName('Gtiff').Create(dst_filename, wide, high, 1, gdalconst.GDT_Float32)
dst.SetGeoTransform( match_geotrans )
dst.SetProjection( match_proj)

# Do the work
aa = gdal.ReprojectImage(src, dst, src_proj, match_proj, gdalconst.GRA_NearestNeighbour)

del dst # Flush

or rasterio from here

In both cases, the projected raster is saved to a file, and I have to load it again to procees it. Is it possible to save the projected raster also as a variable?

1

There are 1 answers

10
Val On

You could use VRT datasets:

src = gdal.Open(“reference.tif”)
dst = gdal.Warp(“warped.vrt”, src, format=“vrt”, dstSRS=“EPSG:3857”)

This way only a small VRT file will be created, and you can use the dst dataset in downstream processing at which point the warping will be actually performed.

You can even create the VRT itself in memory, so nothing is written to disk at all:

dst = gdal.Warp(“”, src, format=“vrt”, dstSRS=“EPSG:3857”)

If your dataset fits entirely in memory, you can create the actual dataset in memory using the vsimem virtual file system driver, which has the upside that you have to perform the processing only once if you want to use it downstream in multiple functions:

dst = gdal.Warp(“/vsimem/result_inmemory.tif”, src, format=“tif”, dstSRS=“EPSG:3857”)

This way the processing will be performed immediately, but then you can use the dataset object to e.g. write it to disk and then perform additional processing.