0

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[0].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'>

  • 2
    Please don't delete and repost. Even if you don't incite anti-spam defenses, you still waste the effort of editing. – Vince Jul 8 at 13:36
  • I just wanted to change the title so that the issue is described better :/ – soph Jul 8 at 14:08
  • 1
    there is an edit link for just that sort of change – Ian Turton Jul 8 at 14:14
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works. – Ian Turton Jul 8 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.