# How to search closest coordinate values in a Lat Long Z data

1 view (last 30 days)
Mustafa Alper Cetintas on 23 Jul 2021
Commented: Image Analyst on 23 Jul 2021
Hello all,
I have a realtime Lat,Long and depth array data stream as the antenna platform moves around the interested area. Then I need to find every intersecting coordinate couples from the position data while it is moving. There will not always be overlapping points, in this case, I need to find Lat-Long points which are close to each other as much as possible. Could not find the suitable MATLAB function for this operation, or can I ask is there a specific function to do this process ?. I would be appreciated for any suggestion for this operation.
Thank you for you time for this.

KSSV on 23 Jul 2021

Scott MacKenzie on 23 Jul 2021
Edited: Scott MacKenzie on 23 Jul 2021
You can do this with the pdist2 function. For latitude and longitude, usually the haversine method is used. Google it if you want the details. Unfortunately, haversine is not provided as a distance option in pdist2. But, the function is easy to write and can be passed in as a handle to pdist2.
Here's an example using the latitudes and longitudes for five Canadian Cities. You'll need to do some post processing to determine "closeness" between cities (or antenna platforms as noted in your question).
% Toronto, Victoria, Regina, Winnepeg, Quebec City (city hall)
lat = [43.653214 48.429986 50.442545 49.872654 46.811837];
lon = [-79.384051 -123.368147 -104.61133 -97.163108 -71.208415];
M = [lat' lon'];
n = numel(lat);
% build triangle in D for distances between cities
D = NaN(n);
for i=1:n
D(i,i+1:end) = pdist2(M(i,:), M((i+1):end,:), @haversine);
end
D
D = 5×5
1.0e+03 * NaN 3.3883 2.0427 1.5154 0.7297 NaN NaN 1.3708 1.9028 3.8369 NaN NaN NaN 0.5342 2.4664 NaN NaN NaN NaN 1.9381 NaN NaN NaN NaN NaN
function [dist] = haversine(ZI, ZJ)
lat1 = ZI(1,1);
lon1 = ZI(1,2);
lat2 = ZJ(:,1);
lon2 = ZJ(:,2);
dist = 2 * 6371 * asin(sqrt(sind((lat2-lat1)/2)^2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1)/2)^2));
end
Image Analyst on 23 Jul 2021
If
1. all you want is to know the closest city and
2. there are just a few cities that are not at really similar distances and
3. don't really care about whether the found distance is super accurate in terms of great circle distance,
then you probably don't have to worry about haversine, though it looks simple enough to include.

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!