find.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Mar 19 11:09:30 2013
  4. @author: pietro
  5. """
  6. import grass.lib.vector as libvect
  7. from grass.pygrass.errors import must_be_open
  8. from grass.pygrass.vector.basic import Ilist, BoxList
  9. from grass.pygrass.vector.geometry import read_line, Isle, Area, Point, Node
  10. # For test purposes
  11. test_vector_name = "find_doctest_map"
  12. class AbstractFinder(object):
  13. def __init__(self, c_mapinfo, table=None, writeable=False):
  14. """Abstract finder
  15. -----------------
  16. Find geometry feature around a point.
  17. """
  18. self.c_mapinfo = c_mapinfo
  19. self.table = table
  20. self.writeable = writeable
  21. self.vtype = {'point': libvect.GV_POINT, # 1
  22. 'line': libvect.GV_LINE, # 2
  23. 'boundary': libvect.GV_BOUNDARY, # 3
  24. 'centroid': libvect.GV_CENTROID, # 4
  25. 'all': -1}
  26. def is_open(self):
  27. """Check if the vector map is open or not"""
  28. from . import abstract
  29. return abstract.is_open(self.c_mapinfo)
  30. class PointFinder(AbstractFinder):
  31. """Point finder
  32. This class provides an interface to search geometry features
  33. of a vector map that are close to a point. The PointFinder class
  34. is part of a topological vector map object.
  35. """
  36. def __init__(self, c_mapinfo, table=None, writeable=False):
  37. """Find geometry feature(s) around a point.
  38. :param c_mapinfo: Pointer to the vector layer mapinfo structure
  39. :type c_mapinfo: ctypes pointer to mapinfo structure
  40. :param table: Attribute table of the vector layer
  41. :type table: Class Table from grass.pygrass.table
  42. :param writable: True or False
  43. :type writeable: boolean
  44. """
  45. super(PointFinder, self).__init__(c_mapinfo, table, writeable)
  46. @must_be_open
  47. def node(self, point, maxdist):
  48. """Find the nearest node around a specific point.
  49. :param point: The point to search
  50. :type point: grass.pygrass.vector.geometry.Point
  51. :param maxdist: The maximum search distance around the point
  52. :type maxdist: float
  53. :return: A grass.pygrass.vector.geometry.Node if found or None
  54. This methods uses libvect.Vect_find_node()()
  55. Examples:
  56. >>> from grass.pygrass.vector import VectorTopo
  57. >>> from grass.pygrass.vector.geometry import Point
  58. >>> test_vect = VectorTopo(test_vector_name)
  59. >>> test_vect.open('r')
  60. # Find nearest node
  61. >>> points = (Point(10,0), Point(10,4), Point(14,0))
  62. >>> result = []
  63. >>> for point in points:
  64. ... f = test_vect.find_by_point.node(point=point, maxdist=1)
  65. ... if f:
  66. ... result.append(f)
  67. >>> result
  68. [Node(2), Node(1), Node(6)]
  69. >>> test_vect.find_by_point.node(point=Point(20,20), maxdist=0)
  70. >>> test_vect.close()
  71. """
  72. node_id = libvect.Vect_find_node(self.c_mapinfo, point.x,
  73. point.y,
  74. point.z if point.z else 0,
  75. float(maxdist),
  76. int(not point.is2D))
  77. if node_id:
  78. return Node(v_id=node_id, c_mapinfo=self.c_mapinfo,
  79. table=self.table, writeable=self.writeable)
  80. @must_be_open
  81. def geo(self, point, maxdist, type='all', exclude=0):
  82. """Find the nearest vector feature around a specific point.
  83. :param point: The point to search
  84. :type point: grass.pygrass.vector.geometry.Point
  85. :param maxdist: The maximum search distance around the point
  86. :type maxdist: float
  87. :param type: The type of feature to search for
  88. Valid type are all the keys in find.vtype dictionary
  89. :type type: string
  90. :param exclude: if > 0 number of lines which should be
  91. excluded from selection
  92. :return: A grass.pygrass.vector.geometry.Node if found or None
  93. This methods uses libvect.Vect_find_line()()
  94. Examples:
  95. >>> from grass.pygrass.vector import VectorTopo
  96. >>> from grass.pygrass.vector.geometry import Point
  97. >>> test_vect = VectorTopo(test_vector_name)
  98. >>> test_vect.open('r')
  99. # Find single features
  100. >>> points = (Point(10,0), Point(10,6), Point(14,2))
  101. >>> result = []
  102. >>> for point in points:
  103. ... f = test_vect.find_by_point.geo(point=point, maxdist=1)
  104. ... if f:
  105. ... result.append(f)
  106. >>> for f in result:
  107. ... print(f.to_wkt_p()) #doctest: +NORMALIZE_WHITESPACE
  108. LINESTRING(10.000000 4.000000,
  109. 10.000000 2.000000,
  110. 10.000000 0.000000)
  111. POINT(10.000000 6.000000)
  112. LINESTRING(14.000000 4.000000,
  113. 14.000000 2.000000,
  114. 14.000000 0.000000)
  115. >>> test_vect.find_by_point.geo(point=Point(20,20), maxdist=0)
  116. >>> test_vect.close()
  117. """
  118. feature_id = libvect.Vect_find_line(self.c_mapinfo,
  119. point.x, point.y,
  120. point.z if point.z else 0,
  121. self.vtype[type], float(maxdist),
  122. int(not point.is2D), exclude)
  123. if feature_id:
  124. return read_line(feature_id, self.c_mapinfo,
  125. self.table, self.writeable)
  126. @must_be_open
  127. def geos(self, point, maxdist, type='all', exclude=None):
  128. """Find the nearest vector features around a specific point.
  129. :param point: The point to search
  130. :type point: grass.pygrass.vector.geometry.Point
  131. :param maxdist: The maximum search distance around the point
  132. :type maxdist: float
  133. :param type: The type of feature to search for
  134. Valid type are all the keys in find.vtype dictionary
  135. :type type: string
  136. :param exclude: if > 0 number of lines which should be
  137. excluded from selection
  138. :return: A list of grass.pygrass.vector.geometry
  139. (Line, Point, Boundary, Centroid) if found or None
  140. This methods uses libvect.Vect_find_line_list()()
  141. Examples:
  142. >>> from grass.pygrass.vector import VectorTopo
  143. >>> from grass.pygrass.vector.geometry import Point
  144. >>> test_vect = VectorTopo(test_vector_name)
  145. >>> test_vect.open('r')
  146. # Find multiple features
  147. >>> points = (Point(10,0), Point(10,5), Point(14,2))
  148. >>> result = []
  149. >>> for point in points:
  150. ... f = test_vect.find_by_point.geos(point=point,
  151. ... maxdist=1.5)
  152. ... if f:
  153. ... result.append(f)
  154. >>> for f in result:
  155. ... print(f) #doctest: +NORMALIZE_WHITESPACE
  156. [Line([Point(10.000000, 4.000000),
  157. Point(10.000000, 2.000000),
  158. Point(10.000000, 0.000000)])]
  159. [Line([Point(10.000000, 4.000000),
  160. Point(10.000000, 2.000000),
  161. Point(10.000000, 0.000000)]),
  162. Point(10.000000, 6.000000)]
  163. [Line([Point(14.000000, 4.000000),
  164. Point(14.000000, 2.000000),
  165. Point(14.000000, 0.000000)])]
  166. # Find multiple boundaries
  167. >>> point = Point(3,3)
  168. >>> result = test_vect.find_by_point.geos(point=Point(3,3),
  169. ... type="boundary",
  170. ... maxdist=1.5)
  171. >>> result #doctest: +NORMALIZE_WHITESPACE
  172. [Boundary([Point(0.000000, 4.000000), Point(4.000000, 4.000000)]),
  173. Boundary([Point(4.000000, 4.000000), Point(4.000000, 0.000000)]),
  174. Boundary([Point(1.000000, 1.000000), Point(1.000000, 3.000000),
  175. Point(3.000000, 3.000000), Point(3.000000, 1.000000),
  176. Point(1.000000, 1.000000)]),
  177. Boundary([Point(4.000000, 4.000000), Point(6.000000, 4.000000)])]
  178. # Find multiple centroids
  179. >>> point = Point(3,3)
  180. >>> result = test_vect.find_by_point.geos(point=Point(3,3),
  181. ... type="centroid",
  182. ... maxdist=1.5)
  183. >>> result #doctest: +NORMALIZE_WHITESPACE
  184. [Centroid(2.500000, 2.500000),
  185. Centroid(3.500000, 3.500000)]
  186. >>> test_vect.find_by_point.geos(point=Point(20,20), maxdist=0)
  187. >>> test_vect.close()
  188. """
  189. excl = Ilist(exclude) if exclude else Ilist([])
  190. found = Ilist()
  191. if libvect.Vect_find_line_list(self.c_mapinfo,
  192. point.x, point.y,
  193. point.z if point.z else 0,
  194. self.vtype[type], float(maxdist),
  195. int(not point.is2D),
  196. excl.c_ilist, found.c_ilist):
  197. return [read_line(f_id, self.c_mapinfo, self.table, self.writeable)
  198. for f_id in found]
  199. @must_be_open
  200. def area(self, point):
  201. """Find the nearest area around a specific point.
  202. :param point: The point to search
  203. :type point: grass.pygrass.vector.geometry.Point
  204. :return: A grass.pygrass.vector.geometry.Area if found or None
  205. This methods uses libvect.Vect_find_area()
  206. Examples:
  207. >>> from grass.pygrass.vector import VectorTopo
  208. >>> from grass.pygrass.vector.geometry import Point
  209. >>> test_vect = VectorTopo(test_vector_name)
  210. >>> test_vect.open('r')
  211. # Find AREAS
  212. >>> points = (Point(0.5,0.5), Point(5,1), Point(7,1))
  213. >>> result = []
  214. >>> for point in points:
  215. ... area = test_vect.find_by_point.area(point)
  216. ... result.append(area)
  217. >>> result
  218. [Area(1), Area(2), Area(4)]
  219. >>> for area in result:
  220. ... print(area.to_wkt()) #doctest: +NORMALIZE_WHITESPACE
  221. POLYGON ((0.0000000000000000 0.0000000000000000,
  222. 0.0000000000000000 4.0000000000000000,
  223. 0.0000000000000000 4.0000000000000000,
  224. 4.0000000000000000 4.0000000000000000,
  225. 4.0000000000000000 4.0000000000000000,
  226. 4.0000000000000000 0.0000000000000000,
  227. 4.0000000000000000 0.0000000000000000,
  228. 0.0000000000000000 0.0000000000000000),
  229. (1.0000000000000000 1.0000000000000000,
  230. 3.0000000000000000 1.0000000000000000,
  231. 3.0000000000000000 3.0000000000000000,
  232. 1.0000000000000000 3.0000000000000000,
  233. 1.0000000000000000 1.0000000000000000))
  234. POLYGON ((4.0000000000000000 0.0000000000000000,
  235. 4.0000000000000000 4.0000000000000000,
  236. 4.0000000000000000 4.0000000000000000,
  237. 6.0000000000000000 4.0000000000000000,
  238. 6.0000000000000000 4.0000000000000000,
  239. 6.0000000000000000 0.0000000000000000,
  240. 6.0000000000000000 0.0000000000000000,
  241. 4.0000000000000000 0.0000000000000000))
  242. POLYGON ((6.0000000000000000 0.0000000000000000,
  243. 6.0000000000000000 4.0000000000000000,
  244. 6.0000000000000000 4.0000000000000000,
  245. 8.0000000000000000 4.0000000000000000,
  246. 8.0000000000000000 4.0000000000000000,
  247. 8.0000000000000000 0.0000000000000000,
  248. 8.0000000000000000 0.0000000000000000,
  249. 6.0000000000000000 0.0000000000000000))
  250. >>> test_vect.find_by_point.area(Point(20,20))
  251. >>> test_vect.close()
  252. """
  253. area_id = libvect.Vect_find_area(self.c_mapinfo, point.x, point.y)
  254. if area_id:
  255. return Area(v_id=area_id, c_mapinfo=self.c_mapinfo,
  256. table=self.table, writeable=self.writeable)
  257. @must_be_open
  258. def island(self, point):
  259. """Find the nearest island around a specific point.
  260. :param point: The point to search
  261. :type point: grass.pygrass.vector.geometry.Point
  262. :return: A grass.pygrass.vector.geometry.Isle if found or None
  263. This methods uses Vect_find_island.Vect_find_area()
  264. Examples:
  265. >>> from grass.pygrass.vector import VectorTopo
  266. >>> from grass.pygrass.vector.geometry import Point
  267. >>> test_vect = VectorTopo(test_vector_name)
  268. >>> test_vect.open('r')
  269. # Find ISLANDS
  270. >>> points = (Point(2,2), Point(5,1))
  271. >>> result = []
  272. >>> for point in points:
  273. ... area = test_vect.find_by_point.island(point)
  274. ... result.append(area)
  275. >>> result
  276. [Isle(2), Isle(1)]
  277. >>> for isle in result:
  278. ... print(isle.to_wkt()) #doctest: +NORMALIZE_WHITESPACE
  279. Polygon((1.000000 1.000000, 3.000000 1.000000,
  280. 3.000000 3.000000, 1.000000 3.000000, 1.000000 1.000000))
  281. Polygon((0.000000 4.000000, 0.000000 0.000000, 4.000000 0.000000,
  282. 6.000000 0.000000, 8.000000 0.000000, 8.000000 4.000000,
  283. 6.000000 4.000000, 4.000000 4.000000, 0.000000 4.000000))
  284. >>> test_vect.find_by_point.island(Point(20,20))
  285. >>> test_vect.close()
  286. """
  287. isle_id = libvect.Vect_find_island(self.c_mapinfo, point.x, point.y)
  288. if isle_id:
  289. return Isle(v_id=isle_id, c_mapinfo=self.c_mapinfo,
  290. table=self.table, writeable=self.writeable)
  291. class BboxFinder(AbstractFinder):
  292. """Bounding Box finder
  293. This class provides an interface to search geometry features
  294. of a vector map that are inside or intersect a boundingbox.
  295. The BboxFinder class is part of a topological vector map object.
  296. """
  297. def __init__(self, c_mapinfo, table=None, writeable=False):
  298. """Find geometry feature(s)that are insider or intersect
  299. with a boundingbox.
  300. :param c_mapinfo: Pointer to the vector layer mapinfo structure
  301. :type c_mapinfo: ctypes pointer to mapinfo structure
  302. :param table: Attribute table of the vector layer
  303. :type table: Class Table from grass.pygrass.table
  304. :param writable: True or False
  305. :type writeable: boolean
  306. """
  307. super(BboxFinder, self).__init__(c_mapinfo, table, writeable)
  308. @must_be_open
  309. def geos(self, bbox, type='all', bboxlist_only=False):
  310. """Find vector features inside a boundingbox.
  311. :param bbox: The boundingbox to search in
  312. :type bbox: grass.pygrass.vector.basic.Bbox
  313. :param type: The type of feature to search for
  314. Valid type are all the keys in find.vtype dictionary
  315. :type type: string
  316. :param bboxlist_only: If true the BoxList will be returned,
  317. no features are generated
  318. :type bboxlist_only: boolean
  319. :return: A list of grass.pygrass.vector.geometry
  320. (Line, Point, Boundary, Centroid) if found,
  321. or None if nothing was found.
  322. If bboxlist_only is True a BoxList
  323. object will be returned, or None if nothing was found.
  324. This methods uses libvect.Vect_select_lines_by_box()
  325. Examples:
  326. >>> from grass.pygrass.vector import VectorTopo
  327. >>> from grass.pygrass.vector.basic import Bbox
  328. >>> test_vect = VectorTopo(test_vector_name)
  329. >>> test_vect.open('r')
  330. >>> bbox = Bbox(north=5, south=-1, east=3, west=-1)
  331. >>> result = test_vect.find_by_bbox.geos(bbox=bbox)
  332. >>> [bbox for bbox in result] #doctest: +NORMALIZE_WHITESPACE
  333. [Boundary([Point(4.000000, 0.000000), Point(0.000000, 0.000000)]),
  334. Boundary([Point(0.000000, 0.000000), Point(0.000000, 4.000000)]),
  335. Boundary([Point(0.000000, 4.000000), Point(4.000000, 4.000000)]),
  336. Boundary([Point(1.000000, 1.000000), Point(1.000000, 3.000000),
  337. Point(3.000000, 3.000000), Point(3.000000, 1.000000),
  338. Point(1.000000, 1.000000)]),
  339. Centroid(2.500000, 2.500000)]
  340. >>> bbox = Bbox(north=5, south=-1, east=3, west=-1)
  341. >>> result = test_vect.find_by_bbox.geos(bbox=bbox,
  342. ... bboxlist_only=True)
  343. >>> result #doctest: +NORMALIZE_WHITESPACE
  344. Boxlist([Bbox(0.0, 0.0, 4.0, 0.0),
  345. Bbox(4.0, 0.0, 0.0, 0.0),
  346. Bbox(4.0, 4.0, 4.0, 0.0),
  347. Bbox(3.0, 1.0, 3.0, 1.0),
  348. Bbox(2.5, 2.5, 2.5, 2.5)])
  349. >>> bbox = Bbox(north=7, south=-1, east=15, west=9)
  350. >>> result = test_vect.find_by_bbox.geos(bbox=bbox)
  351. >>> [bbox for bbox in result] #doctest: +NORMALIZE_WHITESPACE
  352. [Line([Point(10.000000, 4.000000), Point(10.000000, 2.000000),
  353. Point(10.000000, 0.000000)]),
  354. Point(10.000000, 6.000000),
  355. Line([Point(12.000000, 4.000000), Point(12.000000, 2.000000),
  356. Point(12.000000, 0.000000)]),
  357. Point(12.000000, 6.000000),
  358. Line([Point(14.000000, 4.000000), Point(14.000000, 2.000000),
  359. Point(14.000000, 0.000000)]),
  360. Point(14.000000, 6.000000)]
  361. >>> bbox = Bbox(north=20, south=18, east=20, west=18)
  362. >>> test_vect.find_by_bbox.geos(bbox=bbox)
  363. >>> bbox = Bbox(north=20, south=18, east=20, west=18)
  364. >>> test_vect.find_by_bbox.geos(bbox=bbox, bboxlist_only=True)
  365. >>> test_vect.close()
  366. """
  367. found = BoxList()
  368. if libvect.Vect_select_lines_by_box(self.c_mapinfo, bbox.c_bbox,
  369. self.vtype[type], found.c_boxlist):
  370. if bboxlist_only:
  371. return found
  372. else:
  373. return (read_line(f_id, self.c_mapinfo, self.table,
  374. self.writeable) for f_id in found.ids)
  375. @must_be_open
  376. def nodes(self, bbox):
  377. """Find nodes inside a boundingbox.
  378. :param bbox: The boundingbox to search in
  379. :type bbox: grass.pygrass.vector.basic.Bbox
  380. :return: A list of nodes or None if nothing was found
  381. This methods uses libvect.Vect_select_nodes_by_box()
  382. Examples:
  383. >>> from grass.pygrass.vector import VectorTopo
  384. >>> from grass.pygrass.vector.basic import Bbox
  385. >>> test_vect = VectorTopo(test_vector_name)
  386. >>> test_vect.open('r')
  387. # Find nodes in box
  388. >>> bbox = Bbox(north=5, south=-1, east=15, west=9)
  389. >>> result = test_vect.find_by_bbox.nodes(bbox=bbox)
  390. >>> [node for node in result]
  391. [Node(2), Node(1), Node(4), Node(3), Node(5), Node(6)]
  392. >>> bbox = Bbox(north=20, south=18, east=20, west=18)
  393. >>> test_vect.find_by_bbox.nodes(bbox=bbox)
  394. >>> test_vect.close()
  395. """
  396. found = Ilist()
  397. if libvect.Vect_select_nodes_by_box(self.c_mapinfo, bbox.c_bbox,
  398. found.c_ilist):
  399. if len(found) > 0:
  400. return (Node(v_id=n_id, c_mapinfo=self.c_mapinfo,
  401. table=self.table, writeable=self.writeable)
  402. for n_id in found)
  403. @must_be_open
  404. def areas(self, bbox, boxlist=None, bboxlist_only=False):
  405. """Find areas inside a boundingbox.
  406. :param bbox: The boundingbox to search in
  407. :type bbox: grass.pygrass.vector.basic.Bbox
  408. :param boxlist: An existing BoxList to be filled with
  409. :type_boxlist: grass.pygrass.vector.basic.BoxList
  410. :param bboxlist_only: If true the BoxList will be returned,
  411. no features are generated
  412. :type bboxlist_only: boolean
  413. :return: A list of areas or None if nothing was found
  414. This methods uses libvect.Vect_select_areas_by_box()
  415. Examples:
  416. >>> from grass.pygrass.vector import VectorTopo
  417. >>> from grass.pygrass.vector.basic import Bbox
  418. >>> test_vect = VectorTopo(test_vector_name)
  419. >>> test_vect.open('r')
  420. # Find areas in box
  421. >>> bbox = Bbox(north=5, south=-1, east=9, west=-1)
  422. >>> result = test_vect.find_by_bbox.areas(bbox=bbox)
  423. >>> [area for area in result]
  424. [Area(1), Area(2), Area(3), Area(4)]
  425. >>> bbox = Bbox(north=5, south=-1, east=9, west=-1)
  426. >>> result = test_vect.find_by_bbox.areas(bbox=bbox,
  427. ... bboxlist_only=True)
  428. >>> result #doctest: +NORMALIZE_WHITESPACE
  429. Boxlist([Bbox(4.0, 0.0, 4.0, 0.0),
  430. Bbox(4.0, 0.0, 6.0, 4.0),
  431. Bbox(3.0, 1.0, 3.0, 1.0),
  432. Bbox(4.0, 0.0, 8.0, 6.0)])
  433. >>> bbox = Bbox(north=20, south=18, east=20, west=18)
  434. >>> test_vect.find_by_bbox.areas(bbox=bbox)
  435. >>> test_vect.find_by_bbox.areas(bbox=bbox,
  436. ... bboxlist_only=True)
  437. >>> test_vect.close()
  438. """
  439. boxlist = boxlist if boxlist else BoxList()
  440. if libvect.Vect_select_areas_by_box(self.c_mapinfo, bbox.c_bbox,
  441. boxlist.c_boxlist):
  442. if bboxlist_only:
  443. return boxlist
  444. else:
  445. return (Area(v_id=a_id, c_mapinfo=self.c_mapinfo,
  446. table=self.table, writeable=self.writeable)
  447. for a_id in boxlist.ids)
  448. @must_be_open
  449. def islands(self, bbox, bboxlist_only=False):
  450. """Find isles inside a boundingbox.
  451. :param bbox: The boundingbox to search in
  452. :type bbox: grass.pygrass.vector.basic.Bbox
  453. :param bboxlist_only: If true the BoxList will be returned,
  454. no features are generated
  455. :type bboxlist_only: boolean
  456. :return: A list of isles or None if nothing was found
  457. This methods uses libvect.Vect_select_isles_by_box()
  458. Examples:
  459. >>> from grass.pygrass.vector import VectorTopo
  460. >>> from grass.pygrass.vector.basic import Bbox
  461. >>> test_vect = VectorTopo(test_vector_name)
  462. >>> test_vect.open('r')
  463. # Find isles in box
  464. >>> bbox = Bbox(north=5, south=-1, east=9, west=-1)
  465. >>> result = test_vect.find_by_bbox.islands(bbox=bbox)
  466. >>> [isle for isle in result]
  467. [Isle(1), Isle(2)]
  468. >>> bbox = Bbox(north=5, south=-1, east=9, west=-1)
  469. >>> result = test_vect.find_by_bbox.islands(bbox=bbox,
  470. ... bboxlist_only=True)
  471. >>> result #doctest: +NORMALIZE_WHITESPACE
  472. Boxlist([Bbox(4.0, 0.0, 8.0, 0.0),
  473. Bbox(3.0, 1.0, 3.0, 1.0)])
  474. >>> bbox = Bbox(north=20, south=18, east=20, west=18)
  475. >>> test_vect.find_by_bbox.islands(bbox=bbox)
  476. >>> test_vect.find_by_bbox.islands(bbox=bbox,
  477. ... bboxlist_only=True)
  478. >>> test_vect.close()
  479. """
  480. found = BoxList()
  481. if libvect.Vect_select_isles_by_box(self.c_mapinfo, bbox.c_bbox,
  482. found.c_boxlist):
  483. if bboxlist_only:
  484. return found
  485. else:
  486. return (Isle(v_id=i_id, c_mapinfo=self.c_mapinfo,
  487. table=self.table, writeable=self.writeable)
  488. for i_id in found.ids)
  489. class PolygonFinder(AbstractFinder):
  490. def __init__(self, c_mapinfo, table=None, writeable=False):
  491. super(PolygonFinder, self).__init__(c_mapinfo, table, writeable)
  492. def lines(self, polygon, isles=None):
  493. pass
  494. def areas(self, polygon, isles=None):
  495. pass
  496. if __name__ == "__main__":
  497. import doctest
  498. from grass.pygrass import utils
  499. utils.create_test_vector_map(test_vector_name)
  500. doctest.testmod()
  501. """Remove the generated vector map, if exist"""
  502. from grass.pygrass.utils import get_mapset_vector
  503. from grass.script.core import run_command
  504. mset = get_mapset_vector(test_vector_name, mapset='')
  505. if mset:
  506. run_command("g.remove", flags='f', type='vector', name=test_vector_name)