find.py 24 KB

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