PageRenderTime 91ms CodeModel.GetById 76ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://code.google.com/p/mango-py/
Python | 119 lines | 77 code | 20 blank | 22 comment | 3 complexity | 7b48cc04eff355e1e3fd736ff223beb3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from ctypes import c_char_p, c_int, c_size_t, c_ubyte, c_uint, POINTER
  2. from django.contrib.gis.geos.libgeos import CS_PTR, GEOM_PTR, PREPGEOM_PTR, GEOS_PREPARE
  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
  6. # This is the return type used by binary output (WKB, HEX) routines.
  7. c_uchar_p = POINTER(c_ubyte)
  8. # We create a simple subclass of c_char_p here because when the response
  9. # type is set to c_char_p, you get a _Python_ string and there's no way
  10. # to access the string's address inside the error checking function.
  11. # In other words, you can't free the memory allocated inside GEOS. Previously,
  12. # the return type would just be omitted and the integer address would be
  13. # used -- but this allows us to be specific in the function definition and
  14. # keeps the reference so it may be free'd.
  15. class geos_char_p(c_char_p):
  16. pass
  17. ### ctypes generation functions ###
  18. def bin_constructor(func):
  19. "Generates a prototype for binary construction (HEX, WKB) GEOS routines."
  20. func.argtypes = [c_char_p, c_size_t]
  21. func.restype = GEOM_PTR
  22. func.errcheck = check_geom
  23. return func
  24. # HEX & WKB output
  25. def bin_output(func):
  26. "Generates a prototype for the routines that return a a sized string."
  27. func.argtypes = [GEOM_PTR, POINTER(c_size_t)]
  28. func.errcheck = check_sized_string
  29. func.restype = c_uchar_p
  30. return func
  31. def geom_output(func, argtypes):
  32. "For GEOS routines that return a geometry."
  33. if argtypes: func.argtypes = argtypes
  34. func.restype = GEOM_PTR
  35. func.errcheck = check_geom
  36. return func
  37. def geom_index(func):
  38. "For GEOS routines that return geometries from an index."
  39. return geom_output(func, [GEOM_PTR, c_int])
  40. def int_from_geom(func, zero=False):
  41. "Argument is a geometry, return type is an integer."
  42. func.argtypes = [GEOM_PTR]
  43. func.restype = c_int
  44. if zero:
  45. func.errcheck = check_zero
  46. else:
  47. func.errcheck = check_minus_one
  48. return func
  49. def string_from_geom(func):
  50. "Argument is a Geometry, return type is a string."
  51. func.argtypes = [GEOM_PTR]
  52. func.restype = geos_char_p
  53. func.errcheck = check_string
  54. return func
  55. ### ctypes prototypes ###
  56. # Deprecated creation routines from WKB, HEX, WKT
  57. from_hex = bin_constructor(GEOSFunc('GEOSGeomFromHEX_buf'))
  58. from_wkb = bin_constructor(GEOSFunc('GEOSGeomFromWKB_buf'))
  59. from_wkt = geom_output(GEOSFunc('GEOSGeomFromWKT'), [c_char_p])
  60. # Deprecated output routines
  61. to_hex = bin_output(GEOSFunc('GEOSGeomToHEX_buf'))
  62. to_wkb = bin_output(GEOSFunc('GEOSGeomToWKB_buf'))
  63. to_wkt = string_from_geom(GEOSFunc('GEOSGeomToWKT'))
  64. # The GEOS geometry type, typeid, num_coordites and number of geometries
  65. geos_normalize = int_from_geom(GEOSFunc('GEOSNormalize'))
  66. geos_type = string_from_geom(GEOSFunc('GEOSGeomType'))
  67. geos_typeid = int_from_geom(GEOSFunc('GEOSGeomTypeId'))
  68. get_dims = int_from_geom(GEOSFunc('GEOSGeom_getDimensions'), zero=True)
  69. get_num_coords = int_from_geom(GEOSFunc('GEOSGetNumCoordinates'))
  70. get_num_geoms = int_from_geom(GEOSFunc('GEOSGetNumGeometries'))
  71. # Geometry creation factories
  72. create_point = geom_output(GEOSFunc('GEOSGeom_createPoint'), [CS_PTR])
  73. create_linestring = geom_output(GEOSFunc('GEOSGeom_createLineString'), [CS_PTR])
  74. create_linearring = geom_output(GEOSFunc('GEOSGeom_createLinearRing'), [CS_PTR])
  75. # Polygon and collection creation routines are special and will not
  76. # have their argument types defined.
  77. create_polygon = geom_output(GEOSFunc('GEOSGeom_createPolygon'), None)
  78. create_collection = geom_output(GEOSFunc('GEOSGeom_createCollection'), None)
  79. # Ring routines
  80. get_extring = geom_output(GEOSFunc('GEOSGetExteriorRing'), [GEOM_PTR])
  81. get_intring = geom_index(GEOSFunc('GEOSGetInteriorRingN'))
  82. get_nrings = int_from_geom(GEOSFunc('GEOSGetNumInteriorRings'))
  83. # Collection Routines
  84. get_geomn = geom_index(GEOSFunc('GEOSGetGeometryN'))
  85. # Cloning
  86. geom_clone = GEOSFunc('GEOSGeom_clone')
  87. geom_clone.argtypes = [GEOM_PTR]
  88. geom_clone.restype = GEOM_PTR
  89. # Destruction routine.
  90. destroy_geom = GEOSFunc('GEOSGeom_destroy')
  91. destroy_geom.argtypes = [GEOM_PTR]
  92. destroy_geom.restype = None
  93. # SRID routines
  94. geos_get_srid = GEOSFunc('GEOSGetSRID')
  95. geos_get_srid.argtypes = [GEOM_PTR]
  96. geos_get_srid.restype = c_int
  97. geos_set_srid = GEOSFunc('GEOSSetSRID')
  98. geos_set_srid.argtypes = [GEOM_PTR, c_int]
  99. geos_set_srid.restype = None