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
1 Answer 1
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
.
creationOptions = ['None'],
.