Skip to content

Commit

Permalink
research KNN from 2D points_skeleton NOT 3D
Browse files Browse the repository at this point in the history
  • Loading branch information
mdupaysign committed Jul 11, 2024
1 parent fca39eb commit 927152c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
25 changes: 15 additions & 10 deletions lidro/create_virtual_point/stats/knn_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lidro/main_create_virtual_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion test/stats/test_knn_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/vectors/test_las_around_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down

0 comments on commit 927152c

Please sign in to comment.