def test_empty(dtype): order, quadtree = cuspatial.quadtree_on_points( cudf.Series([], dtype=dtype), # x cudf.Series([], dtype=dtype), # y *bbox_1, # bbox 1, # scale 1, # max_depth 1, # min_size ) poly_bboxes = cuspatial.polygon_bounding_boxes( cudf.Series(), cudf.Series(), cudf.Series([], dtype=dtype), cudf.Series([], dtype=dtype), ) # empty should not throw intersections = cuspatial.join_quadtree_and_bounding_boxes( quadtree, poly_bboxes, *bbox_1, 1, 1, # bbox # scale # max_depth ) cudf.testing.assert_frame_equal( intersections, cudf.DataFrame({ "poly_offset": cudf.Series([], dtype=np.uint32), "quad_offset": cudf.Series([], dtype=np.uint32), }), )
def _spatialJoinDist(self, ldf, rdf, lName, rName, lTree, polygon, dist): (polyOffset, ringOffset, xPoints, yPoints) = polygon (points, tree) = lTree boundingBox = cuspatial.polygon_bounding_boxes(polyOffset, ringOffset, xPoints, yPoints) joinFilter = cuspatial.join_quadtree_and_bounding_boxes( tree, boundingBox, 0.0, 1.0, 0.0, 1.0, 1.0, 15) joinPolygon = cuspatial.quadtree_point_in_polygon( joinFilter, tree, points, ldf[lName + 'X'], ldf[lName + 'Y'], polyOffset, ringOffset, xPoints, yPoints, ) # https://github.com/rapidsai/cuspatial/issues/284 lGather = ldf.take(points.take( joinPolygon['point_index'])).reset_index(drop=True) rGather = rdf.take(joinPolygon['polygon_index']).reset_index(drop=True) dfConcat = cudf.concat([lGather, rGather], axis=1) dfConcat['distPred'] = False @cuda.jit def distPredFunc(lX, lY, rX, rY, out, dist): i = cuda.grid(1) if i < lX.shape[0]: dX = lX[i] - rX[i] dY = lY[i] - rY[i] dSquare = (dX * dX) + (dY * dY) out[i] = dSquare < (dist * dist) numbaTime = 0.0 if dist > 0.0: startTime = time.time() distPredFunc.forall(dfConcat.shape[0])(dfConcat[lName + 'X'], dfConcat[lName + 'Y'], dfConcat[rName + 'X'], dfConcat[rName + 'Y'], dfConcat['distPred'], dist) endTime = time.time() numbaTime = endTime - startTime dfConcat = dfConcat[dfConcat['distPred']] return (dfConcat, numbaTime)
def test_polygon_join_small(dtype): x_min = 0 x_max = 8 y_min = 0 y_max = 8 scale = 1 max_depth = 3 min_size = 12 points_x = small_points_x.astype(dtype) points_y = small_points_y.astype(dtype) poly_points_x = small_poly_xs.astype(dtype) poly_points_y = small_poly_ys.astype(dtype) point_indices, quadtree = cuspatial.quadtree_on_points( points_x, points_y, x_min, x_max, y_min, y_max, scale, max_depth, min_size, ) poly_bboxes = cuspatial.polygon_bounding_boxes( small_poly_offsets, small_ring_offsets, poly_points_x, poly_points_y, ) intersections = cuspatial.join_quadtree_and_bounding_boxes( quadtree, poly_bboxes, x_min, x_max, y_min, y_max, scale, max_depth, ) cudf.testing.assert_frame_equal( intersections, cudf.DataFrame({ "poly_offset": cudf.Series([3, 3, 1, 2, 1, 1, 0, 3], dtype=np.uint32), "quad_offset": cudf.Series([10, 11, 6, 6, 12, 13, 2, 2], dtype=np.uint32), }), )
def test_polygon_bounding_boxes_one(dtype): result = cuspatial.polygon_bounding_boxes( cudf.Series([0]), cudf.Series([0]), cudf.Series([2.488450, 1.333584, 3.460720], dtype=dtype), cudf.Series([5.856625, 5.008840, 4.586599], dtype=dtype), ) assert_eq( result, cudf.DataFrame({ "x_min": cudf.Series([1.333584], dtype=dtype), "y_min": cudf.Series([4.586599], dtype=dtype), "x_max": cudf.Series([3.460720], dtype=dtype), "y_max": cudf.Series([5.856625], dtype=dtype), }), )
def test_polygon_bounding_boxes_empty(dtype): result = cuspatial.polygon_bounding_boxes( cudf.Series(), cudf.Series(), cudf.Series([], dtype=dtype), cudf.Series([], dtype=dtype), ) assert_eq( result, cudf.DataFrame({ "x_min": cudf.Series([], dtype=dtype), "y_min": cudf.Series([], dtype=dtype), "x_max": cudf.Series([], dtype=dtype), "y_max": cudf.Series([], dtype=dtype), }), )
def get_updated_df_quadtree_pip(lat, lon, nodes_df): min_x, min_y, max_x, max_y = (nodes_df["x"].min(), nodes_df["y"].min(), nodes_df["x"].max(), nodes_df["y"].max()) max_depth = 6 min_size = 50 scale = max(max_x - min_x, max_y - min_y) // (1 << max_depth) point_indices, quadtree = cuspatial.quadtree_on_points( nodes_df.x, nodes_df.y, min_x, max_x, min_y, max_y, scale, max_depth, min_size, ) poly_offsets, ring_offsets = cudf.Series([0], index=["selection"]),[0] poly_bboxes = cuspatial.polygon_bounding_boxes( poly_offsets, ring_offsets, lat, lon, ) intersections = cuspatial.join_quadtree_and_bounding_boxes( quadtree, poly_bboxes, min_x, max_x, min_y, max_y, scale, max_depth, ) polygons_and_points = cuspatial.quadtree_point_in_polygon( intersections, quadtree, point_indices, nodes_df.x, nodes_df.y, poly_offsets, ring_offsets, lat, lon, ) return nodes_df.loc[polygons_and_points.point_index]
def test_quadtree_point_in_polygon_small(dtype): x_min = 0 x_max = 8 y_min = 0 y_max = 8 scale = 1 max_depth = 3 min_size = 12 points_x = small_points_x.astype(dtype) points_y = small_points_y.astype(dtype) poly_points_x = small_poly_xs.astype(dtype) poly_points_y = small_poly_ys.astype(dtype) point_indices, quadtree = cuspatial.quadtree_on_points( points_x, points_y, x_min, x_max, y_min, y_max, scale, max_depth, min_size, ) poly_bboxes = cuspatial.polygon_bounding_boxes( small_poly_offsets, small_ring_offsets, poly_points_x, poly_points_y, ) intersections = cuspatial.join_quadtree_and_bounding_boxes( quadtree, poly_bboxes, x_min, x_max, y_min, y_max, scale, max_depth, ) polygons_and_points = cuspatial.quadtree_point_in_polygon( intersections, quadtree, point_indices, points_x, points_y, small_poly_offsets, small_ring_offsets, poly_points_x, poly_points_y, ) cudf.testing.assert_frame_equal( polygons_and_points, cudf.DataFrame({ "polygon_index": cudf.Series( [3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 3], dtype=np.uint32, ), "point_index": cudf.Series( [ 28, 29, 30, 31, 32, 33, 34, 35, 45, 46, 47, 48, 49, 50, 51, 52, 54, 62, 60, ], dtype=np.uint32, ), }), )
def test_polygon_bounding_boxes_small(dtype): result = cuspatial.polygon_bounding_boxes( cudf.Series([0, 1, 2, 3]), cudf.Series([0, 3, 8, 12]), cudf.Series( [ # ring 1 2.488450, 1.333584, 3.460720, # ring 2 5.039823, 5.561707, 7.103516, 7.190674, 5.998939, # ring 3 5.998939, 5.573720, 6.703534, 5.998939, # ring 4 2.088115, 1.034892, 2.415080, 3.208660, 2.088115, ], dtype=dtype, ), cudf.Series( [ # ring 1 5.856625, 5.008840, 4.586599, # ring 2 4.229242, 1.825073, 1.503906, 4.025879, 5.653384, # ring 3 1.235638, 0.197808, 0.086693, 1.235638, # ring 4 4.541529, 3.530299, 2.896937, 3.745936, 4.541529, ], dtype=dtype, ), ) assert_eq( result, cudf.DataFrame({ "x_min": cudf.Series( [ 1.3335840000000001, 5.0398230000000002, 5.5737199999999998, 1.0348919999999999, ], dtype=dtype, ), "y_min": cudf.Series( [ 4.5865989999999996, 1.503906, 0.086693000000000006, 2.8969369999999999, ], dtype=dtype, ), "x_max": cudf.Series( [ 3.4607199999999998, 7.1906739999999996, 6.7035340000000003, 3.2086600000000001, ], dtype=dtype, ), "y_max": cudf.Series( [ 5.8566250000000002, 5.653384, 1.235638, 4.5415289999999997, ], dtype=dtype, ), }), )