PageRenderTime 22ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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