Distance within radius from a point (Lat,lon)
Show older comments
I have a question.
I have a point with a specific latitude anfd longtitude. From this point I would like to find different points within the radius of 20 km.
Which command should I use?
Answers (1)
Image Analyst
on 19 Dec 2020
0 votes
sqrt()
7 Comments
Image Analyst
on 23 Dec 2020
Ivan, did that work? Of course you have to consider what points you're looking at. Do you have an array of points that you need to check if they're within 20 km? Because you know that there are an infinite number of points unless you quantize them into a finite set of coordinates. I mean you don't want the latitude and longitude of every point in a 20 km radius, like if the points are only nanometers apart. You'd have trillions of trillions of points. Do you have the Mapping Toolbox? There may be some functions in there to help.
Ivan Mich
on 1 Jan 2021
Image Analyst
on 1 Jan 2021
Edited: Image Analyst
on 1 Jan 2021
sqrt(). It's the square root which is used to compute the Euclidean distance using the Pythagorean theorem. For example, if you have a 2-by-1 vector MYPOINT, and a list of a bunch of other points in an 100-by-2 matrix, called otherPoints, you can do
distances = sqrt((MYPOINT(:, 1) - otherPoints).^2 + (MYPOINT(:, 2) - otherPoints) .^ 2);
% Find row indexes where the distance is less than 20 km.
within20km = distances <= 20;
% Extract only those close points from the set of all points.
closePoints = otherPoints(within20km, :);
% Plot them.
plot(closePoints(:, 1), closePoints(:, 2), 'ro', 'MarkerSize', 20);
grid on;
Does that explain how to use sqrt() better? If not, attach your test file with your coordinates and the code where you converted latitude and longitude into (x,y) coordinates with units of km. Note: I do not have the Mapping Toolbox, if you're requiring that.
Ivan Mich
on 1 Jan 2021
Image Analyst
on 1 Jan 2021
Ivan, try this. Note that all your points are within 20 km. Perhaps you haven't converted degrees into km yet.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Read in multiple points.
filename1 = fullfile(pwd, 'ascii.xlsx');
[d1,tex] = xlsread(filename1);
lat = d1(:,2)
lon = d1(:,1)
% Read in single point.
filename2 = fullfile(pwd, 'MYPOINT.xlsx');
[d2,tex] = xlsread(filename2);
lat1 = d2(:,2)
lon1 = d2(:,1)
% Plot a circle around MYPOINT
radius = 20;
centerX = lat1;
centerY = lon1;
lightGreen = [.9, 1, .9];
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*2],...
'Curvature',[1,1],...
'FaceColor',lightGreen);
axis equal;
% Plot the multiple points.
hold on;
plot(lat, lon, 'b.', 'MarkerSize', 20);
grid on;
xlabel('x = latitude', 'FontSize', fontSize);
ylabel('y = longitude', 'FontSize', fontSize);
caption = sprintf('%d points in blue, 1 point in red', size(lat, 1));
title(caption, 'FontSize', fontSize);
% Plot the single MYPOINT.
xline(lat1, 'Color', 'r', 'LineWidth', 2);
yline(lon1, 'Color', 'r', 'LineWidth', 2);
plot(lat1, lon1, 'r.', 'LineWidth', 2, 'MarkerSize', 50);
% Compute distances.
distances = sqrt((lat - lat1).^2 + (lon - lon1) .^ 2);
% Find row indexes where the distance is less than 20 km.
within20km = distances <= 20;
% Extract only those close points from the set of all points.
closePoints = [lat(within20km), lon(within20km)]
% Plot them.
plot(lat(:, 1), closePoints(:, 2), 'ro', 'MarkerSize', 20);
grid on;
fprintf('Done running %s.m.\n', mfilename);

Ivan Mich
on 1 Jan 2021
Image Analyst
on 1 Jan 2021
Yes, you need to convert. I don't have the deg2km() function so you'll just have to read the help on it.
Categories
Find more on Coordinate Reference Systems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!