[gdal-dev] GDAL DataType class

Pinner, Luke Luke.Pinner at environment.gov.au
Wed Oct 28 19:56:32 EDT 2009


Ooops... I neglected the signed check. 

from osgeo import gdal

class GdalDataType(object):

    def __init__(self, nbits, Float, Signed=True, Complex=False):

        for i in range(0,gdal.GDT_TypeCount):
            self.DataType=None
            DataType=i
            DataTypeName=gdal.GetDataTypeName(i)
            DataTypeSize=gdal.GetDataTypeSize(i)
            DataTypeIsFloat ='Float' in DataTypeName
            DataTypeIsSigned = DataTypeIsFloat or DataTypeName[0:4] in
['Byte','UInt']
            DataTypeIsComplex=gdal.DataTypeIsComplex(i)==1

            if DataTypeSize==nbits and DataTypeIsFloat==Float and
DataTypeIsSigned==Signed and DataTypeIsComplex==Complex:
                self.DataType=DataType
                self.DataTypeName=DataTypeName
                self.DataTypeSize=DataTypeSize
                self.DataTypeIsFloat=DataTypeIsFloat
                self.DataTypeIsSigned=DataTypeIsSigned
                self.DataTypeIsComplex=DataTypeIsComplex
                if DataTypeIsSigned:
                    self.DataTypeRange=[-2**(nbits-1),2**(nbits-1)]
                else:
                    self.DataTypeRange=[0,2**nbits-1]
                break
        if self.DataType is None: raise TypeError, 'No matching DataType
found'

gdt=GdalDataType(nbits=32, Float=True, Complex=False)
print gdt.DataTypeName

-----Original Message-----
From: Pinner, Luke 
Sent: Thursday, 29 October 2009 10:21 AM
To: 'Luca Fasano'; gdal-dev at lists.osgeo.org
Subject: RE: [gdal-dev] GDAL DataType class

You could easily roll your own.

from osgeo import gdal

class GdalDataType(object):

    def __init__(self, nbits, Float, Complex=False, Signed=False):

        for i in range(0,gdal.GDT_TypeCount):
            self.DataType=None
            DataType=i
            DataTypeName=gdal.GetDataTypeName(i)
            DataTypeSize=gdal.GetDataTypeSize(i)
            DataTypeIsFloat ='Float' in DataTypeName
            DataTypeIsComplex=gdal.DataTypeIsComplex(i)==1

            if DataTypeSize==nbits and DataTypeIsFloat==Float and
DataTypeIsComplex==Complex:
                self.DataType=DataType
                self.DataTypeName=DataTypeName
                self.DataTypeSize=DataTypeSize
                self.DataTypeIsFloat=DataTypeIsFloat
                self.DataTypeIsComplex=DataTypeIsComplex
                break
        if self.DataType is None: raise TypeError, 'No matching DataType
found'

gdt=GdalDataType(nbits=32, Float=True, Complex=False)
print gdt.DataTypeName

Luke

-----Original Message-----
From: gdal-dev-bounces at lists.osgeo.org
[mailto:gdal-dev-bounces at lists.osgeo.org] On Behalf Of Luca Fasano
Sent: Thursday, 29 October 2009 3:19 AM
To: gdal-dev at lists.osgeo.org
Subject: [gdal-dev] GDAL DataType class

Hi all,
I need a way to retrieve a GDAL DataType (its code or its string name,
it's the same) starting from all information on it. Does it exist a
class or method to to this?
For example:
GdalDataType(nbits=32, float=True, Complex=True) -> gdal.GDT_CFloat32
I use Python bindings for GDAL.

I'd like to avoid building the string manually (i.e.
'GDT_'+'C'+'Float'+'32')
Thanks 

_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


------
If you have received this transmission in error please notify us immediately by return e-mail and delete all copies. If this e-mail or any attachments have been sent to you in error, that error does not constitute waiver of any confidentiality, privilege or copyright in respect of information in the e-mail or attachments. 



Please consider the environment before printing this email.

------



More information about the gdal-dev mailing list