find.py 24 KB

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