Пример #1
0
def SnapToNode(e, n, tresh, vectMap):
    """Find nearest node to click coordinates (within given threshold)"""
    if not haveCtypes:
        return None

    vectMap, mapSet = ParseMapStr(vectMap)

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

    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 False

    return e, n
Пример #2
0
    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,
            )
Пример #3
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