You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

errcheck.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """
  2. This module houses the error-checking routines used by the GDAL
  3. ctypes prototypes.
  4. """
  5. from ctypes import c_void_p, string_at
  6. from django.contrib.gis.gdal.error import (
  7. GDALException, SRSException, check_err,
  8. )
  9. from django.contrib.gis.gdal.libgdal import lgdal
  10. # Helper routines for retrieving pointers and/or values from
  11. # arguments passed in by reference.
  12. def arg_byref(args, offset=-1):
  13. "Return the pointer argument's by-reference value."
  14. return args[offset]._obj.value
  15. def ptr_byref(args, offset=-1):
  16. "Return the pointer argument passed in by-reference."
  17. return args[offset]._obj
  18. # ### String checking Routines ###
  19. def check_const_string(result, func, cargs, offset=None, cpl=False):
  20. """
  21. Similar functionality to `check_string`, but does not free the pointer.
  22. """
  23. if offset:
  24. check_err(result, cpl=cpl)
  25. ptr = ptr_byref(cargs, offset)
  26. return ptr.value
  27. else:
  28. return result
  29. def check_string(result, func, cargs, offset=-1, str_result=False):
  30. """
  31. Check the string output returned from the given function, and free
  32. the string pointer allocated by OGR. The `str_result` keyword
  33. may be used when the result is the string pointer, otherwise
  34. the OGR error code is assumed. The `offset` keyword may be used
  35. to extract the string pointer passed in by-reference at the given
  36. slice offset in the function arguments.
  37. """
  38. if str_result:
  39. # For routines that return a string.
  40. ptr = result
  41. if not ptr:
  42. s = None
  43. else:
  44. s = string_at(result)
  45. else:
  46. # Error-code return specified.
  47. check_err(result)
  48. ptr = ptr_byref(cargs, offset)
  49. # Getting the string value
  50. s = ptr.value
  51. # Correctly freeing the allocated memory behind GDAL pointer
  52. # with the VSIFree routine.
  53. if ptr:
  54. lgdal.VSIFree(ptr)
  55. return s
  56. # ### DataSource, Layer error-checking ###
  57. # ### Envelope checking ###
  58. def check_envelope(result, func, cargs, offset=-1):
  59. "Check a function that returns an OGR Envelope by reference."
  60. env = ptr_byref(cargs, offset)
  61. return env
  62. # ### Geometry error-checking routines ###
  63. def check_geom(result, func, cargs):
  64. "Check a function that returns a geometry."
  65. # OGR_G_Clone may return an integer, even though the
  66. # restype is set to c_void_p
  67. if isinstance(result, int):
  68. result = c_void_p(result)
  69. if not result:
  70. raise GDALException('Invalid geometry pointer returned from "%s".' % func.__name__)
  71. return result
  72. def check_geom_offset(result, func, cargs, offset=-1):
  73. "Check the geometry at the given offset in the C parameter list."
  74. check_err(result)
  75. geom = ptr_byref(cargs, offset=offset)
  76. return check_geom(geom, func, cargs)
  77. # ### Spatial Reference error-checking routines ###
  78. def check_srs(result, func, cargs):
  79. if isinstance(result, int):
  80. result = c_void_p(result)
  81. if not result:
  82. raise SRSException('Invalid spatial reference pointer returned from "%s".' % func.__name__)
  83. return result
  84. # ### Other error-checking routines ###
  85. def check_arg_errcode(result, func, cargs, cpl=False):
  86. """
  87. The error code is returned in the last argument, by reference.
  88. Check its value with `check_err` before returning the result.
  89. """
  90. check_err(arg_byref(cargs), cpl=cpl)
  91. return result
  92. def check_errcode(result, func, cargs, cpl=False):
  93. """
  94. Check the error code returned (c_int).
  95. """
  96. check_err(result, cpl=cpl)
  97. def check_pointer(result, func, cargs):
  98. "Make sure the result pointer is valid."
  99. if isinstance(result, int):
  100. result = c_void_p(result)
  101. if result:
  102. return result
  103. else:
  104. raise GDALException('Invalid pointer returned from "%s"' % func.__name__)
  105. def check_str_arg(result, func, cargs):
  106. """
  107. This is for the OSRGet[Angular|Linear]Units functions, which
  108. require that the returned string pointer not be freed. This
  109. returns both the double and string values.
  110. """
  111. dbl = result
  112. ptr = cargs[-1]._obj
  113. return dbl, ptr.value.decode()