1

I have data from a digital elevation model and would like to perform a viewshed analysis using gdal.viewshedGenerate() in Python. I have transformed my data into a .tif file and performed georeferencing using rasterio. However, when I try to generate the viewshed I receive a TypeError: not a sequence.

I have already considered the advices from this question > An example for Viewshed analysis in Python. I was wondering if the problem might still be the data type of the mode but could not find another solution yet. Does anyone know what might be the problem?

Here is my code:

from osgeo import gdal 

band=gdal.Open("new.tif").GetRasterBand(1)

gdal.ViewshedGenerate(
    srcBand = band,
    driverName = 'GTiff',
    targetRasterName = 'new.tif',
    creationOptions = None,
    observerX = 306900.0,
    observerY = 5654500.0,
    observerHeight = 2.0,
    targetHeight = 80.0,
    visibleVal = 255.0,
    invisibleVal = 0.0,
    outOfRangeVal = 0.0,
    noDataVal = 0.0,
    dfCurvCoeff = 1.0,
    mode = 1,
    maxDistance = 500.0) 

And the Error I receive:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[17], line 1
----> 1 gdal.ViewshedGenerate(
      2     srcBand = band,
      3     driverName = 'GTiff',
      4     targetRasterName = 'new.tif',
      5     creationOptions = None,
      6     observerX = 306900.0,
      7     observerY = 5654500.0,
      8     observerHeight = 2.0,
      9     targetHeight = 80.0,
     10     visibleVal = 255.0,
     11     invisibleVal = 0.0,
     12     outOfRangeVal = 0.0,
     13     noDataVal = 0.0,
     14     dfCurvCoeff = 1.0,
     15     mode = 1,
     16     maxDistance = 500.0)

File ~\Anaconda3\envs\pygmt\Lib\site-packages\osgeo\gdal.py:4568, in ViewshedGenerate(*args, **kwargs)
   4566 def ViewshedGenerate(*args, **kwargs) -> "GDALDatasetShadow *":
   4567     r"""ViewshedGenerate(Band srcBand, char const * driverName, char const * targetRasterName, char ** creationOptions, double observerX, double observerY, double observerHeight, double targetHeight, double visibleVal, double invisibleVal, double outOfRangeVal, double noDataVal, double dfCurvCoeff, GDALViewshedMode mode, double maxDistance, GDALProgressFunc callback=0, void * callback_data=None, GDALViewshedOutputType heightMode=GVOT_NORMAL, char ** options=None) -> Dataset"""
-> 4568     return _gdal.ViewshedGenerate(*args, **kwargs)

TypeError: not a sequence
3
  • 1
    The "not a sequence" error comes from the creationOptions. Try with a sequence creationOptions = ['None'],.
    – user30184
    Commented Mar 27, 2023 at 6:43
  • I wrote an answer to gis.stackexchange.com/questions/434417/… with Python code that works for me. Please have a look.
    – user30184
    Commented Mar 27, 2023 at 7:25
  • i have problems aswell with the arguments. creationOptions=None is not allowed and mode = 1 neither. has anybody made it to work?
    – marta
    Commented Jan 12 at 15:03

1 Answer 1

0

TypeError: not a sequence is due to creationOptions not being a sequence.

If you don't want to set any creation options then use: creationOptions=[]. This works on my system with GDAL 3.7.1.

Note that this is a breaking change as of GDAL 3.7.X where GDAL 3.6.X accepts creationOptions=None.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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