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

https://code.google.com/p/mango-py/ · Python · 106 lines · 76 code · 17 blank · 13 comment · 2 complexity · c2a97f64b8054d670633de2e2806aabd MD5 · raw file

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