this is the return type used by binary output wkb hex routines we create a simple subclass of ccharp here because when the response type is set to ccharp you get a python string and there s no way to access the string s address inside the error checking function in other words you can t free the memory allocated inside geos previously the return type would just be omitted and the integer address would be used but this allows us to be specific in the function definition and keeps the reference so it may be free d ctypes factory classes ctypes prototypes the geos geometry type typeid numcoordinates and number of geometries geometry creation factories polygon and collection creation routines need argument types defined for compatibility with some platforms e g macos arm64 with argtypes defined arrays are automatically cast and byref calls are not needed ring routines collection routines cloning destruction routine srid routines this is the return type used by binary output wkb hex routines we create a simple subclass of c_char_p here because when the response type is set to c_char_p you get a _python_ string and there s no way to access the string s address inside the error checking function in other words you can t free the memory allocated inside geos previously the return type would just be omitted and the integer address would be used but this allows us to be specific in the function definition and keeps the reference so it may be free d ctypes factory classes ctypes prototypes the geos geometry type typeid num_coordinates and number of geometries geometry creation factories polygon and collection creation routines need argument types defined for compatibility with some platforms e g macos arm64 with argtypes defined arrays are automatically cast and byref calls are not needed ring routines collection routines cloning destruction routine srid routines
from ctypes import POINTER, c_char_p, c_int, c_ubyte, c_uint

from django.contrib.gis.geos.libgeos import CS_PTR, GEOM_PTR, GEOSFuncFactory
from django.contrib.gis.geos.prototypes.errcheck import (
    check_geom,
    check_minus_one,
    check_string,
)


c_uchar_p = POINTER(c_ubyte)









class geos_char_p(c_char_p):
    pass



class GeomOutput(GEOSFuncFactory):
    "For GEOS routines that return a geometry."
    restype = GEOM_PTR
    errcheck = staticmethod(check_geom)


class IntFromGeom(GEOSFuncFactory):
    "Argument is a geometry, return type is an integer."
    argtypes = [GEOM_PTR]
    restype = c_int
    errcheck = staticmethod(check_minus_one)


class StringFromGeom(GEOSFuncFactory):
    "Argument is a Geometry, return type is a string."
    argtypes = [GEOM_PTR]
    restype = geos_char_p
    errcheck = staticmethod(check_string)





geos_makevalid = GeomOutput("GEOSMakeValid", argtypes=[GEOM_PTR])
geos_normalize = IntFromGeom("GEOSNormalize")
geos_type = StringFromGeom("GEOSGeomType")
geos_typeid = IntFromGeom("GEOSGeomTypeId")
get_dims = GEOSFuncFactory("GEOSGeom_getDimensions", argtypes=[GEOM_PTR], restype=c_int)
get_num_coords = IntFromGeom("GEOSGetNumCoordinates")
get_num_geoms = IntFromGeom("GEOSGetNumGeometries")


create_point = GeomOutput("GEOSGeom_createPoint", argtypes=[CS_PTR])
create_linestring = GeomOutput("GEOSGeom_createLineString", argtypes=[CS_PTR])
create_linearring = GeomOutput("GEOSGeom_createLinearRing", argtypes=[CS_PTR])




create_polygon = GeomOutput(
    "GEOSGeom_createPolygon",
    argtypes=[GEOM_PTR, POINTER(GEOM_PTR), c_uint],
)
create_empty_polygon = GeomOutput("GEOSGeom_createEmptyPolygon", argtypes=[])
create_collection = GeomOutput(
    "GEOSGeom_createCollection",
    argtypes=[c_int, POINTER(GEOM_PTR), c_uint],
)


get_extring = GeomOutput("GEOSGetExteriorRing", argtypes=[GEOM_PTR])
get_intring = GeomOutput("GEOSGetInteriorRingN", argtypes=[GEOM_PTR, c_int])
get_nrings = IntFromGeom("GEOSGetNumInteriorRings")


get_geomn = GeomOutput("GEOSGetGeometryN", argtypes=[GEOM_PTR, c_int])


geom_clone = GEOSFuncFactory("GEOSGeom_clone", argtypes=[GEOM_PTR], restype=GEOM_PTR)


destroy_geom = GEOSFuncFactory("GEOSGeom_destroy", argtypes=[GEOM_PTR])


geos_get_srid = GEOSFuncFactory("GEOSGetSRID", argtypes=[GEOM_PTR], restype=c_int)
geos_set_srid = GEOSFuncFactory("GEOSSetSRID", argtypes=[GEOM_PTR, c_int])