Skip to content

Commit

Permalink
use GDAL's type conversion functions instead (#75)
Browse files Browse the repository at this point in the history
* use GDAL's type conversion functions instead

* Call warnings.warn with stacklevel=2. Apparently this will make it show the source code line where the user called our function, instead of the line where we called warnings.warn, which is plainly better

---------

Co-authored-by: Neil Flood <neilflood@fastmail.fm>
  • Loading branch information
gillins and neilflood committed Jan 18, 2024
1 parent a07d612 commit 9cf63c1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 45 deletions.
48 changes: 10 additions & 38 deletions rios/imageio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy
from osgeo import gdalconst
import warnings
from osgeo import gdal

from . import rioserrors
from osgeo import gdal_array

INTERSECTION = 0
UNION = 1
Expand All @@ -52,49 +50,23 @@ def pix2wld(transform, x, y):
return Coord(geox, geoy)


# Mappings between numpy datatypes and GDAL datatypes.
# Note that ambiguities are resolved by the order - the first one found
# is the one chosen.
dataTypeMapping = [
(numpy.uint8, gdalconst.GDT_Byte),
(bool, gdalconst.GDT_Byte),
(numpy.int16, gdalconst.GDT_Int16),
(numpy.uint16, gdalconst.GDT_UInt16),
(numpy.int32, gdalconst.GDT_Int32),
(numpy.uint32, gdalconst.GDT_UInt32),
(numpy.float32, gdalconst.GDT_Float32),
(numpy.float64, gdalconst.GDT_Float64)
]

# hack for GDAL 3.5 and later which suppport 64 bit ints
if hasattr(gdalconst, 'GDT_Int64'):
dataTypeMapping.append((numpy.int64, gdalconst.GDT_Int64))
dataTypeMapping.append((numpy.uint64, gdalconst.GDT_UInt64))

# With GDAL 3.7, there is a plan to introduce GDT_Int8, so try to cope with it.
# Hopefully OK, because we did not previously have anything to cope with arrays
# of type numpy.int8.
if hasattr(gdalconst, 'GDT_Int8'):
dataTypeMapping.append((numpy.int8, gdalconst.GDT_Int8))


def GDALTypeToNumpyType(gdaltype):
"""
Given a gdal data type returns the matching
numpy data type
"""
for (numpy_type, test_gdal_type) in dataTypeMapping:
if test_gdal_type == gdaltype:
return numpy_type
raise rioserrors.TypeConversionError("Unknown GDAL datatype: %s"%gdaltype)
warnings.warn("Future versions of RIOS may remove this function. " +
"Use gdal_array.GDALTypeCodeToNumericTypeCode instead",
DeprecationWarning, stacklevel=2)
return gdal_array.GDALTypeCodeToNumericTypeCode(gdaltype)


def NumpyTypeToGDALType(numpytype):
"""
For a given numpy data type returns the matching
GDAL data type
"""
for (test_numpy_type, gdaltype) in dataTypeMapping:
if test_numpy_type == numpytype:
return gdaltype
raise rioserrors.TypeConversionError("Unknown numpy datatype: %s"%numpytype)
warnings.warn("Future versions of RIOS may remove this function. " +
"Use gdal_array.NumericTypeCodeToGDALTypeCode instead",
DeprecationWarning, stacklevel=2)
return gdal_array.NumericTypeCodeToGDALTypeCode(numpytype)
4 changes: 2 additions & 2 deletions rios/imagewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import math

from osgeo import gdal
from osgeo import gdal_array

from . import imageio
from . import rioserrors
from . import rat
from . import calcstats
Expand Down Expand Up @@ -221,7 +221,7 @@ def __init__(self, filename, drivername=DEFAULTDRIVERNAME, creationoptions=None,
# get the number of bands out of the block
(nbands, y, x) = firstblock.shape
# and the datatype
gdaldatatype = imageio.NumpyTypeToGDALType(firstblock.dtype)
gdaldatatype = gdal_array.NumericTypeCodeToGDALTypeCode(firstblock.dtype)

if creationoptions is None:
if drivername in dfltDriverOptions:
Expand Down
4 changes: 2 additions & 2 deletions rios/inputcollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import sys
import tempfile

from . import imageio
from . import rioserrors
from . import pixelgrid
from osgeo import gdal
from osgeo import gdal_array


class InputIterator(object):
Expand Down Expand Up @@ -134,7 +134,7 @@ def __init__(self, imageList, loggingstream=sys.stdout):

# get the datatype of band 1
gdaldatatype = ds.GetRasterBand(1).DataType
numpytype = imageio.GDALTypeToNumpyType(gdaldatatype)
numpytype = gdal_array.GDALTypeCodeToNumericTypeCode(gdaldatatype)

# store the values in our lists
self.imageList.append(image)
Expand Down
3 changes: 2 additions & 1 deletion rios/readerinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import math

import numpy
from osgeo import gdal_array

from . import imageio

Expand Down Expand Up @@ -322,7 +323,7 @@ def getNoDataValueFor(self, block, band=1):
# if there is a valid novalue, cast it to the type
# of the dataset. Note this creates a numpy 0-d array
if novalue is not None:
numpytype = imageio.GDALTypeToNumpyType(band.DataType)
numpytype = gdal_array.GDALTypeCodeToNumericTypeCode(band.DataType)
novalue = numpy.cast[numpytype](novalue)

return novalue
Expand Down
4 changes: 2 additions & 2 deletions rios/vectorreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class that perform on-the-fly rasterization of
from . import rioserrors
from . import cuiprogress
from .imagereader import ImageReader
from .imageio import NumpyTypeToGDALType
from osgeo import ogr
from osgeo import gdal
from osgeo import osr
from osgeo import gdal_array
import numpy

DEFAULTBURNVALUE = 1
Expand Down Expand Up @@ -259,7 +259,7 @@ def rasterizeSingle(info, vector, progress):
# Haven't yet rasterized, so do this for the whole workingGrid
(nrows, ncols) = info.workingGrid.getDimensions()
numLayers = 1
gdaldatatype = NumpyTypeToGDALType(vector.datatype)
gdaldatatype = gdal_array.NumericTypeCodeToGDALTypeCode(vector.datatype)
outds = vector.driver.Create(vector.temp_image, ncols, nrows, numLayers,
gdaldatatype, vector.driveroptions)
if outds is None:
Expand Down

0 comments on commit 9cf63c1

Please sign in to comment.