app/django/contrib/gis/gdal/prototypes/generation.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 """
       
     2  This module contains functions that generate ctypes prototypes for the
       
     3  GDAL routines.
       
     4 """
       
     5 
       
     6 from ctypes import c_char_p, c_double, c_int, c_void_p
       
     7 from django.contrib.gis.gdal.prototypes.errcheck import \
       
     8     check_arg_errcode, check_errcode, check_geom, check_geom_offset, \
       
     9     check_pointer, check_srs, check_str_arg, check_string, check_const_string
       
    10 
       
    11 def double_output(func, argtypes, errcheck=False, strarg=False):
       
    12     "Generates a ctypes function that returns a double value."
       
    13     func.argtypes = argtypes
       
    14     func.restype = c_double
       
    15     if errcheck: func.errcheck = check_arg_errcode
       
    16     if strarg: func.errcheck = check_str_arg
       
    17     return func
       
    18 
       
    19 def geom_output(func, argtypes, offset=None):
       
    20     """
       
    21     Generates a function that returns a Geometry either by reference
       
    22     or directly (if the return_geom keyword is set to True).
       
    23     """
       
    24     # Setting the argument types
       
    25     func.argtypes = argtypes
       
    26 
       
    27     if not offset:
       
    28         # When a geometry pointer is directly returned.
       
    29         func.restype = c_void_p
       
    30         func.errcheck = check_geom
       
    31     else:
       
    32         # Error code returned, geometry is returned by-reference.
       
    33         func.restype = c_int
       
    34         def geomerrcheck(result, func, cargs):
       
    35             return check_geom_offset(result, func, cargs, offset)
       
    36         func.errcheck = geomerrcheck
       
    37 
       
    38     return func
       
    39 
       
    40 def int_output(func, argtypes):
       
    41     "Generates a ctypes function that returns an integer value."
       
    42     func.argtypes = argtypes
       
    43     func.restype = c_int
       
    44     return func
       
    45 
       
    46 def srs_output(func, argtypes):
       
    47     """
       
    48     Generates a ctypes prototype for the given function with
       
    49     the given C arguments that returns a pointer to an OGR
       
    50     Spatial Reference System.
       
    51     """
       
    52     func.argtypes = argtypes
       
    53     func.restype = c_void_p
       
    54     func.errcheck = check_srs
       
    55     return func
       
    56 
       
    57 def const_string_output(func, argtypes, offset=None):
       
    58     func.argtypes = argtypes
       
    59     if offset:
       
    60         func.restype = c_int
       
    61     else:
       
    62         func.restype = c_char_p
       
    63 
       
    64     def _check_const(result, func, cargs):
       
    65         return check_const_string(result, func, cargs, offset=offset)
       
    66     func.errcheck = _check_const
       
    67 
       
    68     return func
       
    69 
       
    70 def string_output(func, argtypes, offset=-1, str_result=False):
       
    71     """
       
    72     Generates a ctypes prototype for the given function with the
       
    73     given argument types that returns a string from a GDAL pointer.
       
    74     The `const` flag indicates whether the allocated pointer should 
       
    75     be freed via the GDAL library routine VSIFree -- but only applies
       
    76     only when `str_result` is True.
       
    77     """
       
    78     func.argtypes = argtypes
       
    79     if str_result:
       
    80         # String is the result, don't explicitly define
       
    81         # the argument type so we can get the pointer.
       
    82         pass
       
    83     else:
       
    84         # Error code is returned
       
    85         func.restype = c_int
       
    86 
       
    87     # Dynamically defining our error-checking function with the
       
    88     # given offset.
       
    89     def _check_str(result, func, cargs):
       
    90         return check_string(result, func, cargs,
       
    91                             offset=offset, str_result=str_result)
       
    92     func.errcheck = _check_str
       
    93     return func
       
    94 
       
    95 def void_output(func, argtypes, errcheck=True):
       
    96     """
       
    97     For functions that don't only return an error code that needs to
       
    98     be examined.
       
    99     """
       
   100     if argtypes: func.argtypes = argtypes
       
   101     if errcheck:
       
   102         # `errcheck` keyword may be set to False for routines that
       
   103         # return void, rather than a status code.
       
   104         func.restype = c_int
       
   105         func.errcheck = check_errcode
       
   106     else:
       
   107         func.restype = None
       
   108         
       
   109     return func
       
   110 
       
   111 def voidptr_output(func, argtypes):
       
   112     "For functions that return c_void_p."
       
   113     func.argtypes = argtypes
       
   114     func.restype = c_void_p
       
   115     func.errcheck = check_pointer
       
   116     return func