this module houses the errorchecking routines used by the gdal ctypes prototypes helper routines for retrieving pointers andor values from arguments passed in by reference string checking routines similar functionality to checkstring but does not free the pointer check the string output returned from the given function and free the string pointer allocated by ogr the strresult keyword may be used when the result is the string pointer otherwise the ogr error code is assumed the offset keyword may be used to extract the string pointer passed in byreference at the given slice offset in the function arguments for routines that return a string errorcode return specified getting the string value correctly freeing the allocated memory behind gdal pointer with the vsifree routine datasource layer errorchecking envelope checking geometry errorchecking routines ogrgclone may return an integer even though the restype is set to cvoidp spatial reference errorchecking routines other errorchecking routines the error code is returned in the last argument by reference check its value with checkerr before returning the result check the error code returned cint this is for the osrgetangularlinearunits functions which require that the returned string pointer not be freed this returns both the double and string values helper routines for retrieving pointers and or values from arguments passed in by reference string checking routines similar functionality to check_string but does not free the pointer check the string output returned from the given function and free the string pointer allocated by ogr the str_result keyword may be used when the result is the string pointer otherwise the ogr error code is assumed the offset keyword may be used to extract the string pointer passed in by reference at the given slice offset in the function arguments for routines that return a string error code return specified getting the string value correctly freeing the allocated memory behind gdal pointer with the vsifree routine datasource layer error checking envelope checking geometry error checking routines ogr_g_clone may return an integer even though the restype is set to c_void_p spatial reference error checking routines other error checking routines the error code is returned in the last argument by reference check its value with check_err before returning the result check the error code returned c_int this is for the osrget angular linear units functions which require that the returned string pointer not be freed this returns both the double and string values
from ctypes import c_void_p, string_at

from django.contrib.gis.gdal.error import GDALException, SRSException, check_err
from django.contrib.gis.gdal.libgdal import lgdal




def arg_byref(args, offset=-1):
    "Return the pointer argument's by-reference value."
    return args[offset]._obj.value


def ptr_byref(args, offset=-1):
    "Return the pointer argument passed in by-reference."
    return args[offset]._obj



def check_const_string(result, func, cargs, offset=None, cpl=False):
    
    if offset:
        check_err(result, cpl=cpl)
        ptr = ptr_byref(cargs, offset)
        return ptr.value
    else:
        return result


def check_string(result, func, cargs, offset=-1, str_result=False):
    
    if str_result:
        
        ptr = result
        if not ptr:
            s = None
        else:
            s = string_at(result)
    else:
        
        check_err(result)
        ptr = ptr_byref(cargs, offset)
        
        s = ptr.value
    
    
    if ptr:
        lgdal.VSIFree(ptr)
    return s






def check_envelope(result, func, cargs, offset=-1):
    "Check a function that returns an OGR Envelope by reference."
    return ptr_byref(cargs, offset)



def check_geom(result, func, cargs):
    "Check a function that returns a geometry."
    
    
    if isinstance(result, int):
        result = c_void_p(result)
    if not result:
        raise GDALException(
            'Invalid geometry pointer returned from "%s".' % func.__name__
        )
    return result


def check_geom_offset(result, func, cargs, offset=-1):
    "Check the geometry at the given offset in the C parameter list."
    check_err(result)
    geom = ptr_byref(cargs, offset=offset)
    return check_geom(geom, func, cargs)



def check_srs(result, func, cargs):
    if isinstance(result, int):
        result = c_void_p(result)
    if not result:
        raise SRSException(
            'Invalid spatial reference pointer returned from "%s".' % func.__name__
        )
    return result



def check_arg_errcode(result, func, cargs, cpl=False):
    
    check_err(arg_byref(cargs), cpl=cpl)
    return result


def check_errcode(result, func, cargs, cpl=False):
    
    check_err(result, cpl=cpl)


def check_pointer(result, func, cargs):
    "Make sure the result pointer is valid."
    if isinstance(result, int):
        result = c_void_p(result)
    if result:
        return result
    else:
        raise GDALException('Invalid pointer returned from "%s"' % func.__name__)


def check_str_arg(result, func, cargs):
    
    dbl = result
    ptr = cargs[-1]._obj
    return dbl, ptr.value.decode()