represent a geos context handle ptrtype contextptr destructor lgeos finishgeosr def initself initializing the context handler for this thread with the notice and error handler self ptr lgeos initgeosrnoticeh errorh defining a threadlocal object and creating an instance to hold a reference to geoscontexthandle for this thread class geoscontextthreading local handle none threadcontext geoscontext class geosfunc def initself funcname geos threadsafe function signatures end with r and take an additional context handle parameter self cfunc getattrlgeos funcname r create a reference to threadcontext so it s not garbagecollected before an attempt to call this object self threadcontext threadcontext def callself args create a context handle if one doesn t exist for this thread self threadcontext handle self threadcontext handle or geoscontexthandle call the threaded geos routine with the pointer of the context handle as the first argument return self cfuncself threadcontext handle ptr args def strself return self cfunc name argtypes property def getargtypesself return self cfunc argtypes def setargtypesself argtypes self cfunc argtypes contextptr argtypes argtypes propertygetargtypes setargtypes restype property def getrestypeself return self cfunc restype def setrestypeself restype self cfunc restype restype restype propertygetrestype setrestype errcheck property def geterrcheckself return self cfunc errcheck def seterrcheckself errcheck self cfunc errcheck errcheck errcheck propertygeterrcheck seterrcheck represent a geos context handle initializing the context handler for this thread with the notice and error handler defining a thread local object and creating an instance to hold a reference to geoscontexthandle for this thread serve as a wrapper for geos c functions use thread safe function variants when available geos thread safe function signatures end with _r and take an additional context handle parameter create a reference to thread_context so it s not garbage collected before an attempt to call this object create a context handle if one doesn t exist for this thread call the threaded geos routine with the pointer of the context handle as the first argument argtypes property restype property errcheck property
import threading

from django.contrib.gis.geos.base import GEOSBase
from django.contrib.gis.geos.libgeos import CONTEXT_PTR, error_h, lgeos, notice_h


class GEOSContextHandle(GEOSBase):
    

    ptr_type = CONTEXT_PTR
    destructor = lgeos.finishGEOS_r

    def __init__(self):
        
        
        self.ptr = lgeos.initGEOS_r(notice_h, error_h)




class GEOSContext(threading.local):
    handle = None


thread_context = GEOSContext()


class GEOSFunc:
    

    def __init__(self, func_name):
        
        
        self.cfunc = getattr(lgeos, func_name + "_r")
        
        
        self.thread_context = thread_context

    def __call__(self, *args):
        
        self.thread_context.handle = self.thread_context.handle or GEOSContextHandle()
        
        
        return self.cfunc(self.thread_context.handle.ptr, *args)

    def __str__(self):
        return self.cfunc.__name__

    
    def _get_argtypes(self):
        return self.cfunc.argtypes

    def _set_argtypes(self, argtypes):
        self.cfunc.argtypes = [CONTEXT_PTR, *argtypes]

    argtypes = property(_get_argtypes, _set_argtypes)

    
    def _get_restype(self):
        return self.cfunc.restype

    def _set_restype(self, restype):
        self.cfunc.restype = restype

    restype = property(_get_restype, _set_restype)

    
    def _get_errcheck(self):
        return self.cfunc.errcheck

    def _set_errcheck(self, errcheck):
        self.cfunc.errcheck = errcheck

    errcheck = property(_get_errcheck, _set_errcheck)