From c5f492934ecf37457799a49918015f9736e30a86 Mon Sep 17 00:00:00 2001 From: Jake Faulkner Date: Mon, 1 Jul 2024 16:13:44 +1200 Subject: [PATCH 1/3] add coordinate distance function --- qcore/coordinates.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/qcore/coordinates.py b/qcore/coordinates.py index f28626b1..e97f7a7e 100644 --- a/qcore/coordinates.py +++ b/qcore/coordinates.py @@ -84,3 +84,25 @@ def nztm_to_wgs_depth(nztm_coordinates: np.ndarray) -> np.ndarray: array([[-36.8509, 174.7645, 100], [-41.2924, 174.7787, 100]]) """ return np.array(_NZTM2WGS.transform(*nztm_coordinates.T)).T + + +def distance_between_wgs_depth_coordinates( + point_a: np.ndarray, point_b: np.ndarray +) -> float: + """Return the distance between two points in lat, lon, depth format. + + Valid only for points that can be converted into NZTM format. + + Parameters + ---------- + point_a : np.ndarray + The first point (lat, lon, depth). + point_b : np.ndarray + The second point (lat, lon, depth). + + Returns + ------- + float + The distance (in metres) between point_a and point_b. + """ + return np.linalg.norm(wgs_depth_to_nztm(point_a) - wgs_depth_to_nztm(point_b)) From a475894bd37d76557bbbc9641954f82b26390946 Mon Sep 17 00:00:00 2001 From: Jake Faulkner Date: Mon, 1 Jul 2024 16:27:47 +1200 Subject: [PATCH 2/3] vectorise for multiple points --- qcore/coordinates.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/qcore/coordinates.py b/qcore/coordinates.py index e97f7a7e..733baf46 100644 --- a/qcore/coordinates.py +++ b/qcore/coordinates.py @@ -88,7 +88,7 @@ def nztm_to_wgs_depth(nztm_coordinates: np.ndarray) -> np.ndarray: def distance_between_wgs_depth_coordinates( point_a: np.ndarray, point_b: np.ndarray -) -> float: +) -> Union[float, np.ndarray]: """Return the distance between two points in lat, lon, depth format. Valid only for points that can be converted into NZTM format. @@ -96,13 +96,21 @@ def distance_between_wgs_depth_coordinates( Parameters ---------- point_a : np.ndarray - The first point (lat, lon, depth). + The first point (lat, lon, optionally depth). May have shape + (2,), (3,), (n, 2), (n, 3). + point_b : np.ndarray - The second point (lat, lon, depth). + The second point (lat, lon, optionally depth). May have shape + (2, ), (3,), (n, 2), (n, 3) Returns ------- - float - The distance (in metres) between point_a and point_b. + float or np.ndarray + The distance (in metres) between point_a and point_b. Will + return an array of floats if input contains multiple points """ + if len(point_a.shape) > 1: + return np.linalg.norm( + wgs_depth_to_nztm(point_a) - wgs_depth_to_nztm(point_b), axis=0 + ) return np.linalg.norm(wgs_depth_to_nztm(point_a) - wgs_depth_to_nztm(point_b)) From 30c014de75d72df3d87468322dcc62928ef3c5b8 Mon Sep 17 00:00:00 2001 From: Jake Faulkner Date: Wed, 3 Jul 2024 10:10:14 +1200 Subject: [PATCH 3/3] fix type-signatures and fix vectorisation --- qcore/coordinates.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/qcore/coordinates.py b/qcore/coordinates.py index 733baf46..f3f586b2 100644 --- a/qcore/coordinates.py +++ b/qcore/coordinates.py @@ -17,6 +17,8 @@ [0]: https://www.linz.govt.nz/guidance/geodetic-system/coordinate-systems-used-new-zealand/projections/new-zealand-transverse-mercator-2000-nztm2000 """ +from typing import Union + import numpy as np import pyproj @@ -111,6 +113,6 @@ def distance_between_wgs_depth_coordinates( """ if len(point_a.shape) > 1: return np.linalg.norm( - wgs_depth_to_nztm(point_a) - wgs_depth_to_nztm(point_b), axis=0 + wgs_depth_to_nztm(point_a) - wgs_depth_to_nztm(point_b), axis=1 ) return np.linalg.norm(wgs_depth_to_nztm(point_a) - wgs_depth_to_nztm(point_b))