PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/geos/coordseq.py

https://code.google.com/p/mango-py/
Python | 156 lines | 114 code | 23 blank | 19 comment | 20 complexity | 96bd6157c51f2800c4c81f4772dbe3e5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. This module houses the GEOSCoordSeq object, which is used internally
  3. by GEOSGeometry to house the actual coordinates of the Point,
  4. LineString, and LinearRing geometries.
  5. """
  6. from ctypes import c_double, c_uint, byref
  7. from django.contrib.gis.geos.base import GEOSBase, numpy
  8. from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
  9. from django.contrib.gis.geos.libgeos import CS_PTR
  10. from django.contrib.gis.geos import prototypes as capi
  11. class GEOSCoordSeq(GEOSBase):
  12. "The internal representation of a list of coordinates inside a Geometry."
  13. ptr_type = CS_PTR
  14. #### Python 'magic' routines ####
  15. def __init__(self, ptr, z=False):
  16. "Initializes from a GEOS pointer."
  17. if not isinstance(ptr, CS_PTR):
  18. raise TypeError('Coordinate sequence should initialize with a CS_PTR.')
  19. self._ptr = ptr
  20. self._z = z
  21. def __iter__(self):
  22. "Iterates over each point in the coordinate sequence."
  23. for i in xrange(self.size):
  24. yield self[i]
  25. def __len__(self):
  26. "Returns the number of points in the coordinate sequence."
  27. return int(self.size)
  28. def __str__(self):
  29. "Returns the string representation of the coordinate sequence."
  30. return str(self.tuple)
  31. def __getitem__(self, index):
  32. "Returns the coordinate sequence value at the given index."
  33. coords = [self.getX(index), self.getY(index)]
  34. if self.dims == 3 and self._z:
  35. coords.append(self.getZ(index))
  36. return tuple(coords)
  37. def __setitem__(self, index, value):
  38. "Sets the coordinate sequence value at the given index."
  39. # Checking the input value
  40. if isinstance(value, (list, tuple)):
  41. pass
  42. elif numpy and isinstance(value, numpy.ndarray):
  43. pass
  44. else:
  45. raise TypeError('Must set coordinate with a sequence (list, tuple, or numpy array).')
  46. # Checking the dims of the input
  47. if self.dims == 3 and self._z:
  48. n_args = 3
  49. set_3d = True
  50. else:
  51. n_args = 2
  52. set_3d = False
  53. if len(value) != n_args:
  54. raise TypeError('Dimension of value does not match.')
  55. # Setting the X, Y, Z
  56. self.setX(index, value[0])
  57. self.setY(index, value[1])
  58. if set_3d: self.setZ(index, value[2])
  59. #### Internal Routines ####
  60. def _checkindex(self, index):
  61. "Checks the given index."
  62. sz = self.size
  63. if (sz < 1) or (index < 0) or (index >= sz):
  64. raise GEOSIndexError('invalid GEOS Geometry index: %s' % str(index))
  65. def _checkdim(self, dim):
  66. "Checks the given dimension."
  67. if dim < 0 or dim > 2:
  68. raise GEOSException('invalid ordinate dimension "%d"' % dim)
  69. #### Ordinate getting and setting routines ####
  70. def getOrdinate(self, dimension, index):
  71. "Returns the value for the given dimension and index."
  72. self._checkindex(index)
  73. self._checkdim(dimension)
  74. return capi.cs_getordinate(self.ptr, index, dimension, byref(c_double()))
  75. def setOrdinate(self, dimension, index, value):
  76. "Sets the value for the given dimension and index."
  77. self._checkindex(index)
  78. self._checkdim(dimension)
  79. capi.cs_setordinate(self.ptr, index, dimension, value)
  80. def getX(self, index):
  81. "Get the X value at the index."
  82. return self.getOrdinate(0, index)
  83. def setX(self, index, value):
  84. "Set X with the value at the given index."
  85. self.setOrdinate(0, index, value)
  86. def getY(self, index):
  87. "Get the Y value at the given index."
  88. return self.getOrdinate(1, index)
  89. def setY(self, index, value):
  90. "Set Y with the value at the given index."
  91. self.setOrdinate(1, index, value)
  92. def getZ(self, index):
  93. "Get Z with the value at the given index."
  94. return self.getOrdinate(2, index)
  95. def setZ(self, index, value):
  96. "Set Z with the value at the given index."
  97. self.setOrdinate(2, index, value)
  98. ### Dimensions ###
  99. @property
  100. def size(self):
  101. "Returns the size of this coordinate sequence."
  102. return capi.cs_getsize(self.ptr, byref(c_uint()))
  103. @property
  104. def dims(self):
  105. "Returns the dimensions of this coordinate sequence."
  106. return capi.cs_getdims(self.ptr, byref(c_uint()))
  107. @property
  108. def hasz(self):
  109. """
  110. Returns whether this coordinate sequence is 3D. This property value is
  111. inherited from the parent Geometry.
  112. """
  113. return self._z
  114. ### Other Methods ###
  115. def clone(self):
  116. "Clones this coordinate sequence."
  117. return GEOSCoordSeq(capi.cs_clone(self.ptr), self.hasz)
  118. @property
  119. def kml(self):
  120. "Returns the KML representation for the coordinates."
  121. # Getting the substitution string depending on whether the coordinates have
  122. # a Z dimension.
  123. if self.hasz: substr = '%s,%s,%s '
  124. else: substr = '%s,%s,0 '
  125. return '<coordinates>%s</coordinates>' % \
  126. ''.join([substr % self[i] for i in xrange(len(self))]).strip()
  127. @property
  128. def tuple(self):
  129. "Returns a tuple version of this coordinate sequence."
  130. n = self.size
  131. if n == 1: return self[0]
  132. else: return tuple([self[i] for i in xrange(n)])