[Python] Pysheds `nodata` value not representable in dtype of array

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    When I use pysheds in the following example code, I got the error of nodata value not representable in dtype of array in the line of fdir = fdir = grid.flowdir(inflated_dem). Could you please help me to solve it?

    import numpy as np
    from pysheds.grid import Grid

    # Instantiate grid from raster
    dem_file = 'CLSA_LiDAR.tif'
    grid = Grid.from_raster(dem_file)
    dem = grid.read_raster(dem_file)

    # Resolve flats and compute flow directions
    inflated_dem = grid.resolve_flats(dem)
    fdir = grid.flowdir(inflated_dem)

    # Compute accumulation
    acc = grid.accumulation(fdir)


    The following is the error message I got.

    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call last)
    /usr/local/lib/python3.11/dist-packages/pysheds/sview.py in __new__(cls, input_array, viewfinder, metadata)
    84 try:
    ---> 85 assert np.can_cast(viewfinder.nodata, obj.dtype, casting='safe')
    86 except:

    TypeError: can_cast() does not support Python ints, floats, and complex because the result used to depend on the value.
    This change was part of adopting NEP 50, we may explicitly allow them again in the future.

    During handling of the above exception, another exception occurred:

    TypeError Traceback (most recent call last)
    4 frames
    /usr/local/lib/python3.11/dist-packages/pysheds/sview.py in __new__(cls, input_array, viewfinder, metadata)
    85 assert np.can_cast(viewfinder.nodata, obj.dtype, casting='safe')
    86 except:
    ---> 87 raise TypeError('`nodata` value not representable in dtype of array.')
    88 # Don't allow original viewfinder and metadata to be modified
    89 viewfinder = viewfinder.copy()

    TypeError: `nodata` value not representable in dtype of array.

    Continue reading...

Compartilhe esta Página