I created a GeoTIFF file from a numpy array, loaded it into a PostgreSQL DB and now I want to retrieve this raster data and convert it back into a numpy array. I know this seems nothing really practically, but its just exemplary for another bigger task.
But when I create the numpy file, it only loads NaN Values. I checked creating the numpy array with another GeoTIFF file (as they describe here: https://gist.github.com/gzagatti/a649674d85b480b98b7d75b31d4c9555)
So the mistake must be happened, when I created the GeoTIFF.
# Create dataset file drv = gdal.GetDriverByName('GTiff') ds = drv.Create('/Path/converted_numpy.tif', 5000, 6635,1,gdal.GDT_Float32) outband=ds.GetRasterBand(1) # Write array to raster outband.WriteArray(array) outband.SetNoDataValue(-999) outband.FlushCache() # Take care of spatial reference ds.SetGeoTransform((414733,0.5,0,5953200,0,-0.5)) dsSRS=osr.SpatialReference() dsSRS.ImportFromEPSG(25832) ds.SetProjection(dsSRS.ExportToWkt())
Does anybody know if something's missing?
EDIT: The Output of the query and reconverting is as follows:
cur.execute(''' SET postgis.gdal_enabled_drivers TO 'GTiff'; WITH temp as ( SELECT ST_Union(rast) As file_rast FROM public.table) SELECT ST_AsGDALRaster(file_rast, 'GTiff') FROM temp;''') for row in cur: np.rast = row.tobytes() with MemoryFile(np.rast).open() as dataset: data_array = dataset.read() print(data_array) max = np.max(data_array) print(max) print(type(max)) print(type(max.item()))
Output: nan, <class 'numpy.float32'> and <class 'float'>