basic.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Jul 31 13:06:20 2012
  4. @author: pietro
  5. """
  6. import ctypes
  7. import grass.lib.vector as libvect
  8. from collections import Iterable
  9. class Bbox(object):
  10. """Instantiate a Bounding Box class that contain
  11. a ctypes pointer to the C struct bound_box, that could be use
  12. by C GRASS functions. ::
  13. >>> bbox = Bbox()
  14. >>> bbox
  15. Bbox(0.0, 0.0, 0.0, 0.0)
  16. The default parameters are 0. It is possible to set or change
  17. the parameters later, with: ::
  18. >>> bbox.north = 10
  19. >>> bbox.south = -10
  20. >>> bbox.east = -20
  21. >>> bbox.west = 20
  22. >>> bbox
  23. Bbox(10.0, -10.0, -20.0, 20.0)
  24. Or directly istantiate the class with the values, with: ::
  25. >>> bbox = Bbox(north=100, south=0, east=0, west=100)
  26. >>> bbox
  27. Bbox(100.0, 0.0, 0.0, 100.0)
  28. ..
  29. """
  30. def __init__(self, north=0, south=0, east=0, west=0, top=0, bottom=0):
  31. self.c_bbox = ctypes.pointer(libvect.bound_box())
  32. self.north = north
  33. self.south = south
  34. self.east = east
  35. self.west = west
  36. self.top = top
  37. self.bottom = bottom
  38. def _get_n(self):
  39. return self.c_bbox.contents.N
  40. def _set_n(self, value):
  41. self.c_bbox.contents.N = value
  42. north = property(fget=_get_n, fset=_set_n)
  43. def _get_s(self):
  44. return self.c_bbox.contents.S
  45. def _set_s(self, value):
  46. self.c_bbox.contents.S = value
  47. south = property(fget=_get_s, fset=_set_s)
  48. def _get_e(self):
  49. return self.c_bbox.contents.E
  50. def _set_e(self, value):
  51. self.c_bbox.contents.E = value
  52. east = property(fget=_get_e, fset=_set_e)
  53. def _get_w(self):
  54. return self.c_bbox.contents.W
  55. def _set_w(self, value):
  56. self.c_bbox.contents.W = value
  57. west = property(fget=_get_w, fset=_set_w)
  58. def _get_t(self):
  59. return self.c_bbox.contents.T
  60. def _set_t(self, value):
  61. self.c_bbox.contents.T = value
  62. top = property(fget=_get_t, fset=_set_t)
  63. def _get_b(self):
  64. return self.c_bbox.contents.B
  65. def _set_b(self, value):
  66. self.c_bbox.contents.B = value
  67. bottom = property(fget=_get_b, fset=_set_b)
  68. def __repr__(self):
  69. return "Bbox({n}, {s}, {e}, {w})".format(n=self.north, s=self.south,
  70. e=self.east, w=self.west)
  71. class BoxList(object):
  72. def __init__(self, boxlist=None):
  73. self.c_boxlist = ctypes.pointer(libvect.boxlist())
  74. # if set to 0, the list will hold only ids and no boxes
  75. self.c_boxlist.contents.have_boxes = 1
  76. if boxlist is not None:
  77. for box in boxlist:
  78. self.append(box)
  79. def __len__(self):
  80. return self.c_boxlist.contents.n_values
  81. def __repr__(self):
  82. return "Boxlist([%s])" % ", ".join([repr(box)
  83. for box in self.__iter__()])
  84. def __getitem__(self, indx):
  85. bbox = Bbox()
  86. bbox.c_bbox = ctypes.pointer(self.c_boxlist.contents.box[indx])
  87. return bbox
  88. def __setitem__(self, indx, bbox):
  89. self.c_boxlist.cotents.box[indx] = bbox
  90. def __iter__(self):
  91. return (self.__getitem__(box_id) for box_id in xrange(self.__len__()))
  92. def __str__(self):
  93. return self.__repr__()
  94. def append(self, box):
  95. """Append a Bbox object to a Boxlist object, using the
  96. ``Vect_boxlist_append`` C fuction. ::
  97. >>> box0 = Bbox()
  98. >>> box1 = Bbox(1,2,3,4)
  99. >>> box2 = Bbox(5,6,7,8)
  100. >>> boxlist = BoxList([box0, box1])
  101. >>> boxlist
  102. Boxlist([Bbox(0.0, 0.0, 0.0, 0.0), Bbox(1.0, 2.0, 3.0, 4.0)])
  103. >>> len(boxlist)
  104. 2
  105. >>> boxlist.append(box2)
  106. >>> len(boxlist)
  107. 3
  108. ..
  109. """
  110. indx = self.__len__()
  111. libvect.Vect_boxlist_append(self.c_boxlist, indx, box.c_bbox.contents)
  112. # def extend(self, boxlist):
  113. # """Extend a boxlist with another boxlist or using a list of Bbox, using
  114. # ``Vect_boxlist_append_boxlist`` c function. ::
  115. #
  116. # >>> box0 = Bbox()
  117. # >>> box1 = Bbox(1,2,3,4)
  118. # >>> box2 = Bbox(5,6,7,8)
  119. # >>> box3 = Bbox(9,8,7,6)
  120. # >>> boxlist0 = BoxList([box0, box1])
  121. # >>> boxlist0
  122. # Boxlist([Bbox(0.0, 0.0, 0.0, 0.0), Bbox(1.0, 2.0, 3.0, 4.0)])
  123. # >>> boxlist1 = BoxList([box2, box3])
  124. # >>> len(boxlist0)
  125. # 2
  126. # >>> boxlist0.extend(boxlist1)
  127. # >>> len(boxlist0)
  128. # 4
  129. # >>> boxlist1.extend([box0, box1])
  130. # >>> len(boxlist1)
  131. # 4
  132. #
  133. # ..
  134. # """
  135. # if hasattr(boxlist, 'c_boxlist'):
  136. # #import pdb; pdb.set_trace()
  137. # # FIXME: doesn't work
  138. # libvect.Vect_boxlist_append_boxlist(self.c_boxlist,
  139. # boxlist.c_boxlist)
  140. # else:
  141. # for box in boxlist:
  142. # self.append(box)
  143. def remove(self, indx):
  144. """Remove Bbox from the boxlist, given an integer or a list of integer
  145. or a boxlist, using ``Vect_boxlist_delete`` C function or the
  146. ``Vect_boxlist_delete_boxlist``. ::
  147. >>> boxlist = BoxList([Bbox(),
  148. ... Bbox(1, 0, 0, 1),
  149. ... Bbox(1, -1, -1, 1)])
  150. >>> boxlist.remove(0)
  151. >>> boxlist
  152. Boxlist([Bbox(1.0, 0.0, 0.0, 1.0), Bbox(1.0, -1.0, -1.0, 1.0)])
  153. ..
  154. """
  155. if hasattr(indx, 'c_boxlist'):
  156. libvect.Vect_boxlist_delete_boxlist(self.c_boxlist, indx.c_boxlist)
  157. elif isinstance(indx, int):
  158. libvect.Vect_boxlist_delete(self.c_boxlist, indx)
  159. else:
  160. for ind in indx:
  161. libvect.Vect_boxlist_delete(self.c_boxlist, ind)
  162. def reset(self):
  163. """Reset the c_boxlist C struct, using the ``Vect_reset_boxlist`` C
  164. function. ::
  165. >>> boxlist = BoxList([Bbox(),
  166. ... Bbox(1, 0, 0, 1),
  167. ... Bbox(1, -1, -1, 1)])
  168. >>> len(boxlist)
  169. 3
  170. >>> boxlist.reset()
  171. >>> len(boxlist)
  172. 0
  173. ..
  174. """
  175. libvect.Vect_reset_boxlist(self.c_boxlist)
  176. class Ilist(object):
  177. """Instantiate a list of integer using the C GRASS struct ``ilist``,
  178. the class contains this struct as ``c_ilist`` attribute. """
  179. def __init__(self, integer_list=None):
  180. self.c_ilist = ctypes.pointer(libvect.struct_ilist())
  181. if integer_list is not None:
  182. self.extend(integer_list)
  183. def __getitem__(self, key):
  184. if isinstance(key, slice):
  185. #import pdb; pdb.set_trace()
  186. #Get the start, stop, and step from the slice
  187. return [self.c_ilist.contents.value[indx]
  188. for indx in xrange(*key.indices(len(self)))]
  189. elif isinstance(key, int):
  190. if key < 0: # Handle negative indices
  191. key += self.c_ilist.contents.n_values
  192. if key >= self.c_ilist.contents.n_values:
  193. raise IndexError('Index out of range')
  194. return self.c_ilist.contents.value[key]
  195. else:
  196. raise ValueError("Invalid argument type: %r." % key)
  197. def __setitem__(self, key, value):
  198. if self.contains(value):
  199. raise ValueError('Integer already in the list')
  200. self.c_ilist.contents.value[key] = int(value)
  201. def __len__(self):
  202. return self.c_ilist.contents.n_values
  203. def __iter__(self):
  204. return [self.c_ilist.contents.value[i] for i in xrange(self.__len__())]
  205. def __repr__(self):
  206. return "Ilist(%r)" % repr(self.__iter__())
  207. def append(self, value):
  208. """Append an integer to the list"""
  209. if libvect.Vect_list_append(self.c_ilist, value):
  210. raise # TODO
  211. def reset(self):
  212. """Reset the list"""
  213. libvect.Vect_reset_list(self.c_ilist)
  214. def extend(self, ilist):
  215. """Extend the list with another Ilist object or
  216. with a list of integers"""
  217. if isinstance(ilist, Ilist):
  218. libvect.Vect_list_append_list(self.c_ilist, ilist.ilist)
  219. else:
  220. for i in ilist:
  221. self.append(i)
  222. def remove(self, value):
  223. """Remove a value from a list"""
  224. if isinstance(value, int):
  225. libvect.Vect_list_delete(self.c_ilist, value)
  226. elif isinstance(value, Ilist):
  227. libvect.Vect_list_delete_list(self.c_ilist, value.ilist)
  228. elif isinstance(value, Iterable):
  229. for i in value:
  230. libvect.Vect_list_delete(self.c_ilist, int(i))
  231. else:
  232. raise ValueError('Value: %r, is not supported' % value)
  233. def contains(self, value):
  234. """Check if value is in the list"""
  235. return bool(libvect.Vect_val_in_list(self.c_ilist, value))
  236. class Cats(object):
  237. """Instantiate a Category class that contains a ctypes pointer
  238. to the Map_info C struct, and a ctypes pointer to that C cats struct,
  239. that could be used by C functions.
  240. """
  241. def __init__(self, c_mapinfo, v_id, c_cats=None):
  242. self.c_mapinfo = c_mapinfo
  243. self.id = v_id
  244. if c_cats is not None:
  245. self.c_cats = c_cats
  246. else:
  247. self.c_cats = ctypes.pointer(libvect.line_cats())
  248. self.get_area_cats()
  249. def get_area_cats(self):
  250. """Get area categories, set the c_cats struct given an area, using the
  251. ``Vect_get_area_cats`` function.
  252. """
  253. libvect.Vect_get_area_cats(self.c_mapinfo, self.id, self.c_cats)
  254. def reset(self):
  255. """Reset the C cats struct from previous values."""
  256. libvect.Vect_reset_cats(self.c_cats)