From 927152cbaeb85adac8e048e71a218fba7ff86cf2 Mon Sep 17 00:00:00 2001 From: mdupays Date: Thu, 11 Jul 2024 14:01:57 +0200 Subject: [PATCH] research KNN from 2D points_skeleton NOT 3D --- .../stats/knn_distance.py | 25 +++++++++++-------- lidro/main_create_virtual_point.py | 2 +- test/stats/test_knn_distance.py | 2 +- test/vectors/test_las_around_point.py | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lidro/create_virtual_point/stats/knn_distance.py b/lidro/create_virtual_point/stats/knn_distance.py index fe6b3a46..7362bcbe 100644 --- a/lidro/create_virtual_point/stats/knn_distance.py +++ b/lidro/create_virtual_point/stats/knn_distance.py @@ -4,29 +4,34 @@ from sklearn.neighbors import NearestNeighbors -def find_k_nearest_neighbors(point_2d: np.array, points_3d: np.array, k: int) -> np.array: +def find_k_nearest_neighbors(points_skeleton: np.array, points_ground_3d: np.array, k: int) -> np.array: """Finds the K nearest neighbors of a given point from a list of 3D points Args: - point_2d (np.array): An array of 3D point [x, y, 0] - points_3D (np.array): An array of 3D points [x, y, z] - k (int): The number of nearest neighbors to find + points_skeleton (np.array): a single point representing the skeleton Hydro + an array of 2D point [x, y] + points_ground_3d (np.array): ground's points from LIDAR. + an array of 3D points [x, y, z] + k (int): the number of nearest neighbors to find Returns: - numpy.ndarray: An array of the K nearest 3D points [x, y, z] + numpy.ndarray: an array of the K nearest 3D points [x, y, z] """ - # Convert point_2d to nump.arrat - point_2d_array = np.array(point_2d).reshape(1, -1) + # Extract 2D coordinates (x, y) from the 3D points + points_ground_2d = points_ground_3d[:, :2] + + # Convert point_2d to nump.array + points_skeleton_array = np.array(points_skeleton).reshape(1, -1) # Initialize and fit the NearestNeighbors model - nbrs = NearestNeighbors(n_neighbors=k, algorithm="auto").fit(points_3d) + nbrs = NearestNeighbors(n_neighbors=k, algorithm="auto").fit(points_ground_2d) # Find the distances and indices of the nearest neighbors - indices = nbrs.kneighbors(point_2d_array, return_distance=False) + indices = nbrs.kneighbors(points_skeleton_array, return_distance=False) # Retrieve the points corresponding to the indices # Use indices to retrieve the closest 3D points - k_nearest_points = points_3d[indices[0]] + k_nearest_points = points_ground_3d[indices[0]] # = points_3d[indices.flatten()] return k_nearest_points diff --git a/lidro/main_create_virtual_point.py b/lidro/main_create_virtual_point.py index 0660afcd..349cab05 100644 --- a/lidro/main_create_virtual_point.py +++ b/lidro/main_create_virtual_point.py @@ -98,7 +98,7 @@ def extract_points_around_skeleton_points_one_tile(filename): points_skeleton_clip = clip_points_with_box(points_list_skeleton, bbox) # Create list with Skeleton's points with Z for step 4 points_skeleton_with_z_clip = [ - ([geom.x, geom.y, 0]) for geom in points_skeleton_clip.geometry if isinstance(geom, Point) + ([geom.x, geom.y]) for geom in points_skeleton_clip.geometry if isinstance(geom, Point) ] # Step 4 : Extract a Z elevation value along the hydrographic skeleton logging.info(f"\nExtract a Z elevation value along the hydrographic skeleton for tile : {tilename}") diff --git a/test/stats/test_knn_distance.py b/test/stats/test_knn_distance.py index 8f943eb6..32412591 100644 --- a/test/stats/test_knn_distance.py +++ b/test/stats/test_knn_distance.py @@ -12,7 +12,7 @@ def test_find_k_nearest_neighbors_default(): [830867.61, 6290202.62, 2.89], ] ) - point = np.array([[830574.89, 6290648.53, 0]]) + point = np.array([[830574.89, 6290648.53]]) k = 3 result = find_k_nearest_neighbors(point, points_array, k) diff --git a/test/vectors/test_las_around_point.py b/test/vectors/test_las_around_point.py index 083b0ea2..5cdfb756 100644 --- a/test/vectors/test_las_around_point.py +++ b/test/vectors/test_las_around_point.py @@ -22,7 +22,7 @@ def setup_module(module): def test_las_around_point_default(): # Parameters - points_along_skeleton = [[830864.5181373736, 6290217.943739296, 0], [830866.5957826116, 6290208.162525126, 0]] + points_along_skeleton = [[830864.5181373736, 6290217.943739296], [830866.5957826116, 6290208.162525126]] points_clip = np.array( [