3

I'm trying to get a raster from a DSM running the following code from python libraries osgeo-gdal in python qgis console:

import pandas as pd 
from osgeo import gdal

df = pd.read_csv(azimut_altitude.csv')
src_ds = gdal.Open(my_raster.tif')

#array generation function
def shade_array (index):
    out = r'.\my_path\raster'
    azimut = df.azimut[index]
    altitude = df.altitude[index]
    data = gdal.DEMProcessingOptions(options = [],
    colorFilename=None,
    format="GTiff",
    creationOptions=None,
    computeEdges=False,
    alg='Horn',
    band=1,
    zFactor=None,
    scale=None,
    azimuth=azimut,
    altitude=altitude,
    combined=False,
    multiDirectional = False,
    slopeFormat=None,
    trigonometric=False,
    zeroForFlat=False,
    addAlpha=None,
    callback=None,
    callback_data=None)

    shade = gdal.DEMProcessing(out + str(index), src_ds, 'hillshade')

    return shade

shade_array(0)

The problem is that when I run the script it yields this message:

ValueError: Received a NULL pointer. 

I've tried to solve introducing the input file as an array but then the error message is this:

TypeError: in method 'DEMProcessingInternal', argument 2 of type 'GDALDatasetShadow *'
1
  • your code has a typo on line 4, you forgot to add the starting quote – Johan Feb 18 '20 at 13:31
4
+50

The error message that you get indicates that there is something wrong with your input dataset. I think this is because there are formatting and syntax errors in your code. I have edited your script so that it should work:

import pandas as pd 
from osgeo import gdal
import os

df = pd.read_csv("azimut_altitude.csv")
src_ds = gdal.Open("my_raster.tif")

#array generation function
def shade_array (index):
    outdir = '/path/to/outdir' # i'm not sure if the path you provided is valid
    outfn = os.path.join(outdir, index + ".tif")
    azimut = df.azimut[index]
    altitude = df.altitude[index]

    # Most of the options that you have in your example are the defaults. 
    # These do not have to be set when you are satisfied with the defaults.
    # I have kept them here fow now, but feel free to remove anything that is 
    # not different from the default argument
    options = dict(
        colorFilename=None,
        format="GTiff",
        creationOptions=None,
        computeEdges=False,
        alg='Horn',
        band=1,
        zFactor=None,
        scale=None,
        azimuth=azimut,
        altitude=altitude,
        combined=False,
        multiDirectional = False,
        slopeFormat=None,
        trigonometric=False,
        zeroForFlat=False,
        addAlpha=None,
        callback=None,
        callback_data=None
    )

    # In your example you did not provide the options to the function. Below, it is done:
    ds = gdal.DEMProcessing(outfn, src_ds, 'hillshade', **options)

    # I have assumed that you would like to receive the actual data, 
    # not the file that is written on disk. 
    # You can easily modify this example if you want something else to be returned.
    data = ds.ReadAsArray()

    # close the dataset, by setting the ds to None.
    ds = None
    return data
3
  • Hi Johan, it yields ds as a Nontype object so it can't run the function ds.ReadAsArray(). How can I fix it? – Rodrigo Vargas Feb 20 '20 at 13:14
  • 3 things come to mind: 1) check if your input is a valid dataset (e.g. does it open in ArcGIS / QGIS?), 2) check if your output directory does exist and 3) verify that you have not by accident opened your output file in QGIS or Python. – Johan 29 mins ago – Johan Feb 20 '20 at 14:20
  • You may want to enable exceptions to help avoid silent errors when e.g. an invalid path was used to open the dataset – mikewatt Feb 20 '20 at 19:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.