Philip Riggs wrote:

>I am not finding an answer to this question in the latest numpy  
>documentation. I have a package that still uses Numeric (GDAL with  
>python bindings). Is this valid code that will work as expected to  
>convert the Numeric array to a numpy array (very simplified from my  
>script)?
>
>import numpy
>from Numeric import *
>import gdal
>import gdalconst
>import gdalnumeric
>
>tempmap = array ([[1,0,1,0],[1,1,0,1]])
>map = numpy.array(tempmap)
>  
>
Yes, this will do the conversion.

>I considered converting the old bindings to use numpy arrays, but  
>receive an error. I believe the problem lies in the datatype  
>conversion shown below. Is there an easy way to fix this?
>
>
># GDALDataType
>GDT_Unknown = 0
>GDT_Byte = 1
>GDT_UInt16 = 2
>GDT_Int16 = 3
>GDT_UInt32 = 4
>GDT_Int32 = 5
>GDT_Float32 = 6
>GDT_Float64 = 7
>GDT_CInt16 = 8
>GDT_CInt32 = 9
>GDT_CFloat32 = 10
>GDT_CFloat64 = 11
>GDT_TypeCount = 12
>
>def GDALTypeCodeToNumericTypeCode( gdal_code ):
>     if gdal_code == GDT_Byte:
>         return UnsignedInt8
>     elif gdal_code == GDT_UInt16:
>         return UnsignedInt16
>     elif gdal_code == GDT_Int16:
>         return Int16
>     elif gdal_code == GDT_UInt32:
>         return UnsignedInt32
>     elif gdal_code == GDT_Int32:
>         return Int32
>     elif gdal_code == GDT_Float32:
>         return Float32
>     elif gdal_code == GDT_Float64:
>         return Float64
>     elif gdal_code == GDT_CInt16:
>         return Complex32
>     elif gdal_code == GDT_CInt32:
>         return Complex32
>     elif gdal_code == GDT_CFloat32:
>         return Complex32
>     elif gdal_code == GDT_CFloat64:
>         return Complex64
>     else:
>         return None
>
>def NumericTypeCodeToGDALTypeCode( numeric_code ):
>     if numeric_code == UnsignedInt8:
>         return GDT_Byte
>     elif numeric_code == Int16:
>         return GDT_Int16
>     elif numeric_code == UnsignedInt16:
>         return GDT_UInt16
>     elif numeric_code == Int32:
>         return GDT_Int32
>     elif numeric_code == UnsignedInt32:
>         return GDT_UInt32
>     elif numeric_code == Int:
>         return GDT_Int32
>     elif numeric_code == UnsignedInteger:
>         return GDT_UInt32
>     elif numeric_code == Float32:
>         return GDT_Float32
>     elif numeric_code == Float64:
>         return GDT_Float64
>     elif numeric_code == Complex32:
>         return GDT_CFloat32
>     elif numeric_code == Complex64:
>         return GDT_CFloat64
>     else:
>         return None
>  
>
All of these Int16, UnsignedInt16, etc. names are in

numpy.oldnumeric

However, they use the "new" character codes and so you have to take that 
into account.   You might also consider using a dictionary to do this 
conversion instead of the if ... elif tree.

-Travis

_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to