python (scipy) TypeError

MRAB python at mrabarnett.plus.com
Mon Oct 3 12:45:28 EDT 2016


On 2016-10-03 17:14, chrischris201444 at gmail.com wrote:
> hello
>
>
> i try to follow some tutorial but i have that error :
>
> Traceback (most recent call last):
>   File "C:\Python27\test\test\earth.py", line 42, in <module>
>     slope_array = np.ones_like(data_array) * nodataval
> TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
>
The errors is saying that nodataval is None.

>
> first i define that lines of code :
>
> data_array = raster2array(filename)
> nodataval = getNoDataValue(filename)
>
> and the line with error :
>
> slope_array = np.ones_like(data_array) * nodataval
>
> how can i fix this error ?i change type field ?
>
> on the four prints:
>
Look at what print(nodataval) shows. It says that nodataval is None.

> print(resolution)
> print(nodataval)
> print(type(data_array))
> print(data_array.shape)
>
> i take that exports :
>
> {'east-west': 0.0002777777777777778, 'north-south': 0.0002777777777777778}
> None
> <type 'numpy.ndarray'>
> (3601, 3601)
>
> the full code :
>
Why is nodataval None? It's because getNoDataValue returned None, which 
means that band.GetNoDataValue() returned None.

You'll have to read the docs to see under what conditions it does that.

>
> from __future__ import division
> from osgeo import gdal
> from matplotlib.colors import ListedColormap
> from matplotlib import colors
> import sys
> import numpy as np
> import matplotlib
> import matplotlib.pyplot as plt
> import math
>
> filename = 'dem.tif'
>
> def getResolution(rasterfn):
>     raster = gdal.Open(rasterfn)
>     geotransform = raster.GetGeoTransform()
>     res = {"east-west": abs(geotransform[1]),
>            "north-south": abs(geotransform[5])}
>     return res
>
> def raster2array(rasterfn):
>     raster = gdal.Open(rasterfn)
>     band = raster.GetRasterBand(1)
>     return band.ReadAsArray()
>
> def getNoDataValue(rasterfn):
>     raster = gdal.Open(rasterfn)
>     band = raster.GetRasterBand(1)
>     return band.GetNoDataValue()
>
> data_array = raster2array(filename)
> nodataval = getNoDataValue(filename)
> resolution = getResolution(filename)
> print(resolution)
> print(nodataval)
>
> print(type(data_array))
> print(data_array.shape)
>
[snip]




More information about the Python-list mailing list