| 1 |
""" |
| 2 |
Error checking functions for GEOS ctypes prototype functions. |
| 3 |
""" |
| 4 |
|
| 5 |
from ctypes import c_void_p, string_at |
| 6 |
|
| 7 |
from django.contrib.gis.geos.error import GEOSException |
| 8 |
from django.contrib.gis.geos.libgeos import GEOSFuncFactory |
| 9 |
|
| 10 |
# Getting the `free` routine used to free the memory allocated for |
| 11 |
# string pointers returned by GEOS. |
| 12 |
free = GEOSFuncFactory("GEOSFree") |
| 13 |
free.argtypes = [c_void_p] |
| 14 |
|
| 15 |
|
| 16 |
def last_arg_byref(args): |
| 17 |
"Return the last C argument's value by reference." |
| 18 |
return args[-1]._obj.value |
| 19 |
|
| 20 |
|
| 21 |
def check_dbl(result, func, cargs): |
| 22 |
"Check the status code and returns the double value passed in by reference." |
| 23 |
# Checking the status code |
| 24 |
if result != 1: |
| 25 |
return None |
| 26 |
# Double passed in by reference, return its value. |
| 27 |
return last_arg_byref(cargs) |
| 28 |
|
| 29 |
|
| 30 |
def check_geom(result, func, cargs): |
| 31 |
"Error checking on routines that return Geometries." |
| 32 |
if not result: |
| 33 |
raise GEOSException( |
| 34 |
'Error encountered checking Geometry returned from GEOS C function "%s".' |
| 35 |
% func.__name__ |
| 36 |
) |
| 37 |
return result |
| 38 |
|
| 39 |
|
| 40 |
def check_minus_one(result, func, cargs): |
| 41 |
"Error checking on routines that should not return -1." |
| 42 |
if result == -1: |
| 43 |
raise GEOSException( |
| 44 |
'Error encountered in GEOS C function "%s".' % func.__name__ |
| 45 |
) |
| 46 |
else: |
| 47 |
return result |
| 48 |
|
| 49 |
|
| 50 |
def check_predicate(result, func, cargs): |
| 51 |
"Error checking for unary/binary predicate functions." |
| 52 |
if result == 1: |
| 53 |
return True |
| 54 |
elif result == 0: |
| 55 |
return False |
| 56 |
else: |
| 57 |
raise GEOSException( |
| 58 |
'Error encountered on GEOS C predicate function "%s".' % func.__name__ |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
def check_sized_string(result, func, cargs): |
| 63 |
""" |
| 64 |
Error checking for routines that return explicitly sized strings. |
| 65 |
|
| 66 |
This frees the memory allocated by GEOS at the result pointer. |
| 67 |
""" |
| 68 |
if not result: |
| 69 |
raise GEOSException( |
| 70 |
'Invalid string pointer returned by GEOS C function "%s"' % func.__name__ |
| 71 |
) |
| 72 |
# A c_size_t object is passed in by reference for the second |
| 73 |
# argument on these routines, and its needed to determine the |
| 74 |
# correct size. |
| 75 |
s = string_at(result, last_arg_byref(cargs)) |
| 76 |
# Freeing the memory allocated within GEOS |
| 77 |
free(result) |
| 78 |
return s |
| 79 |
|
| 80 |
|
| 81 |
def check_string(result, func, cargs): |
| 82 |
""" |
| 83 |
Error checking for routines that return strings. |
| 84 |
|
| 85 |
This frees the memory allocated by GEOS at the result pointer. |
| 86 |
""" |
| 87 |
if not result: |
| 88 |
raise GEOSException( |
| 89 |
'Error encountered checking string return value in GEOS C function "%s".' |
| 90 |
% func.__name__ |
| 91 |
) |
| 92 |
# Getting the string value at the pointer address. |
| 93 |
s = string_at(result) |
| 94 |
# Freeing the memory allocated within GEOS |
| 95 |
free(result) |
| 96 |
return s |