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.

point.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from ctypes import c_uint
  2. from django.contrib.gis import gdal
  3. from django.contrib.gis.geos import prototypes as capi
  4. from django.contrib.gis.geos.error import GEOSException
  5. from django.contrib.gis.geos.geometry import GEOSGeometry
  6. class Point(GEOSGeometry):
  7. _minlength = 2
  8. _maxlength = 3
  9. has_cs = True
  10. def __init__(self, x=None, y=None, z=None, srid=None):
  11. """
  12. The Point object may be initialized with either a tuple, or individual
  13. parameters.
  14. For example:
  15. >>> p = Point((5, 23)) # 2D point, passed in as a tuple
  16. >>> p = Point(5, 23, 8) # 3D point, passed in with individual parameters
  17. """
  18. if x is None:
  19. coords = []
  20. elif isinstance(x, (tuple, list)):
  21. # Here a tuple or list was passed in under the `x` parameter.
  22. coords = x
  23. elif isinstance(x, (float, int)) and isinstance(y, (float, int)):
  24. # Here X, Y, and (optionally) Z were passed in individually, as parameters.
  25. if isinstance(z, (float, int)):
  26. coords = [x, y, z]
  27. else:
  28. coords = [x, y]
  29. else:
  30. raise TypeError('Invalid parameters given for Point initialization.')
  31. point = self._create_point(len(coords), coords)
  32. # Initializing using the address returned from the GEOS
  33. # createPoint factory.
  34. super().__init__(point, srid=srid)
  35. def _to_pickle_wkb(self):
  36. return None if self.empty else super()._to_pickle_wkb()
  37. def _from_pickle_wkb(self, wkb):
  38. return self._create_empty() if wkb is None else super()._from_pickle_wkb(wkb)
  39. def _ogr_ptr(self):
  40. return gdal.geometries.Point._create_empty() if self.empty else super()._ogr_ptr()
  41. @classmethod
  42. def _create_empty(cls):
  43. return cls._create_point(None, None)
  44. @classmethod
  45. def _create_point(cls, ndim, coords):
  46. """
  47. Create a coordinate sequence, set X, Y, [Z], and create point
  48. """
  49. if not ndim:
  50. return capi.create_point(None)
  51. if ndim < 2 or ndim > 3:
  52. raise TypeError('Invalid point dimension: %s' % ndim)
  53. cs = capi.create_cs(c_uint(1), c_uint(ndim))
  54. i = iter(coords)
  55. capi.cs_setx(cs, 0, next(i))
  56. capi.cs_sety(cs, 0, next(i))
  57. if ndim == 3:
  58. capi.cs_setz(cs, 0, next(i))
  59. return capi.create_point(cs)
  60. def _set_list(self, length, items):
  61. ptr = self._create_point(length, items)
  62. if ptr:
  63. capi.destroy_geom(self.ptr)
  64. self._ptr = ptr
  65. self._post_init()
  66. else:
  67. # can this happen?
  68. raise GEOSException('Geometry resulting from slice deletion was invalid.')
  69. def _set_single(self, index, value):
  70. self._cs.setOrdinate(index, 0, value)
  71. def __iter__(self):
  72. "Iterate over coordinates of this Point."
  73. for i in range(len(self)):
  74. yield self[i]
  75. def __len__(self):
  76. "Return the number of dimensions for this Point (either 0, 2 or 3)."
  77. if self.empty:
  78. return 0
  79. if self.hasz:
  80. return 3
  81. else:
  82. return 2
  83. def _get_single_external(self, index):
  84. if index == 0:
  85. return self.x
  86. elif index == 1:
  87. return self.y
  88. elif index == 2:
  89. return self.z
  90. _get_single_internal = _get_single_external
  91. @property
  92. def x(self):
  93. "Return the X component of the Point."
  94. return self._cs.getOrdinate(0, 0)
  95. @x.setter
  96. def x(self, value):
  97. "Set the X component of the Point."
  98. self._cs.setOrdinate(0, 0, value)
  99. @property
  100. def y(self):
  101. "Return the Y component of the Point."
  102. return self._cs.getOrdinate(1, 0)
  103. @y.setter
  104. def y(self, value):
  105. "Set the Y component of the Point."
  106. self._cs.setOrdinate(1, 0, value)
  107. @property
  108. def z(self):
  109. "Return the Z component of the Point."
  110. return self._cs.getOrdinate(2, 0) if self.hasz else None
  111. @z.setter
  112. def z(self, value):
  113. "Set the Z component of the Point."
  114. if not self.hasz:
  115. raise GEOSException('Cannot set Z on 2D Point.')
  116. self._cs.setOrdinate(2, 0, value)
  117. # ### Tuple setting and retrieval routines. ###
  118. @property
  119. def tuple(self):
  120. "Return a tuple of the point."
  121. return self._cs.tuple
  122. @tuple.setter
  123. def tuple(self, tup):
  124. "Set the coordinates of the point with the given tuple."
  125. self._cs[0] = tup
  126. # The tuple and coords properties
  127. coords = tuple