PageRenderTime 52ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 21ms app.codeStats 0ms

/django/contrib/gis/gdal/prototypes/errcheck.py

https://code.google.com/p/mango-py/
Python | 127 lines | 71 code | 14 blank | 42 comment | 14 complexity | 64b0a2a98d8309ecfec9ecbbef2d4a50 MD5 | raw file
  1"""
  2 This module houses the error-checking routines used by the GDAL
  3 ctypes prototypes.
  4"""
  5from ctypes import c_void_p, string_at
  6from django.contrib.gis.gdal.error import check_err, OGRException, SRSException
  7from django.contrib.gis.gdal.libgdal import lgdal
  8
  9# Helper routines for retrieving pointers and/or values from 
 10# arguments passed in by reference. 
 11def arg_byref(args, offset=-1):
 12    "Returns the pointer argument's by-refernece value."
 13    return args[offset]._obj.value
 14
 15def ptr_byref(args, offset=-1):
 16    "Returns the pointer argument passed in by-reference."
 17    return args[offset]._obj
 18
 19def 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 ###
 25def 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
 36def 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 ###
 64def 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 ###
 70def 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, long)):
 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
 80def 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 ###
 87def check_srs(result, func, cargs):
 88    if isinstance(result, (int, long)):
 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 ###
 95def 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
103def check_errcode(result, func, cargs):
104    """
105    Check the error code returned (c_int).
106    """
107    check_err(result)
108    return
109
110def check_pointer(result, func, cargs):
111    "Makes sure the result pointer is valid."
112    if isinstance(result, (int, long)):
113        result = c_void_p(result)
114    if bool(result): 
115        return result
116    else: 
117        raise OGRException('Invalid pointer returned from "%s"' % func.__name__)
118
119def check_str_arg(result, func, cargs):
120    """
121    This is for the OSRGet[Angular|Linear]Units functions, which
122    require that the returned string pointer not be freed.  This
123    returns both the double and tring values.
124    """
125    dbl = result
126    ptr = cargs[-1]._obj
127    return dbl, ptr.value