Exemplo n.º 1
0
def directed_hausdorff_distance(x, y, count):
    """ Compute the directed Hausdorff distances between all pairs of
    trajectories.

    params
    x: x coordinates
    y: y coordinates
    count: size of each trajectory

    Parameters
    ----------
    {params}

    Example
    -------
    The directed Hausdorff distance from one trajectory to another is the
    greatest of all the distances from a point in the first trajectory to
    the closest point in the second.
    [Wikipedia](https://en.wikipedia.org/wiki/Hausdorff_distance)

    Consider a pair of lines on a grid.

     |
     o
    -oxx-
     |
    o = [[0, 0], [0, 1]]
    x = [[1, 0], [2, 0]]

    o[0] is the closest point in o to x. The distance from o[0] to the farthest
    point in x = 2.
    x[0] is the closest point in x to o. The distance from x[0] to the farthest
    point in o = 1.414.

        result = cuspatial.directed_hausdorff_distance(
            cudf.Series([0, 1, 0, 0]),
            cudf.Series([0, 0, 1, 2]),
            cudf.Series([2, 2,]),
        )
        print(result)
             0         1
        0  0.0  1.414214
        1  2.0  0.000000

    Returns
    -------
    DataFrame: The pairwise directed distance matrix with one row and one
    column per input trajectory; the value at row i, column j represents the
    hausdorff distance from trajectory i to trajectory j.
    """
    result = cpp_directed_hausdorff_distance(x, y, count)
    dim = len(count)
    return DataFrame.from_gpu_matrix(
        result.data.to_gpu_array().reshape(dim, dim)
    )
Exemplo n.º 2
0
 def _check_input(self, X, is_categories=False):
     """
     If input is cupy, convert it to a DataFrame with 0 copies
     """
     if isinstance(X, cp.ndarray):
         self._set_input_type('array')
         if is_categories:
             X = X.transpose()
         return DataFrame.from_gpu_matrix(X)
     else:
         self._set_input_type('df')
         return X
Exemplo n.º 3
0
def directed_hausdorff_distance(xs, ys, points_per_space):
    """Compute the directed Hausdorff distances between all pairs of
    spaces.

    Parameters
    ----------
    xs
        column of x-coordinates
    ys
        column of y-coordinates
    points_per_space
        number of points in each space

    Returns
    -------
    result : cudf.DataFrame
        The pairwise directed distance matrix with one row and one
        column per input space; the value at row i, column j represents the
        hausdorff distance from space i to space j.

    Examples
    --------
    The directed Hausdorff distance from one space to another is the greatest
    of all the distances between any point in the first space to the closest
    point in the second.

    `Wikipedia <https://en.wikipedia.org/wiki/Hausdorff_distance>`_

    Consider a pair of lines on a grid::

             :
             x
        -----xyy---
             :
             :

    x\\ :sub:`0` = (0, 0), x\\ :sub:`1` = (0, 1)

    y\\ :sub:`0` = (1, 0), y\\ :sub:`1` = (2, 0)

    x\\ :sub:`0` is the closest point in ``x`` to ``y``. The distance from
    x\\ :sub:`0` to the farthest point in ``y`` is 2.

    y\\ :sub:`0` is the closest point in ``y`` to ``x``. The distance from
    y\\ :sub:`0` to the farthest point in ``x`` is 1.414.

    Compute the directed hausdorff distances between a set of spaces

    >>> result = cuspatial.directed_hausdorff_distance(
            [0, 1, 0, 0], # xs
            [0, 0, 1, 2], # ys
            [2,    2],    # points_per_space
        )
    >>> print(result)
             0         1
        0  0.0  1.414214
        1  2.0  0.000000
    """

    num_spaces = len(points_per_space)
    if num_spaces == 0:
        return DataFrame()
    xs, ys = normalize_point_columns(as_column(xs), as_column(ys))
    result = cpp_directed_hausdorff_distance(
        xs, ys, as_column(points_per_space, dtype="int32"),
    )
    result = result.data_array_view
    result = result.reshape(num_spaces, num_spaces)
    return DataFrame.from_gpu_matrix(result)
