/django/contrib/gis/gdal/prototypes/generation.py

https://code.google.com/p/mango-py/ · Python · 119 lines · 63 code · 14 blank · 42 comment · 8 complexity · 8f1b7fbdf0adad1aaf6a3c3af9acd7f4 MD5 · raw file

  1. """
  2. This module contains functions that generate ctypes prototypes for the
  3. GDAL routines.
  4. """
  5. from ctypes import c_char_p, c_double, c_int, c_void_p
  6. from django.contrib.gis.gdal.prototypes.errcheck import \
  7. check_arg_errcode, check_errcode, check_geom, check_geom_offset, \
  8. check_pointer, check_srs, check_str_arg, check_string, check_const_string
  9. class gdal_char_p(c_char_p):
  10. pass
  11. def double_output(func, argtypes, errcheck=False, strarg=False):
  12. "Generates a ctypes function that returns a double value."
  13. func.argtypes = argtypes
  14. func.restype = c_double
  15. if errcheck: func.errcheck = check_arg_errcode
  16. if strarg: func.errcheck = check_str_arg
  17. return func
  18. def geom_output(func, argtypes, offset=None):
  19. """
  20. Generates a function that returns a Geometry either by reference
  21. or directly (if the return_geom keyword is set to True).
  22. """
  23. # Setting the argument types
  24. func.argtypes = argtypes
  25. if not offset:
  26. # When a geometry pointer is directly returned.
  27. func.restype = c_void_p
  28. func.errcheck = check_geom
  29. else:
  30. # Error code returned, geometry is returned by-reference.
  31. func.restype = c_int
  32. def geomerrcheck(result, func, cargs):
  33. return check_geom_offset(result, func, cargs, offset)
  34. func.errcheck = geomerrcheck
  35. return func
  36. def int_output(func, argtypes):
  37. "Generates a ctypes function that returns an integer value."
  38. func.argtypes = argtypes
  39. func.restype = c_int
  40. return func
  41. def srs_output(func, argtypes):
  42. """
  43. Generates a ctypes prototype for the given function with
  44. the given C arguments that returns a pointer to an OGR
  45. Spatial Reference System.
  46. """
  47. func.argtypes = argtypes
  48. func.restype = c_void_p
  49. func.errcheck = check_srs
  50. return func
  51. def const_string_output(func, argtypes, offset=None):
  52. func.argtypes = argtypes
  53. if offset:
  54. func.restype = c_int
  55. else:
  56. func.restype = c_char_p
  57. def _check_const(result, func, cargs):
  58. return check_const_string(result, func, cargs, offset=offset)
  59. func.errcheck = _check_const
  60. return func
  61. def string_output(func, argtypes, offset=-1, str_result=False):
  62. """
  63. Generates a ctypes prototype for the given function with the
  64. given argument types that returns a string from a GDAL pointer.
  65. The `const` flag indicates whether the allocated pointer should
  66. be freed via the GDAL library routine VSIFree -- but only applies
  67. only when `str_result` is True.
  68. """
  69. func.argtypes = argtypes
  70. if str_result:
  71. # Use subclass of c_char_p so the error checking routine
  72. # can free the memory at the pointer's address.
  73. func.restype = gdal_char_p
  74. else:
  75. # Error code is returned
  76. func.restype = c_int
  77. # Dynamically defining our error-checking function with the
  78. # given offset.
  79. def _check_str(result, func, cargs):
  80. return check_string(result, func, cargs,
  81. offset=offset, str_result=str_result)
  82. func.errcheck = _check_str
  83. return func
  84. def void_output(func, argtypes, errcheck=True):
  85. """
  86. For functions that don't only return an error code that needs to
  87. be examined.
  88. """
  89. if argtypes: func.argtypes = argtypes
  90. if errcheck:
  91. # `errcheck` keyword may be set to False for routines that
  92. # return void, rather than a status code.
  93. func.restype = c_int
  94. func.errcheck = check_errcode
  95. else:
  96. func.restype = None
  97. return func
  98. def voidptr_output(func, argtypes):
  99. "For functions that return c_void_p."
  100. func.argtypes = argtypes
  101. func.restype = c_void_p
  102. func.errcheck = check_pointer
  103. return func