File: _geos.pyx

package info (click to toggle)
python-shapely 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 18,648; ansic: 6,615; makefile: 88; sh: 62
file content (45 lines) | stat: -rw-r--r-- 1,450 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#from shapely import GEOSException
from libc.stdio cimport snprintf
from libc.stdlib cimport free, malloc

import warnings

from shapely import GEOSException


cdef void geos_message_handler(const char* message, void* userdata) noexcept:
    snprintf(<char *>userdata, 1024, "%s", message)


cdef class get_geos_handle:
    '''This class provides a context manager that wraps the GEOS context handle.

    Example
    -------
    with get_geos_handle() as geos_handle:
        SomeGEOSFunc(geos_handle, ...<other params>)
    '''
    cdef GEOSContextHandle_t __enter__(self):
        self.handle = GEOS_init_r()
        self.last_error = <char *> malloc((1025) * sizeof(char))
        self.last_error[0] = 0
        self.last_warning = <char *> malloc((1025) * sizeof(char))
        self.last_warning[0] = 0
        GEOSContext_setErrorMessageHandler_r(
            self.handle, &geos_message_handler, self.last_error
        )
        GEOSContext_setNoticeMessageHandler_r(
            self.handle, &geos_message_handler, self.last_warning
        )
        return self.handle

    def __exit__(self, type, value, traceback):
        try:
            if self.last_error[0] != 0:
                raise GEOSException(self.last_error)
            if self.last_warning[0] != 0:
                warnings.warn(self.last_warning)
        finally:
            GEOS_finish_r(self.handle)
            free(self.last_error)
            free(self.last_warning)