Exemplo n.º 4
0
def point_in_polygon_bitmap(
    x_points,
    y_points,
    polygon_ids,
    polygon_end_indices,
    polygons_x,
    polygons_y,
):
    """ Compute from a set of points and a set of polygons which points fall
    within which polygons. Note that `polygons_(x,y)` must be specified as
    closed polygons: the first and last coordinate of each polygon must be
    the same.

    params
    x_points: x coordinates of points to test
    y_points: y coordinates of points to test
    polygon_ids: a unique integer id for each polygon
    polygon_end_indices: the (n+1)th vertex of the final coordinate of each
                         polygon in the next parameters
    polygons_x: x closed coordinates of all polygon points
    polygons_y: y closed coordinates of all polygon points

    Parameters
    ----------
    {params}

    Examples
    --------
        result = cuspatial.point_in_polygon_bitmap(
            cudf.Series([0, -8, 6.0]), # x coordinates of 3 query points
            cudf.Series([0, -8, 6.0]), # y coordinates of 3 query points
            cudf.Series([1, 2]), # unique id of two polygons
            cudf.Series([5, 10]), # position of last vertex in each polygon
            # polygon coordinates, x and y. Note [-10, -10] and [0, 0] repeat
            # the start/end coordinate of the two polygons.
            cudf.Series([-10.0, 5, 5, -10, -10, 0, 10, 10, 0, 0]),
            cudf.Series([-10.0, -10, 5, 5, -10, 0, 0, 10, 10, 0]),
        )
        # The result of point_in_polygon_bitmap is a DataFrame of Boolean
        # values indicating whether each point (rows) falls within
        # each polygon (columns).
        print(result)
                   in_polygon_1  in_polygon_2
        0          True          True
        1          True         False
        2         False          True

        # Point 0: (0, 0) falls in both polygons
        # Point 1: (-8, -8) falls in the first polygon
        # Point 2: (6.0, 6.0) falls in the second polygon

    returns
    DataFrame: a DataFrame of Boolean values indicating whether each point
    falls within each polygon.
    """
    bitmap_result = cpp_point_in_polygon_bitmap(
        x_points,
        y_points,
        polygon_ids,
        polygon_end_indices,
        polygons_x,
        polygons_y,
    )

    result_binary = gis_utils.pip_bitmap_column_to_binary_array(
        polygon_bitmap_column=bitmap_result, width=len(polygon_ids)
    )
    result_bools = DataFrame.from_gpu_matrix(
        result_binary
    )._apply_support_method("astype", dtype="bool")
    result_bools.columns = [
        f"in_polygon_{x}" for x in list(reversed(polygon_ids))
    ]
    result_bools = result_bools[list(reversed(result_bools.columns))]
    return result_bools
Exemplo n.º 5
0
def point_in_polygon(
    test_points_x,
    test_points_y,
    poly_offsets,
    poly_ring_offsets,
    poly_points_x,
    poly_points_y,
):
    """ Compute from a set of points and a set of polygons which points fall
    within which polygons. Note that `polygons_(x,y)` must be specified as
    closed polygons: the first and last coordinate of each polygon must be
    the same.

    Parameters
    ----------
    test_points_x
        x-coordinate of test points
    test_points_y
        y-coordinate of test points
    poly_offsets
        beginning index of the first ring in each polygon
    poly_ring_offsets
        beginning index of the first point in each ring
    poly_points_x
        x closed-coordinate of polygon points
    poly_points_y
        y closed-coordinate of polygon points

    Examples
    --------

    Test whether 3 points fall within either of two polygons

    >>> result = cuspatial.point_in_polygon(
        [0, -8, 6.0],                             # test_points_x
        [0, -8, 6.0],                             # test_points_y
        cudf.Series([0, 1], index=['nyc', 'hudson river']), # poly_offsets
        [0, 3],                                   # ring_offsets
        [-10, 5, 5, -10, 0, 10, 10, 0],           # poly_points_x
        [-10, -10, 5, 5, 0, 0, 10, 10],           # poly_points_y
    )
    # The result of point_in_polygon is a DataFrame of Boolean
    # values indicating whether each point (rows) falls within
    # each polygon (columns).
    >>> print(result)
                nyc            hudson river
    0          True          True
    1          True         False
    2         False          True
    # Point 0: (0, 0) falls in both polygons
    # Point 1: (-8, -8) falls in the first polygon
    # Point 2: (6.0, 6.0) falls in the second polygon

    note
    input Series x and y will not be index aligned, but computed as
    sequential arrays.

    Returns
    -------
    result : cudf.DataFrame
        A DataFrame of boolean values indicating whether each point falls
        within each polygon.
    """

    if len(poly_offsets) == 0:
        return DataFrame()

    (
        test_points_x,
        test_points_y,
        poly_points_x,
        poly_points_y,
    ) = normalize_point_columns(
        as_column(test_points_x),
        as_column(test_points_y),
        as_column(poly_points_x),
        as_column(poly_points_y),
    )

    result = cpp_point_in_polygon(
        test_points_x,
        test_points_y,
        as_column(poly_offsets, dtype="int32"),
        as_column(poly_ring_offsets, dtype="int32"),
        poly_points_x,
        poly_points_y,
    )

    result = gis_utils.pip_bitmap_column_to_binary_array(
        polygon_bitmap_column=result, width=len(poly_offsets)
    )
    result = DataFrame.from_gpu_matrix(result)
    result = result._apply_support_method("astype", dtype="bool")
    result.columns = [x for x in list(reversed(poly_offsets.index))]
    result = result[list(reversed(result.columns))]
    return result