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 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 return
elif code in OGRERR_DICT: elif code in err_dict:
e, msg = OGRERR_DICT[code] e, msg = err_dict[code]
raise e(msg) raise e(msg)
else: 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 ### ### 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. Similar functionality to `check_string`, but does not free the pointer.
""" """
if offset: if offset:
check_err(result) check_err(result, cpl=cpl)
ptr = ptr_byref(cargs, offset) ptr = ptr_byref(cargs, offset)
return ptr.value return ptr.value
else: else:
@ -101,20 +101,20 @@ def check_srs(result, func, cargs):
### Other error-checking routines ### ### 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. The error code is returned in the last argument, by reference.
Check its value with `check_err` before returning the result. Check its value with `check_err` before returning the result.
""" """
check_err(arg_byref(cargs)) check_err(arg_byref(cargs), cpl=cpl)
return result return result
def check_errcode(result, func, cargs): def check_errcode(result, func, cargs, cpl=False):
""" """
Check the error code returned (c_int). Check the error code returned (c_int).
""" """
check_err(result) check_err(result, cpl=cpl)
def check_pointer(result, func, cargs): def check_pointer(result, func, cargs):

View File

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

View File

@ -3,6 +3,8 @@ This module houses the ctypes function prototypes for GDAL DataSource (raster)
related data structures. related data structures.
""" """
from ctypes import c_char_p, c_double, c_int, c_void_p, POINTER 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.libgdal import lgdal
from django.contrib.gis.gdal.prototypes.generation import (const_string_output, from django.contrib.gis.gdal.prototypes.generation import (const_string_output,
double_output, int_output, void_output, voidptr_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/gdal_8h.html
# http://gdal.org/gdalwarper_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 ### ### Raster Driver Routines ###
register_all = void_output(lgdal.GDALAllRegister, []) register_all = void_output(lgdal.GDALAllRegister, [])
get_driver = voidptr_output(lgdal.GDALGetDriver, [c_int]) get_driver = voidptr_output(lgdal.GDALGetDriver, [c_int])