Skip to content

Instantly share code, notes, and snippets.

@chryss
Created January 11, 2014 03:09
Show Gist options
  • Save chryss/8366492 to your computer and use it in GitHub Desktop.
Save chryss/8366492 to your computer and use it in GitHub Desktop.
import json
import numpy as np
from osgeo import gdal, gdal_array
typemap = {}
typenames = {}
for name in dir(np):
obj = getattr(np, name)
if hasattr(obj, 'dtype'):
try:
npn = obj(0)
nat = np.asscalar(npn)
if gdal_array.NumericTypeCodeToGDALTypeCode(npn.dtype.type):
typemap[npn.dtype.name] = gdal_array.NumericTypeCodeToGDALTypeCode(npn.dtype.type)
typenames[npn.dtype.name] = gdal.GetDataTypeName(gdal_array.NumericTypeCodeToGDALTypeCode(npn.dtype.type))
except:
pass
print(json.dumps(typemap, indent=2))
print(json.dumps(typenames, indent=2))
# Output:
# {
# "int32": 5,
# "int16": 3,
# "float64": 7,
# "complex128": 11,
# "uint8": 1,
# "uint16": 2,
# "complex64": 10,
# "uint32": 4,
# "int8": 1,
# "float32": 6
# }
# {
# "int32": "Int32",
# "int16": "Int16",
# "float64": "Float64",
# "complex128": "CFloat64",
# "uint8": "Byte",
# "uint16": "UInt16",
# "complex64": "CFloat32",
# "uint32": "UInt32",
# "int8": "Byte",
# "float32": "Float32"
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment