PageRenderTime 46ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/geos/point.py

https://code.google.com/p/mango-py/
Python | 135 lines | 93 code | 23 blank | 19 comment | 22 complexity | 47617c662e0b71b0c784e9ad0dc64a14 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from ctypes import c_uint
  2. from django.contrib.gis.geos.error import GEOSException
  3. from django.contrib.gis.geos.geometry import GEOSGeometry
  4. from django.contrib.gis.geos import prototypes as capi
  5. class Point(GEOSGeometry):
  6. _minlength = 2
  7. _maxlength = 3
  8. def __init__(self, x, y=None, z=None, srid=None):
  9. """
  10. The Point object may be initialized with either a tuple, or individual
  11. parameters.
  12. For Example:
  13. >>> p = Point((5, 23)) # 2D point, passed in as a tuple
  14. >>> p = Point(5, 23, 8) # 3D point, passed in with individual parameters
  15. """
  16. if isinstance(x, (tuple, list)):
  17. # Here a tuple or list was passed in under the `x` parameter.
  18. ndim = len(x)
  19. coords = x
  20. elif isinstance(x, (int, float, long)) and isinstance(y, (int, float, long)):
  21. # Here X, Y, and (optionally) Z were passed in individually, as parameters.
  22. if isinstance(z, (int, float, long)):
  23. ndim = 3
  24. coords = [x, y, z]
  25. else:
  26. ndim = 2
  27. coords = [x, y]
  28. else:
  29. raise TypeError('Invalid parameters given for Point initialization.')
  30. point = self._create_point(ndim, coords)
  31. # Initializing using the address returned from the GEOS
  32. # createPoint factory.
  33. super(Point, self).__init__(point, srid=srid)
  34. def _create_point(self, ndim, coords):
  35. """
  36. Create a coordinate sequence, set X, Y, [Z], and create point
  37. """
  38. if ndim < 2 or ndim > 3:
  39. raise TypeError('Invalid point dimension: %s' % str(ndim))
  40. cs = capi.create_cs(c_uint(1), c_uint(ndim))
  41. i = iter(coords)
  42. capi.cs_setx(cs, 0, i.next())
  43. capi.cs_sety(cs, 0, i.next())
  44. if ndim == 3: capi.cs_setz(cs, 0, i.next())
  45. return capi.create_point(cs)
  46. def _set_list(self, length, items):
  47. ptr = self._create_point(length, items)
  48. if ptr:
  49. capi.destroy_geom(self.ptr)
  50. self._ptr = ptr
  51. self._set_cs()
  52. else:
  53. # can this happen?
  54. raise GEOSException('Geometry resulting from slice deletion was invalid.')
  55. def _set_single(self, index, value):
  56. self._cs.setOrdinate(index, 0, value)
  57. def __iter__(self):
  58. "Allows iteration over coordinates of this Point."
  59. for i in xrange(len(self)):
  60. yield self[i]
  61. def __len__(self):
  62. "Returns the number of dimensions for this Point (either 0, 2 or 3)."
  63. if self.empty: return 0
  64. if self.hasz: return 3
  65. else: return 2
  66. def _get_single_external(self, index):
  67. if index == 0:
  68. return self.x
  69. elif index == 1:
  70. return self.y
  71. elif index == 2:
  72. return self.z
  73. _get_single_internal = _get_single_external
  74. def get_x(self):
  75. "Returns the X component of the Point."
  76. return self._cs.getOrdinate(0, 0)
  77. def set_x(self, value):
  78. "Sets the X component of the Point."
  79. self._cs.setOrdinate(0, 0, value)
  80. def get_y(self):
  81. "Returns the Y component of the Point."
  82. return self._cs.getOrdinate(1, 0)
  83. def set_y(self, value):
  84. "Sets the Y component of the Point."
  85. self._cs.setOrdinate(1, 0, value)
  86. def get_z(self):
  87. "Returns the Z component of the Point."
  88. if self.hasz:
  89. return self._cs.getOrdinate(2, 0)
  90. else:
  91. return None
  92. def set_z(self, value):
  93. "Sets the Z component of the Point."
  94. if self.hasz:
  95. self._cs.setOrdinate(2, 0, value)
  96. else:
  97. raise GEOSException('Cannot set Z on 2D Point.')
  98. # X, Y, Z properties
  99. x = property(get_x, set_x)
  100. y = property(get_y, set_y)
  101. z = property(get_z, set_z)
  102. ### Tuple setting and retrieval routines. ###
  103. def get_coords(self):
  104. "Returns a tuple of the point."
  105. return self._cs.tuple
  106. def set_coords(self, tup):
  107. "Sets the coordinates of the point with the given tuple."
  108. self._cs[0] = tup
  109. # The tuple and coords properties
  110. tuple = property(get_coords, set_coords)
  111. coords = tuple