Skip to content
Snippets Groups Projects
sandbox_raster.py 2.13 KiB
Newer Older
Clemens Berteld's avatar
Clemens Berteld committed
import configparser
import numpy as np
import psycopg2
import pandas as pd
from osgeo import gdal, osr

cfg = configparser.ConfigParser()
cfg.read('../config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
param_postgres = cfg["POSTGRES"]
param_interpol = cfg["INTERPOLATION"]

db_name, user, pw, host, port = param_postgres['dbName'], param_postgres["user"], param_postgres["password"], param_postgres["host"], param_postgres["port"]

with psycopg2.connect(database=db_name, user=user, password=pw, host=param_postgres["host"], port=port) as connection:
    with connection.cursor() as cursor:
        cursor.execute('SELECT station_id, "2018" FROM stations WHERE file IS NULL ORDER BY station_id ASC;')
        pixel_array = []
        results = cursor.fetchall()
        for j in range(0, 36):
            row_array = []
            for i, station_id in enumerate(results):
                if i % 36 == 0:
                    row_array.append(results[i + j][1])
                    # print(row_array)
            pixel_array.append(row_array)
        np_pixel_array = np.array(pixel_array)
        print(np_pixel_array)

        xmin, ymin, xmax, ymax = [5.01, 47.15, 14.81, 55.33]
        nrows, ncols = np.shape(np_pixel_array)
        xres = (xmax - xmin) / float(ncols)
        yres = (ymax - ymin) / float(nrows)
        geotransform = (xmin, xres, 0, ymax, 0, -yres)

        output_raster = gdal.GetDriverByName('GTiff').Create('D:/Uni/Master/01_SS2021/Automatisierte_Geodatenprozessierung/temperaturverteilung/dataacquisition/output/myraster.tif', ncols, nrows, 1, gdal.GDT_Float32)  # Open the file
        output_raster.SetGeoTransform(geotransform)  # Specify its coordinates
        srs = osr.SpatialReference()  # Establish its coordinate encoding
        srs.ImportFromEPSG(4326)  # This one specifies WGS84 lat long.
        # Anyone know how to specify the
        # IAU2000:49900 Mars encoding?
        output_raster.SetProjection(srs.ExportToWkt())  # Exports the coordinate system
        # to the file
        output_raster.GetRasterBand(1).WriteArray(np_pixel_array)  # Writes my array to the raster

        output_raster.FlushCache()