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