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.

geom.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from ctypes import POINTER, c_char_p, c_int, c_size_t, c_ubyte
  2. from django.contrib.gis.geos.libgeos import CS_PTR, GEOM_PTR, GEOSFuncFactory
  3. from django.contrib.gis.geos.prototypes.errcheck import (
  4. check_geom, check_minus_one, check_sized_string, check_string,
  5. )
  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 factory classes ###
  18. class BinConstructor(GEOSFuncFactory):
  19. "Generate a prototype for binary construction (HEX, WKB) GEOS routines."
  20. argtypes = [c_char_p, c_size_t]
  21. restype = GEOM_PTR
  22. errcheck = staticmethod(check_geom)
  23. # HEX & WKB output
  24. class BinOutput(GEOSFuncFactory):
  25. "Generate a prototype for the routines that return a sized string."
  26. argtypes = [GEOM_PTR, POINTER(c_size_t)]
  27. restype = c_uchar_p
  28. errcheck = staticmethod(check_sized_string)
  29. class GeomOutput(GEOSFuncFactory):
  30. "For GEOS routines that return a geometry."
  31. restype = GEOM_PTR
  32. errcheck = staticmethod(check_geom)
  33. class IntFromGeom(GEOSFuncFactory):
  34. "Argument is a geometry, return type is an integer."
  35. argtypes = [GEOM_PTR]
  36. restype = c_int
  37. errcheck = staticmethod(check_minus_one)
  38. class StringFromGeom(GEOSFuncFactory):
  39. "Argument is a Geometry, return type is a string."
  40. argtypes = [GEOM_PTR]
  41. restype = geos_char_p
  42. errcheck = staticmethod(check_string)
  43. # ### ctypes prototypes ###
  44. # The GEOS geometry type, typeid, num_coordinates and number of geometries
  45. geos_normalize = IntFromGeom('GEOSNormalize')
  46. geos_type = StringFromGeom('GEOSGeomType')
  47. geos_typeid = IntFromGeom('GEOSGeomTypeId')
  48. get_dims = GEOSFuncFactory('GEOSGeom_getDimensions', argtypes=[GEOM_PTR], restype=c_int)
  49. get_num_coords = IntFromGeom('GEOSGetNumCoordinates')
  50. get_num_geoms = IntFromGeom('GEOSGetNumGeometries')
  51. # Geometry creation factories
  52. create_point = GeomOutput('GEOSGeom_createPoint', argtypes=[CS_PTR])
  53. create_linestring = GeomOutput('GEOSGeom_createLineString', argtypes=[CS_PTR])
  54. create_linearring = GeomOutput('GEOSGeom_createLinearRing', argtypes=[CS_PTR])
  55. # Polygon and collection creation routines are special and will not
  56. # have their argument types defined.
  57. create_polygon = GeomOutput('GEOSGeom_createPolygon')
  58. create_empty_polygon = GeomOutput('GEOSGeom_createEmptyPolygon')
  59. create_collection = GeomOutput('GEOSGeom_createCollection')
  60. # Ring routines
  61. get_extring = GeomOutput('GEOSGetExteriorRing', argtypes=[GEOM_PTR])
  62. get_intring = GeomOutput('GEOSGetInteriorRingN', argtypes=[GEOM_PTR, c_int])
  63. get_nrings = IntFromGeom('GEOSGetNumInteriorRings')
  64. # Collection Routines
  65. get_geomn = GeomOutput('GEOSGetGeometryN', argtypes=[GEOM_PTR, c_int])
  66. # Cloning
  67. geom_clone = GEOSFuncFactory('GEOSGeom_clone', argtypes=[GEOM_PTR], restype=GEOM_PTR)
  68. # Destruction routine.
  69. destroy_geom = GEOSFuncFactory('GEOSGeom_destroy', argtypes=[GEOM_PTR])
  70. # SRID routines
  71. geos_get_srid = GEOSFuncFactory('GEOSGetSRID', argtypes=[GEOM_PTR], restype=c_int)
  72. geos_set_srid = GEOSFuncFactory('GEOSSetSRID', argtypes=[GEOM_PTR, c_int])