Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#5223 closed enhancement (fixed)

gdal_array.NumericTypeCodeToGDALTypeCode does not accept numpy dtype arguments (patch)

Reported by: lpinner Owned by: hobu
Priority: normal Milestone: 1.11.0
Component: PythonBindings Version: svn-trunk
Severity: normal Keywords:
Cc: Even Rouault

Description (last modified by lpinner)

Patch to enable checks against numpy dtypes as well as types attached.

Why...? Not all numpy types are created equal! Consider this (on Windows, works as expected on Linux):

import numpy as np, numexpr as ne
from osgeo import gdal_array

a = np.array([1], np.int32)
b = ne.evaluate('a')

print 'dtypes:',a.dtype,b.dtype
print 'types:',a.dtype.type,b.dtype.type

print 'Compare dtypes (expected output True):', a.dtype==b.dtype
print 'Compare types (expected output True):', a.dtype.type==b.dtype.type

print 'Compare a.dtype.type to np.int32 (expected output True):', a.dtype==np.int32
print 'Compare b.dtype.type to np.int32 (expected output True):', b.dtype.type==np.int32
print 'b.dtype.type is actually a np.intc:', b.dtype.type==np.intc

print 'NumericTypeCodeToGDALTypeCode(a.dtype.type):',gdal_array.NumericTypeCodeToGDALTypeCode(a.dtype.type)
print 'NumericTypeCodeToGDALTypeCode(b.dtype.type):',gdal_array.NumericTypeCodeToGDALTypeCode(b.dtype.type)

This outputs:

dtypes: int32 int32
types: <type 'numpy.int32'> <type 'numpy.int32'>
Compare dtypes (expected output True): True
Compare types (expected output True): False
Compare b.dtype.type to np.int32 (expected output True): True
Compare b.dtype.type to np.int32 (expected output True): False
b.dtype.type is actually a np.intc: True
NumericTypeCodeToGDALTypeCode(a.dtype.type): 5
NumericTypeCodeToGDALTypeCode(b.dtype.type): None

With the patch applied, I can pass b.dtype:

print gdal_array.NumericTypeCodeToGDALTypeCode(b.dtype)
5

Attachments (1)

gdal_array.i.patch (773 bytes) - added by lpinner 3 years ago.
gdal_array.i.patch (v. 2)

Download all attachments as: .zip

Change History (7)

comment:1 Changed 3 years ago by lpinner

Description: modified (diff)

comment:2 Changed 3 years ago by lpinner

Version: unspecifiedsvn-trunk

comment:3 Changed 3 years ago by lpinner

Description: modified (diff)

Changed 3 years ago by lpinner

Attachment: gdal_array.i.patch added

gdal_array.i.patch (v. 2)

comment:4 Changed 3 years ago by lpinner

Cc: Even Rouault added

comment:5 Changed 3 years ago by Even Rouault

Milestone: 2.0
Resolution: fixed
Status: newclosed

Patch applied in r26422. Thanks

comment:6 Changed 3 years ago by Even Rouault

Milestone: 2.01.11.0
Note: See TracTickets for help on using tickets.