Opened 11 years ago

Last modified 10 years ago

#5223 closed enhancement

gdal_array.NumericTypeCodeToGDALTypeCode does not accept numpy dtype arguments (patch) — at Initial Version

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

Description

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:', a.dtype==b.dtype
print 'Compare dtype.types:', a.dtype.type==b.dtype.type
print 'Compare a.dtype to np.int32:', a.dtype==np.int32
print 'Compare b.dtype to np.int32:', b.dtype==np.int32

print 'NumericTypeCodeToGDALTypeCode(a.dtype.type):',repr(gdal_array.NumericTypeCodeToGDALTypeCode(a.dtype.type))
print 'NumericTypeCodeToGDALTypeCode(b.dtype.type):',repr(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

Change History (0)

Note: See TracTickets for help on using tickets.