app/django/contrib/gis/gdal/prototypes/errcheck.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 """
       
     2  This module houses the error-checking routines used by the GDAL
       
     3  ctypes prototypes.
       
     4 """
       
     5 from ctypes import c_void_p, string_at
       
     6 from django.contrib.gis.gdal.error import check_err, OGRException, SRSException
       
     7 from django.contrib.gis.gdal.libgdal import lgdal
       
     8 
       
     9 # Helper routines for retrieving pointers and/or values from 
       
    10 # arguments passed in by reference. 
       
    11 def arg_byref(args, offset=-1):
       
    12     "Returns the pointer argument's by-refernece value."
       
    13     return args[offset]._obj.value
       
    14 
       
    15 def ptr_byref(args, offset=-1):
       
    16     "Returns the pointer argument passed in by-reference."
       
    17     return args[offset]._obj
       
    18 
       
    19 def check_bool(result, func, cargs):
       
    20     "Returns the boolean evaluation of the value."
       
    21     if bool(result): return True
       
    22     else: return False
       
    23 
       
    24 ### String checking Routines ###
       
    25 def check_const_string(result, func, cargs, offset=None):
       
    26     """
       
    27     Similar functionality to `check_string`, but does not free the pointer.
       
    28     """
       
    29     if offset:
       
    30         check_err(result)
       
    31         ptr = ptr_byref(cargs, offset)
       
    32         return ptr.value
       
    33     else:
       
    34         return result
       
    35 
       
    36 def check_string(result, func, cargs, offset=-1, str_result=False):
       
    37     """
       
    38     Checks the string output returned from the given function, and frees
       
    39     the string pointer allocated by OGR.  The `str_result` keyword
       
    40     may be used when the result is the string pointer, otherwise
       
    41     the OGR error code is assumed.  The `offset` keyword may be used
       
    42     to extract the string pointer passed in by-reference at the given
       
    43     slice offset in the function arguments.
       
    44     """
       
    45     if str_result:
       
    46         # For routines that return a string.
       
    47         ptr = result
       
    48         if not ptr: s = None
       
    49         else: s = string_at(result)
       
    50     else:
       
    51         # Error-code return specified.
       
    52         check_err(result)
       
    53         ptr = ptr_byref(cargs, offset)
       
    54         # Getting the string value
       
    55         s = ptr.value
       
    56     # Correctly freeing the allocated memory beind GDAL pointer 
       
    57     # w/the VSIFree routine.
       
    58     if ptr: lgdal.VSIFree(ptr)
       
    59     return s
       
    60 
       
    61 ### DataSource, Layer error-checking ###
       
    62 
       
    63 ### Envelope checking ###
       
    64 def check_envelope(result, func, cargs, offset=-1):
       
    65     "Checks a function that returns an OGR Envelope by reference."
       
    66     env = ptr_byref(cargs, offset)
       
    67     return env
       
    68 
       
    69 ### Geometry error-checking routines ###
       
    70 def check_geom(result, func, cargs):
       
    71     "Checks a function that returns a geometry."
       
    72     # OGR_G_Clone may return an integer, even though the
       
    73     # restype is set to c_void_p
       
    74     if isinstance(result, int):
       
    75         result = c_void_p(result)
       
    76     if not result: 
       
    77         raise OGRException('Invalid geometry pointer returned from "%s".' % func.__name__)
       
    78     return result
       
    79 
       
    80 def check_geom_offset(result, func, cargs, offset=-1):
       
    81     "Chcks the geometry at the given offset in the C parameter list."
       
    82     check_err(result)
       
    83     geom = ptr_byref(cargs, offset=offset)
       
    84     return check_geom(geom, func, cargs)
       
    85 
       
    86 ### Spatial Reference error-checking routines ###
       
    87 def check_srs(result, func, cargs):
       
    88     if isinstance(result, int):
       
    89         result = c_void_p(result)
       
    90     if not result:
       
    91         raise SRSException('Invalid spatial reference pointer returned from "%s".' % func.__name__)
       
    92     return result
       
    93 
       
    94 ### Other error-checking routines ###
       
    95 def check_arg_errcode(result, func, cargs):
       
    96     """
       
    97     The error code is returned in the last argument, by reference.
       
    98     Check its value with `check_err` before returning the result.
       
    99     """
       
   100     check_err(arg_byref(cargs))
       
   101     return result
       
   102 
       
   103 def check_errcode(result, func, cargs):
       
   104     """
       
   105     Check the error code returned (c_int).
       
   106     """
       
   107     check_err(result)
       
   108     return
       
   109 
       
   110 def check_pointer(result, func, cargs):
       
   111     "Makes sure the result pointer is valid."
       
   112     if bool(result): 
       
   113         return result
       
   114     else: 
       
   115         raise OGRException('Invalid pointer returned from "%s"' % func.__name__)
       
   116 
       
   117 def check_str_arg(result, func, cargs):
       
   118     """
       
   119     This is for the OSRGet[Angular|Linear]Units functions, which
       
   120     require that the returned string pointer not be freed.  This
       
   121     returns both the double and tring values.
       
   122     """
       
   123     dbl = result
       
   124     ptr = cargs[-1]._obj
       
   125     return dbl, ptr.value