/django/contrib/gis/geos/prototypes/coordseq.py
Python | 83 lines | 58 code | 14 blank | 11 comment | 5 complexity | 692bd71d949786145886f213a3f93086 MD5 | raw file
1from ctypes import c_double, c_int, c_uint, POINTER 2from django.contrib.gis.geos.libgeos import GEOM_PTR, CS_PTR 3from django.contrib.gis.geos.prototypes.errcheck import last_arg_byref, GEOSException 4from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc 5 6## Error-checking routines specific to coordinate sequences. ## 7def check_cs_ptr(result, func, cargs): 8 "Error checking on routines that return Geometries." 9 if not result: 10 raise GEOSException('Error encountered checking Coordinate Sequence returned from GEOS C function "%s".' % func.__name__) 11 return result 12 13def check_cs_op(result, func, cargs): 14 "Checks the status code of a coordinate sequence operation." 15 if result == 0: 16 raise GEOSException('Could not set value on coordinate sequence') 17 else: 18 return result 19 20def check_cs_get(result, func, cargs): 21 "Checking the coordinate sequence retrieval." 22 check_cs_op(result, func, cargs) 23 # Object in by reference, return its value. 24 return last_arg_byref(cargs) 25 26## Coordinate sequence prototype generation functions. ## 27def cs_int(func): 28 "For coordinate sequence routines that return an integer." 29 func.argtypes = [CS_PTR, POINTER(c_uint)] 30 func.restype = c_int 31 func.errcheck = check_cs_get 32 return func 33 34def cs_operation(func, ordinate=False, get=False): 35 "For coordinate sequence operations." 36 if get: 37 # Get routines get double parameter passed-in by reference. 38 func.errcheck = check_cs_get 39 dbl_param = POINTER(c_double) 40 else: 41 func.errcheck = check_cs_op 42 dbl_param = c_double 43 44 if ordinate: 45 # Get/Set ordinate routines have an extra uint parameter. 46 func.argtypes = [CS_PTR, c_uint, c_uint, dbl_param] 47 else: 48 func.argtypes = [CS_PTR, c_uint, dbl_param] 49 50 func.restype = c_int 51 return func 52 53def cs_output(func, argtypes): 54 "For routines that return a coordinate sequence." 55 func.argtypes = argtypes 56 func.restype = CS_PTR 57 func.errcheck = check_cs_ptr 58 return func 59 60## Coordinate Sequence ctypes prototypes ## 61 62# Coordinate Sequence constructors & cloning. 63cs_clone = cs_output(GEOSFunc('GEOSCoordSeq_clone'), [CS_PTR]) 64create_cs = cs_output(GEOSFunc('GEOSCoordSeq_create'), [c_uint, c_uint]) 65get_cs = cs_output(GEOSFunc('GEOSGeom_getCoordSeq'), [GEOM_PTR]) 66 67# Getting, setting ordinate 68cs_getordinate = cs_operation(GEOSFunc('GEOSCoordSeq_getOrdinate'), ordinate=True, get=True) 69cs_setordinate = cs_operation(GEOSFunc('GEOSCoordSeq_setOrdinate'), ordinate=True) 70 71# For getting, x, y, z 72cs_getx = cs_operation(GEOSFunc('GEOSCoordSeq_getX'), get=True) 73cs_gety = cs_operation(GEOSFunc('GEOSCoordSeq_getY'), get=True) 74cs_getz = cs_operation(GEOSFunc('GEOSCoordSeq_getZ'), get=True) 75 76# For setting, x, y, z 77cs_setx = cs_operation(GEOSFunc('GEOSCoordSeq_setX')) 78cs_sety = cs_operation(GEOSFunc('GEOSCoordSeq_setY')) 79cs_setz = cs_operation(GEOSFunc('GEOSCoordSeq_setZ')) 80 81# These routines return size & dimensions. 82cs_getsize = cs_int(GEOSFunc('GEOSCoordSeq_getSize')) 83cs_getdims = cs_int(GEOSFunc('GEOSCoordSeq_getDimensions'))