Example #1
0
    def geos(self, bbox, type='all', bbox_list=False):
        """Find the geometry features contained in the bbox.
        Vect_select_lines_by_box

        Valid type are all the keys in find.vtype dictionary
        """
        found = BoxList()
        if libvect.Vect_select_lines_by_box(self.c_mapinfo, bbox.c_bbox,
                                            self.vtype[type], found.c_boxlist):
            if bbox_list:
                return found
            else:
                return (read_line(f_id, self.c_mapinfo, self.table,
                                  self.writable) for f_id in found.ids)
Example #2
0
def GetNearestNodeCat(e, n, layer, tresh, vectMap):

    if not haveCtypes:
        return -2

    vectMapName, mapSet = ParseMapStr(vectMap)

    openedMap = pointer(vectlib.Map_info())
    ret = vectlib.Vect_open_old(openedMap,
                                c_char_p(encode(vectMapName)),
                                c_char_p(encode(mapSet)))
    if ret == 1:
        vectlib.Vect_close(openedMap)
    if ret != 2:
        return -1

    nodeNum = vectlib.Vect_find_node(openedMap,
                                     c_double(e),
                                     c_double(n),
                                     c_double(0),
                                     c_double(tresh),
                                     vectlib.WITHOUT_Z)

    if nodeNum > 0:
        e = c_double(0)
        n = c_double(0)
        vectlib.Vect_get_node_coor(openedMap,
                                   nodeNum,
                                   byref(e),
                                   byref(n),
                                   None)  # z
        e = e.value
        n = n.value
    else:
        vectlib.Vect_close(openedMap)
        return -1

    box = vectlib.bound_box()
    List = POINTER(vectlib.boxlist)
    List = vectlib.Vect_new_boxlist(c_int(0))

    box.E = box.W = e
    box.N = box.S = n
    box.T = box.B = 0
    vectlib.Vect_select_lines_by_box(
        openedMap, byref(box),
        vectlib.GV_POINT, List)

    found = 0
    dcost = 0

    Cats = POINTER(vectlib.line_cats)
    Cats = vectlib.Vect_new_cats_struct()

    cat = c_int(0)

    for j in range(List.contents.n_values):
        line = List.contents.id[j]
        type = vectlib.Vect_read_line(openedMap, None, Cats, line)
        if type != vectlib.GV_POINT:
            continue

        if vectlib.Vect_cat_get(Cats, c_int(layer), byref(cat)):
            found = 1
            break
    if found:
        return cat.value

    return -1
Example #3
0
    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)]),
         Centroid(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
                )