Debian Codegraph @

django/contrib/gis/geos/prototypes/geom.py

raw
1 from ctypes import POINTER, c_char_p, c_int, c_ubyte, c_uint
2
3 from django.contrib.gis.geos.libgeos import CS_PTR, GEOM_PTR, GEOSFuncFactory
4 from django.contrib.gis.geos.prototypes.errcheck import (
5 check_geom,
6 check_minus_one,
7 check_string,
8 )
9
10 # This is the return type used by binary output (WKB, HEX) routines.
11 c_uchar_p = POINTER(c_ubyte)
12
13
14 # We create a simple subclass of c_char_p here because when the response
15 # type is set to c_char_p, you get a _Python_ string and there's no way
16 # to access the string's address inside the error checking function.
17 # In other words, you can't free the memory allocated inside GEOS. Previously,
18 # the return type would just be omitted and the integer address would be
19 # used -- but this allows us to be specific in the function definition and
20 # keeps the reference so it may be free'd.
21 class geos_char_p(c_char_p):
22 pass
23
24
25 # ### ctypes factory classes ###
26 class GeomOutput(GEOSFuncFactory):
27 "For GEOS routines that return a geometry."
28
29 restype = GEOM_PTR
30 errcheck = staticmethod(check_geom)
31
32
33 class IntFromGeom(GEOSFuncFactory):
34 "Argument is a geometry, return type is an integer."
35
36 argtypes = [GEOM_PTR]
37 restype = c_int
38 errcheck = staticmethod(check_minus_one)
39
40
41 class StringFromGeom(GEOSFuncFactory):
42 "Argument is a Geometry, return type is a string."
43
44 argtypes = [GEOM_PTR]
45 restype = geos_char_p
46 errcheck = staticmethod(check_string)
47
48
49 # ### ctypes prototypes ###
50
51 # The GEOS geometry type, typeid, num_coordinates and number of geometries
52 geos_makevalid = GeomOutput("GEOSMakeValid", argtypes=[GEOM_PTR])
53 geos_normalize = IntFromGeom("GEOSNormalize")
54 geos_type = StringFromGeom("GEOSGeomType")
55 geos_typeid = IntFromGeom("GEOSGeomTypeId")
56 get_dims = GEOSFuncFactory("GEOSGeom_getDimensions", argtypes=[GEOM_PTR], restype=c_int)
57 get_num_coords = IntFromGeom("GEOSGetNumCoordinates")
58 get_num_geoms = IntFromGeom("GEOSGetNumGeometries")
59
60 # Geometry creation factories
61 create_point = GeomOutput("GEOSGeom_createPoint", argtypes=[CS_PTR])
62 create_linestring = GeomOutput("GEOSGeom_createLineString", argtypes=[CS_PTR])
63 create_linearring = GeomOutput("GEOSGeom_createLinearRing", argtypes=[CS_PTR])
64
65 # Polygon and collection creation routines need argument types defined
66 # for compatibility with some platforms, e.g. macOS ARM64. With argtypes
67 # defined, arrays are automatically cast and byref() calls are not needed.
68 create_polygon = GeomOutput(
69 "GEOSGeom_createPolygon",
70 argtypes=[GEOM_PTR, POINTER(GEOM_PTR), c_uint],
71 )
72 create_empty_polygon = GeomOutput("GEOSGeom_createEmptyPolygon", argtypes=[])
73 create_collection = GeomOutput(
74 "GEOSGeom_createCollection",
75 argtypes=[c_int, POINTER(GEOM_PTR), c_uint],
76 )
77
78 # Ring routines
79 get_extring = GeomOutput("GEOSGetExteriorRing", argtypes=[GEOM_PTR])
80 get_intring = GeomOutput("GEOSGetInteriorRingN", argtypes=[GEOM_PTR, c_int])
81 get_nrings = IntFromGeom("GEOSGetNumInteriorRings")
82
83 # Collection Routines
84 get_geomn = GeomOutput("GEOSGetGeometryN", argtypes=[GEOM_PTR, c_int])
85
86 # Cloning
87 geom_clone = GEOSFuncFactory("GEOSGeom_clone", argtypes=[GEOM_PTR], restype=GEOM_PTR)
88
89 # Destruction routine.
90 destroy_geom = GEOSFuncFactory("GEOSGeom_destroy", argtypes=[GEOM_PTR])
91
92 # SRID routines
93 geos_get_srid = GEOSFuncFactory("GEOSGetSRID", argtypes=[GEOM_PTR], restype=c_int)
94 geos_set_srid = GEOSFuncFactory("GEOSSetSRID", argtypes=[GEOM_PTR, c_int])