this module contains functions that generate ctypes prototypes for the gdal routines generate a ctypes function that returns a boolean value func argtypes argtypes func restype cbool if errcheck func errcheck errcheck return func def doubleoutputfunc argtypes errcheckfalse strargfalse cplfalse generate a ctypes function that returns a double value func argtypes argtypes func restype cdouble if errcheck func errcheck partialcheckargerrcode cplcpl if strarg func errcheck checkstrarg return func def geomoutputfunc argtypes offsetnone setting the argument types func argtypes argtypes if not offset when a geometry pointer is directly returned func restype cvoidp func errcheck checkgeom else error code returned geometry is returned byreference func restype cint def geomerrcheckresult func cargs return checkgeomoffsetresult func cargs offset func errcheck geomerrcheck return func def intoutputfunc argtypes errchecknone generate a ctypes function that returns an integer value func argtypes argtypes func restype cint if errcheck func errcheck errcheck return func def int64outputfunc argtypes generate a ctypes function that returns a 64bit integer value func argtypes argtypes func restype cint64 return func def srsoutputfunc argtypes func argtypes argtypes func restype cvoidp func errcheck checksrs return func def conststringoutputfunc argtypes offsetnone decodingnone cplfalse func argtypes argtypes if offset func restype cint else func restype ccharp def checkconstresult func cargs res checkconststringresult func cargs offsetoffset cplcpl if res and decoding res res decodedecoding return res func errcheck checkconst return func def stringoutputfunc argtypes offset1 strresultfalse decodingnone func argtypes argtypes if strresult use subclass of ccharp so the error checking routine can free the memory at the pointer s address func restype gdalcharp else error code is returned func restype cint dynamically defining our errorchecking function with the given offset def checkstrresult func cargs res checkstringresult func cargs offsetoffset strresultstrresult if res and decoding res res decodedecoding return res func errcheck checkstr return func def voidoutputfunc argtypes errchecktrue cplfalse if argtypes func argtypes argtypes if errcheck errcheck keyword may be set to false for routines that return void rather than a status code func restype cint func errcheck partialcheckerrcode cplcpl else func restype none return func def voidptroutputfunc argtypes errchecktrue for functions that return cvoidp func argtypes argtypes func restype cvoidp if errcheck func errcheck checkpointer return func def chararrayoutputfunc argtypes errchecktrue generate a ctypes function that returns a boolean value generate a function that returns a geometry either by reference or directly if the return_geom keyword is set to true setting the argument types when a geometry pointer is directly returned error code returned geometry is returned by reference generate a ctypes prototype for the given function with the given c arguments that returns a pointer to an ogr spatial reference system generate a ctypes prototype for the given function with the given argument types that returns a string from a gdal pointer the const flag indicates whether the allocated pointer should be freed via the gdal library routine vsifree but only applies only when str_result is true use subclass of c_char_p so the error checking routine can free the memory at the pointer s address error code is returned dynamically defining our error checking function with the given offset for functions that don t only return an error code that needs to be examined errcheck keyword may be set to false for routines that return void rather than a status code for functions that return a c_char_p array
from ctypes import POINTER, c_bool, c_char_p, c_double, c_int, c_int64, c_void_p
from functools import partial

from django.contrib.gis.gdal.prototypes.errcheck import (
    check_arg_errcode,
    check_const_string,
    check_errcode,
    check_geom,
    check_geom_offset,
    check_pointer,
    check_srs,
    check_str_arg,
    check_string,
)


class gdal_char_p(c_char_p):
    pass


def bool_output(func, argtypes, errcheck=None):
    
    func.argtypes = argtypes
    func.restype = c_bool
    if errcheck:
        func.errcheck = errcheck
    return func


def double_output(func, argtypes, errcheck=False, strarg=False, cpl=False):
    "Generate a ctypes function that returns a double value."
    func.argtypes = argtypes
    func.restype = c_double
    if errcheck:
        func.errcheck = partial(check_arg_errcode, cpl=cpl)
    if strarg:
        func.errcheck = check_str_arg
    return func


def geom_output(func, argtypes, offset=None):
    
    
    func.argtypes = argtypes

    if not offset:
        
        func.restype = c_void_p
        func.errcheck = check_geom
    else:
        
        func.restype = c_int

        def geomerrcheck(result, func, cargs):
            return check_geom_offset(result, func, cargs, offset)

        func.errcheck = geomerrcheck

    return func


def int_output(func, argtypes, errcheck=None):
    "Generate a ctypes function that returns an integer value."
    func.argtypes = argtypes
    func.restype = c_int
    if errcheck:
        func.errcheck = errcheck
    return func


def int64_output(func, argtypes):
    "Generate a ctypes function that returns a 64-bit integer value."
    func.argtypes = argtypes
    func.restype = c_int64
    return func


def srs_output(func, argtypes):
    
    func.argtypes = argtypes
    func.restype = c_void_p
    func.errcheck = check_srs
    return func


def const_string_output(func, argtypes, offset=None, decoding=None, cpl=False):
    func.argtypes = argtypes
    if offset:
        func.restype = c_int
    else:
        func.restype = c_char_p

    def _check_const(result, func, cargs):
        res = check_const_string(result, func, cargs, offset=offset, cpl=cpl)
        if res and decoding:
            res = res.decode(decoding)
        return res

    func.errcheck = _check_const

    return func


def string_output(func, argtypes, offset=-1, str_result=False, decoding=None):
    
    func.argtypes = argtypes
    if str_result:
        
        
        func.restype = gdal_char_p
    else:
        
        func.restype = c_int

    
    
    def _check_str(result, func, cargs):
        res = check_string(result, func, cargs, offset=offset, str_result=str_result)
        if res and decoding:
            res = res.decode(decoding)
        return res

    func.errcheck = _check_str
    return func


def void_output(func, argtypes, errcheck=True, cpl=False):
    
    if argtypes:
        func.argtypes = argtypes
    if errcheck:
        
        
        func.restype = c_int
        func.errcheck = partial(check_errcode, cpl=cpl)
    else:
        func.restype = None

    return func


def voidptr_output(func, argtypes, errcheck=True):
    "For functions that return c_void_p."
    func.argtypes = argtypes
    func.restype = c_void_p
    if errcheck:
        func.errcheck = check_pointer
    return func


def chararray_output(func, argtypes, errcheck=True):
    
    func.argtypes = argtypes
    func.restype = POINTER(c_char_p)
    if errcheck:
        func.errcheck = check_pointer
    return func