Opened 9 years ago

Closed 9 years ago

Last modified 8 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)

Attachments (1)

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

Download all attachments as: .zip

Change History (7)

comment:1 by lpinner, 9 years ago

Description: modified (diff)

comment:2 by lpinner, 9 years ago

Version: unspecifiedsvn-trunk

comment:3 by lpinner, 9 years ago

Description: modified (diff)

by lpinner, 9 years ago

Attachment: gdal_array.i.patch added

gdal_array.i.patch (v. 2)

comment:4 by lpinner, 9 years ago

Cc: Even Rouault added

comment:5 by Even Rouault, 9 years ago

Milestone: 2.0
Resolution: fixed
Status: newclosed

Patch applied in r26422. Thanks

comment:6 by Even Rouault, 8 years ago

Note: See TracTickets for help on using tickets.