Development of an internal social media platform with personalised dashboards for students
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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 _ogr_ptr(self):
  36. return gdal.geometries.Point._create_empty() if self.empty else super()._ogr_ptr()
  37. @classmethod
  38. def _create_empty(cls):
  39. return cls._create_point(None, None)
  40. @classmethod
  41. def _create_point(cls, ndim, coords):
  42. """
  43. Create a coordinate sequence, set X, Y, [Z], and create point
  44. """
  45. if not ndim:
  46. return capi.create_point(None)
  47. if ndim < 2 or ndim > 3:
  48. raise TypeError('Invalid point dimension: %s' % ndim)
  49. cs = capi.create_cs(c_uint(1), c_uint(ndim))
  50. i = iter(coords)
  51. capi.cs_setx(cs, 0, next(i))
  52. capi.cs_sety(cs, 0, next(i))
  53. if ndim == 3:
  54. capi.cs_setz(cs, 0, next(i))
  55. return capi.create_point(cs)
  56. def _set_list(self, length, items):
  57. ptr = self._create_point(length, items)
  58. if ptr:
  59. capi.destroy_geom(self.ptr)
  60. self._ptr = ptr
  61. self._post_init()
  62. else:
  63. # can this happen?
  64. raise GEOSException('Geometry resulting from slice deletion was invalid.')
  65. def _set_single(self, index, value):
  66. self._cs.setOrdinate(index, 0, value)
  67. def __iter__(self):
  68. "Iterate over coordinates of this Point."
  69. for i in range(len(self)):
  70. yield self[i]
  71. def __len__(self):
  72. "Return the number of dimensions for this Point (either 0, 2 or 3)."
  73. if self.empty:
  74. return 0
  75. if self.hasz:
  76. return 3
  77. else:
  78. return 2
  79. def _get_single_external(self, index):
  80. if index == 0:
  81. return self.x
  82. elif index == 1:
  83. return self.y
  84. elif index == 2:
  85. return self.z
  86. _get_single_internal = _get_single_external
  87. @property
  88. def x(self):
  89. "Return the X component of the Point."
  90. return self._cs.getOrdinate(0, 0)
  91. @x.setter
  92. def x(self, value):
  93. "Set the X component of the Point."
  94. self._cs.setOrdinate(0, 0, value)
  95. @property
  96. def y(self):
  97. "Return the Y component of the Point."
  98. return self._cs.getOrdinate(1, 0)
  99. @y.setter
  100. def y(self, value):
  101. "Set the Y component of the Point."
  102. self._cs.setOrdinate(1, 0, value)
  103. @property
  104. def z(self):
  105. "Return the Z component of the Point."
  106. return self._cs.getOrdinate(2, 0) if self.hasz else None
  107. @z.setter
  108. def z(self, value):
  109. "Set the Z component of the Point."
  110. if not self.hasz:
  111. raise GEOSException('Cannot set Z on 2D Point.')
  112. self._cs.setOrdinate(2, 0, value)
  113. # ### Tuple setting and retrieval routines. ###
  114. @property
  115. def tuple(self):
  116. "Return a tuple of the point."
  117. return self._cs.tuple
  118. @tuple.setter
  119. def tuple(self, tup):
  120. "Set the coordinates of the point with the given tuple."
  121. self._cs[0] = tup
  122. # The tuple and coords properties
  123. coords = tuple