Added cpl error codes to errcheck functions

GDAL raster methods return different error codes from the vector
API.
This commit is contained in:
Daniel Wiesmann 2014-12-01 17:34:50 +00:00 committed by Claude Paroz
parent 4df3a3e0e9
commit ac51496ceb
4 changed files with 46 additions and 19 deletions

View File

@ -42,14 +42,34 @@ OGRERR_DICT = {
}
OGRERR_NONE = 0
# CPL Error Codes
# http://www.gdal.org/cpl__error_8h.html
CPLERR_DICT = {
1: (GDALException, 'AppDefined'),
2: (GDALException, 'OutOfMemory'),
3: (GDALException, 'FileIO'),
4: (GDALException, 'OpenFailed'),
5: (GDALException, 'IllegalArg'),
6: (GDALException, 'NotSupported'),
7: (GDALException, 'AssertionFailed'),
8: (GDALException, 'NoWriteAccess'),
9: (GDALException, 'UserInterrupt'),
10: (GDALException, 'ObjectNull'),
}
CPLERR_NONE = 0
def check_err(code):
"Checks the given OGRERR, and raises an exception where appropriate."
if code == OGRERR_NONE:
def check_err(code, cpl=False):
"""
Checks the given CPL/OGRERR, and raises an exception where appropriate.
"""
err_none = CPLERR_NONE if cpl else OGRERR_NONE
err_dict = CPLERR_DICT if cpl else OGRERR_DICT
if code == err_none:
return
elif code in OGRERR_DICT:
e, msg = OGRERR_DICT[code]
elif code in err_dict:
e, msg = err_dict[code]
raise e(msg)
else:
raise OGRException('Unknown error code: "%s"' % code)
raise GDALException('Unknown error code: "%s"' % code)

View File

@ -22,12 +22,12 @@ def ptr_byref(args, offset=-1):
### String checking Routines ###
def check_const_string(result, func, cargs, offset=None):
def check_const_string(result, func, cargs, offset=None, cpl=False):
"""
Similar functionality to `check_string`, but does not free the pointer.
"""
if offset:
check_err(result)
check_err(result, cpl=cpl)
ptr = ptr_byref(cargs, offset)
return ptr.value
else:
@ -101,20 +101,20 @@ def check_srs(result, func, cargs):
### Other error-checking routines ###
def check_arg_errcode(result, func, cargs):
def check_arg_errcode(result, func, cargs, cpl=False):
"""
The error code is returned in the last argument, by reference.
Check its value with `check_err` before returning the result.
"""
check_err(arg_byref(cargs))
check_err(arg_byref(cargs), cpl=cpl)
return result
def check_errcode(result, func, cargs):
def check_errcode(result, func, cargs, cpl=False):
"""
Check the error code returned (c_int).
"""
check_err(result)
check_err(result, cpl=cpl)
def check_pointer(result, func, cargs):

View File

@ -2,8 +2,8 @@
This module contains functions that generate ctypes prototypes for the
GDAL routines.
"""
from ctypes import c_char_p, c_double, c_int, c_void_p
from functools import partial
from django.contrib.gis.gdal.prototypes.errcheck import (
check_arg_errcode, check_errcode, check_geom, check_geom_offset,
check_pointer, check_srs, check_str_arg, check_string, check_const_string)
@ -13,12 +13,12 @@ class gdal_char_p(c_char_p):
pass
def double_output(func, argtypes, errcheck=False, strarg=False):
def double_output(func, argtypes, errcheck=False, strarg=False, cpl=False):
"Generates a ctypes function that returns a double value."
func.argtypes = argtypes
func.restype = c_double
if errcheck:
func.errcheck = check_arg_errcode
func.errcheck = partial(check_arg_errcode, cpl=cpl)
if strarg:
func.errcheck = check_str_arg
return func
@ -66,7 +66,7 @@ def srs_output(func, argtypes):
return func
def const_string_output(func, argtypes, offset=None, decoding=None):
def const_string_output(func, argtypes, offset=None, decoding=None, cpl=False):
func.argtypes = argtypes
if offset:
func.restype = c_int
@ -74,7 +74,7 @@ def const_string_output(func, argtypes, offset=None, decoding=None):
func.restype = c_char_p
def _check_const(result, func, cargs):
res = check_const_string(result, func, cargs, offset=offset)
res = check_const_string(result, func, cargs, offset=offset, cpl=cpl)
if res and decoding:
res = res.decode(decoding)
return res
@ -112,7 +112,7 @@ def string_output(func, argtypes, offset=-1, str_result=False, decoding=None):
return func
def void_output(func, argtypes, errcheck=True):
def void_output(func, argtypes, errcheck=True, cpl=False):
"""
For functions that don't only return an error code that needs to
be examined.
@ -123,7 +123,7 @@ def void_output(func, argtypes, errcheck=True):
# `errcheck` keyword may be set to False for routines that
# return void, rather than a status code.
func.restype = c_int
func.errcheck = check_errcode
func.errcheck = partial(check_errcode, cpl=cpl)
else:
func.restype = None

View File

@ -3,6 +3,8 @@ This module houses the ctypes function prototypes for GDAL DataSource (raster)
related data structures.
"""
from ctypes import c_char_p, c_double, c_int, c_void_p, POINTER
from functools import partial
from django.contrib.gis.gdal.libgdal import lgdal
from django.contrib.gis.gdal.prototypes.generation import (const_string_output,
double_output, int_output, void_output, voidptr_output)
@ -11,6 +13,11 @@ from django.contrib.gis.gdal.prototypes.generation import (const_string_output,
# http://gdal.org/gdal_8h.html
# http://gdal.org/gdalwarper_8h.html
# Prepare partial functions that use cpl error codes
void_output = partial(void_output, cpl=True)
const_string_output = partial(const_string_output, cpl=True)
double_output = partial(double_output, cpl=True)
### Raster Driver Routines ###
register_all = void_output(lgdal.GDALAllRegister, [])
get_driver = voidptr_output(lgdal.GDALGetDriver, [c_int])