def cat(self, cat_id, vtype, layer=None, generator=False, geo=None): """Return the geometry features with category == cat_id. :param cat_id: the category number :type cat_id: int :param vtype: the type of geometry feature that we are looking for :type vtype: str :param layer: the layer number that will be used :type layer: int :param generator: if True return a generator otherwise it return a list of features :type generator: bool """ if geo is None and vtype not in _GEOOBJ: keys = "', '".join(sorted(_GEOOBJ.keys())) raise ValueError("vtype not supported, use one of: '%s'" % keys) Obj = _GEOOBJ[vtype] if geo is None else geo ilist = Ilist() libvect.Vect_cidx_find_all(self.c_mapinfo, layer if layer else self.layer, Obj.gtype, cat_id, ilist.c_ilist) is2D = not self.is_3D() if generator: return (read_line(feature_id=v_id, c_mapinfo=self.c_mapinfo, table=self.table, writeable=self.writeable, is2D=is2D) for v_id in ilist) else: return [read_line(feature_id=v_id, c_mapinfo=self.c_mapinfo, table=self.table, writeable=self.writeable, is2D=is2D) for v_id in ilist]
def geos(self, point, maxdist, type='all', exclude=None): """Find the nearest line. Vect_find_line_list Valid type are all the keys in find.vtype dictionary """ 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.writable) for f_id in found ] else: return []
def nodes(self, bbox): """Find the nearest area. Vect_find_area""" found = Ilist() if libvect.Vect_select_nodes_by_box(self.c_mapinfo, bbox.c_bbox, found.c_ilist): for n_id in found: yield Node(v_id=n_id, c_mapinfo=self.c_mapinfo, table=self.table, writable=self.writable)
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 )
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 [Centroid(2.500000, 2.500000), Centroid(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 ]