Debian Codegraph @

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

raw
1 """
2 This module contains functions that generate ctypes prototypes for the
3 GDAL routines.
4 """
5
6 from ctypes import POINTER, c_bool, c_char_p, c_double, c_int, c_int64, c_void_p
7 from functools import partial
8
9 from django.contrib.gis.gdal.prototypes.errcheck import (
10 check_arg_errcode,
11 check_const_string,
12 check_errcode,
13 check_geom,
14 check_geom_offset,
15 check_pointer,
16 check_srs,
17 check_str_arg,
18 check_string,
19 )
20
21
22 class gdal_char_p(c_char_p):
23 pass
24
25
26 def bool_output(func, argtypes, errcheck=None):
27 """Generate a ctypes function that returns a boolean value."""
28 func.argtypes = argtypes
29 func.restype = c_bool
30 if errcheck:
31 func.errcheck = errcheck
32 return func
33
34
35 def double_output(func, argtypes, errcheck=False, strarg=False, cpl=False):
36 "Generate a ctypes function that returns a double value."
37 func.argtypes = argtypes
38 func.restype = c_double
39 if errcheck:
40 func.errcheck = partial(check_arg_errcode, cpl=cpl)
41 if strarg:
42 func.errcheck = check_str_arg
43 return func
44
45
46 def geom_output(func, argtypes, offset=None):
47 """
48 Generate a function that returns a Geometry either by reference
49 or directly (if the return_geom keyword is set to True).
50 """
51 # Setting the argument types
52 func.argtypes = argtypes
53
54 if not offset:
55 # When a geometry pointer is directly returned.
56 func.restype = c_void_p
57 func.errcheck = check_geom
58 else:
59 # Error code returned, geometry is returned by-reference.
60 func.restype = c_int
61
62 def geomerrcheck(result, func, cargs):
63 return check_geom_offset(result, func, cargs, offset)
64
65 func.errcheck = geomerrcheck
66
67 return func
68
69
70 def int_output(func, argtypes, errcheck=None):
71 "Generate a ctypes function that returns an integer value."
72 func.argtypes = argtypes
73 func.restype = c_int
74 if errcheck:
75 func.errcheck = errcheck
76 return func
77
78
79 def int64_output(func, argtypes):
80 "Generate a ctypes function that returns a 64-bit integer value."
81 func.argtypes = argtypes
82 func.restype = c_int64
83 return func
84
85
86 def srs_output(func, argtypes):
87 """
88 Generate a ctypes prototype for the given function with
89 the given C arguments that returns a pointer to an OGR
90 Spatial Reference System.
91 """
92 func.argtypes = argtypes
93 func.restype = c_void_p
94 func.errcheck = check_srs
95 return func
96
97
98 def const_string_output(func, argtypes, offset=None, decoding=None, cpl=False):
99 func.argtypes = argtypes
100 if offset:
101 func.restype = c_int
102 else:
103 func.restype = c_char_p
104
105 def _check_const(result, func, cargs):
106 res = check_const_string(result, func, cargs, offset=offset, cpl=cpl)
107 if res and decoding:
108 res = res.decode(decoding)
109 return res
110
111 func.errcheck = _check_const
112
113 return func
114
115
116 def string_output(func, argtypes, offset=-1, str_result=False, decoding=None):
117 """
118 Generate a ctypes prototype for the given function with the
119 given argument types that returns a string from a GDAL pointer.
120 The `const` flag indicates whether the allocated pointer should
121 be freed via the GDAL library routine VSIFree -- but only applies
122 only when `str_result` is True.
123 """
124 func.argtypes = argtypes
125 if str_result:
126 # Use subclass of c_char_p so the error checking routine
127 # can free the memory at the pointer's address.
128 func.restype = gdal_char_p
129 else:
130 # Error code is returned
131 func.restype = c_int
132
133 # Dynamically defining our error-checking function with the
134 # given offset.
135 def _check_str(result, func, cargs):
136 res = check_string(result, func, cargs, offset=offset, str_result=str_result)
137 if res and decoding:
138 res = res.decode(decoding)
139 return res
140
141 func.errcheck = _check_str
142 return func
143
144
145 def void_output(func, argtypes, errcheck=True, cpl=False):
146 """
147 For functions that don't only return an error code that needs to
148 be examined.
149 """
150 if argtypes:
151 func.argtypes = argtypes
152 if errcheck:
153 # `errcheck` keyword may be set to False for routines that
154 # return void, rather than a status code.
155 func.restype = c_int
156 func.errcheck = partial(check_errcode, cpl=cpl)
157 else:
158 func.restype = None
159
160 return func
161
162
163 def voidptr_output(func, argtypes, errcheck=True):
164 "For functions that return c_void_p."
165 func.argtypes = argtypes
166 func.restype = c_void_p
167 if errcheck:
168 func.errcheck = check_pointer
169 return func
170
171
172 def chararray_output(func, argtypes, errcheck=True):
173 """For functions that return a c_char_p array."""
174 func.argtypes = argtypes
175 func.restype = POINTER(c_char_p)
176 if errcheck:
177 func.errcheck = check_pointer
178 return func