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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from ctypes import POINTER, c_char_p, c_double, c_int, c_void_p
  2. from django.contrib.gis.gdal.envelope import OGREnvelope
  3. from django.contrib.gis.gdal.libgdal import lgdal
  4. from django.contrib.gis.gdal.prototypes.errcheck import check_envelope
  5. from django.contrib.gis.gdal.prototypes.generation import (
  6. const_string_output, double_output, geom_output, int_output, srs_output,
  7. string_output, void_output,
  8. )
  9. # ### Generation routines specific to this module ###
  10. def env_func(f, argtypes):
  11. "For getting OGREnvelopes."
  12. f.argtypes = argtypes
  13. f.restype = None
  14. f.errcheck = check_envelope
  15. return f
  16. def pnt_func(f):
  17. "For accessing point information."
  18. return double_output(f, [c_void_p, c_int])
  19. def topology_func(f):
  20. f.argtypes = [c_void_p, c_void_p]
  21. f.restype = c_int
  22. f.errcheck = lambda result, func, cargs: bool(result)
  23. return f
  24. # ### OGR_G ctypes function prototypes ###
  25. # GeoJSON routines.
  26. from_json = geom_output(lgdal.OGR_G_CreateGeometryFromJson, [c_char_p])
  27. to_json = string_output(lgdal.OGR_G_ExportToJson, [c_void_p], str_result=True, decoding='ascii')
  28. to_kml = string_output(lgdal.OGR_G_ExportToKML, [c_void_p, c_char_p], str_result=True, decoding='ascii')
  29. # GetX, GetY, GetZ all return doubles.
  30. getx = pnt_func(lgdal.OGR_G_GetX)
  31. gety = pnt_func(lgdal.OGR_G_GetY)
  32. getz = pnt_func(lgdal.OGR_G_GetZ)
  33. # Geometry creation routines.
  34. from_wkb = geom_output(lgdal.OGR_G_CreateFromWkb, [c_char_p, c_void_p, POINTER(c_void_p), c_int], offset=-2)
  35. from_wkt = geom_output(lgdal.OGR_G_CreateFromWkt, [POINTER(c_char_p), c_void_p, POINTER(c_void_p)], offset=-1)
  36. from_gml = geom_output(lgdal.OGR_G_CreateFromGML, [c_char_p])
  37. create_geom = geom_output(lgdal.OGR_G_CreateGeometry, [c_int])
  38. clone_geom = geom_output(lgdal.OGR_G_Clone, [c_void_p])
  39. get_geom_ref = geom_output(lgdal.OGR_G_GetGeometryRef, [c_void_p, c_int])
  40. get_boundary = geom_output(lgdal.OGR_G_GetBoundary, [c_void_p])
  41. geom_convex_hull = geom_output(lgdal.OGR_G_ConvexHull, [c_void_p])
  42. geom_diff = geom_output(lgdal.OGR_G_Difference, [c_void_p, c_void_p])
  43. geom_intersection = geom_output(lgdal.OGR_G_Intersection, [c_void_p, c_void_p])
  44. geom_sym_diff = geom_output(lgdal.OGR_G_SymmetricDifference, [c_void_p, c_void_p])
  45. geom_union = geom_output(lgdal.OGR_G_Union, [c_void_p, c_void_p])
  46. # Geometry modification routines.
  47. add_geom = void_output(lgdal.OGR_G_AddGeometry, [c_void_p, c_void_p])
  48. import_wkt = void_output(lgdal.OGR_G_ImportFromWkt, [c_void_p, POINTER(c_char_p)])
  49. # Destroys a geometry
  50. destroy_geom = void_output(lgdal.OGR_G_DestroyGeometry, [c_void_p], errcheck=False)
  51. # Geometry export routines.
  52. to_wkb = void_output(lgdal.OGR_G_ExportToWkb, None, errcheck=True) # special handling for WKB.
  53. to_wkt = string_output(lgdal.OGR_G_ExportToWkt, [c_void_p, POINTER(c_char_p)], decoding='ascii')
  54. to_gml = string_output(lgdal.OGR_G_ExportToGML, [c_void_p], str_result=True, decoding='ascii')
  55. get_wkbsize = int_output(lgdal.OGR_G_WkbSize, [c_void_p])
  56. # Geometry spatial-reference related routines.
  57. assign_srs = void_output(lgdal.OGR_G_AssignSpatialReference, [c_void_p, c_void_p], errcheck=False)
  58. get_geom_srs = srs_output(lgdal.OGR_G_GetSpatialReference, [c_void_p])
  59. # Geometry properties
  60. get_area = double_output(lgdal.OGR_G_GetArea, [c_void_p])
  61. get_centroid = void_output(lgdal.OGR_G_Centroid, [c_void_p, c_void_p])
  62. get_dims = int_output(lgdal.OGR_G_GetDimension, [c_void_p])
  63. get_coord_dim = int_output(lgdal.OGR_G_GetCoordinateDimension, [c_void_p])
  64. set_coord_dim = void_output(lgdal.OGR_G_SetCoordinateDimension, [c_void_p, c_int], errcheck=False)
  65. is_empty = int_output(lgdal.OGR_G_IsEmpty, [c_void_p], errcheck=lambda result, func, cargs: bool(result))
  66. get_geom_count = int_output(lgdal.OGR_G_GetGeometryCount, [c_void_p])
  67. get_geom_name = const_string_output(lgdal.OGR_G_GetGeometryName, [c_void_p], decoding='ascii')
  68. get_geom_type = int_output(lgdal.OGR_G_GetGeometryType, [c_void_p])
  69. get_point_count = int_output(lgdal.OGR_G_GetPointCount, [c_void_p])
  70. get_point = void_output(
  71. lgdal.OGR_G_GetPoint,
  72. [c_void_p, c_int, POINTER(c_double), POINTER(c_double), POINTER(c_double)], errcheck=False
  73. )
  74. geom_close_rings = void_output(lgdal.OGR_G_CloseRings, [c_void_p], errcheck=False)
  75. # Topology routines.
  76. ogr_contains = topology_func(lgdal.OGR_G_Contains)
  77. ogr_crosses = topology_func(lgdal.OGR_G_Crosses)
  78. ogr_disjoint = topology_func(lgdal.OGR_G_Disjoint)
  79. ogr_equals = topology_func(lgdal.OGR_G_Equals)
  80. ogr_intersects = topology_func(lgdal.OGR_G_Intersects)
  81. ogr_overlaps = topology_func(lgdal.OGR_G_Overlaps)
  82. ogr_touches = topology_func(lgdal.OGR_G_Touches)
  83. ogr_within = topology_func(lgdal.OGR_G_Within)
  84. # Transformation routines.
  85. geom_transform = void_output(lgdal.OGR_G_Transform, [c_void_p, c_void_p])
  86. geom_transform_to = void_output(lgdal.OGR_G_TransformTo, [c_void_p, c_void_p])
  87. # For retrieving the envelope of the geometry.
  88. get_envelope = env_func(lgdal.OGR_G_GetEnvelope, [c_void_p, POINTER(OGREnvelope)])