1 from ctypes
import c_char_p
, c_int
, c_size_t
, c_ubyte
, POINTER
2 from django
.contrib
.gis
.geos
.libgeos
import CS_PTR
, GEOM_PTR
3 from django
.contrib
.gis
.geos
.prototypes
.errcheck
import (
4 check_geom
, check_minus_one
, check_sized_string
, check_string
, check_zero
)
5 from django
.contrib
.gis
.geos
.prototypes
.threadsafe
import GEOSFunc
7 # This is the return type used by binary output (WKB, HEX) routines.
8 c_uchar_p
= POINTER(c_ubyte
)
10 # We create a simple subclass of c_char_p here because when the response
11 # type is set to c_char_p, you get a _Python_ string and there's no way
12 # to access the string's address inside the error checking function.
13 # In other words, you can't free the memory allocated inside GEOS. Previously,
14 # the return type would just be omitted and the integer address would be
15 # used -- but this allows us to be specific in the function definition and
16 # keeps the reference so it may be free'd.
17 class geos_char_p(c_char_p
):
20 ### ctypes generation functions ###
21 def bin_constructor(func
):
22 "Generates a prototype for binary construction (HEX, WKB) GEOS routines."
23 func
.argtypes
= [c_char_p
, c_size_t
]
24 func
.restype
= GEOM_PTR
25 func
.errcheck
= check_geom
30 "Generates a prototype for the routines that return a a sized string."
31 func
.argtypes
= [GEOM_PTR
, POINTER(c_size_t
)]
32 func
.errcheck
= check_sized_string
33 func
.restype
= c_uchar_p
36 def geom_output(func
, argtypes
):
37 "For GEOS routines that return a geometry."
38 if argtypes
: func
.argtypes
= argtypes
39 func
.restype
= GEOM_PTR
40 func
.errcheck
= check_geom
44 "For GEOS routines that return geometries from an index."
45 return geom_output(func
, [GEOM_PTR
, c_int
])
47 def int_from_geom(func
, zero
=False):
48 "Argument is a geometry, return type is an integer."
49 func
.argtypes
= [GEOM_PTR
]
52 func
.errcheck
= check_zero
54 func
.errcheck
= check_minus_one
57 def string_from_geom(func
):
58 "Argument is a Geometry, return type is a string."
59 func
.argtypes
= [GEOM_PTR
]
60 func
.restype
= geos_char_p
61 func
.errcheck
= check_string
64 ### ctypes prototypes ###
66 # Deprecated creation routines from WKB, HEX, WKT
67 from_hex
= bin_constructor(GEOSFunc('GEOSGeomFromHEX_buf'))
68 from_wkb
= bin_constructor(GEOSFunc('GEOSGeomFromWKB_buf'))
69 from_wkt
= geom_output(GEOSFunc('GEOSGeomFromWKT'), [c_char_p
])
71 # Deprecated output routines
72 to_hex
= bin_output(GEOSFunc('GEOSGeomToHEX_buf'))
73 to_wkb
= bin_output(GEOSFunc('GEOSGeomToWKB_buf'))
74 to_wkt
= string_from_geom(GEOSFunc('GEOSGeomToWKT'))
76 # The GEOS geometry type, typeid, num_coordites and number of geometries
77 geos_normalize
= int_from_geom(GEOSFunc('GEOSNormalize'))
78 geos_type
= string_from_geom(GEOSFunc('GEOSGeomType'))
79 geos_typeid
= int_from_geom(GEOSFunc('GEOSGeomTypeId'))
80 get_dims
= int_from_geom(GEOSFunc('GEOSGeom_getDimensions'), zero
=True)
81 get_num_coords
= int_from_geom(GEOSFunc('GEOSGetNumCoordinates'))
82 get_num_geoms
= int_from_geom(GEOSFunc('GEOSGetNumGeometries'))
84 # Geometry creation factories
85 create_point
= geom_output(GEOSFunc('GEOSGeom_createPoint'), [CS_PTR
])
86 create_linestring
= geom_output(GEOSFunc('GEOSGeom_createLineString'), [CS_PTR
])
87 create_linearring
= geom_output(GEOSFunc('GEOSGeom_createLinearRing'), [CS_PTR
])
89 # Polygon and collection creation routines are special and will not
90 # have their argument types defined.
91 create_polygon
= geom_output(GEOSFunc('GEOSGeom_createPolygon'), None)
92 create_collection
= geom_output(GEOSFunc('GEOSGeom_createCollection'), None)
95 get_extring
= geom_output(GEOSFunc('GEOSGetExteriorRing'), [GEOM_PTR
])
96 get_intring
= geom_index(GEOSFunc('GEOSGetInteriorRingN'))
97 get_nrings
= int_from_geom(GEOSFunc('GEOSGetNumInteriorRings'))
100 get_geomn
= geom_index(GEOSFunc('GEOSGetGeometryN'))
103 geom_clone
= GEOSFunc('GEOSGeom_clone')
104 geom_clone
.argtypes
= [GEOM_PTR
]
105 geom_clone
.restype
= GEOM_PTR
107 # Destruction routine.
108 destroy_geom
= GEOSFunc('GEOSGeom_destroy')
109 destroy_geom
.argtypes
= [GEOM_PTR
]
110 destroy_geom
.restype
= None
113 geos_get_srid
= GEOSFunc('GEOSGetSRID')
114 geos_get_srid
.argtypes
= [GEOM_PTR
]
115 geos_get_srid
.restype
= c_int
117 geos_set_srid
= GEOSFunc('GEOSSetSRID')
118 geos_set_srid
.argtypes
= [GEOM_PTR
, c_int
]
119 geos_set_srid
.restype
= None