Distance within radius from a point (Lat,lon)

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)

sqrt()

7 Comments

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.
Excuse me , but you do not understand what I mean. Well I have an ascii file, with two columns. The first has Latitude and the second has longtitude. Also I have one point (lets call it "MYPOINT") with spesific latitde and longtitude. I would like with the base of MYPOINT to find all the point in the radious/cirle distance of 20km (For example from the 100 points, the points that are located at a radial distance of 20m from the MYPOINT.
Which functions should I use?
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.
I have write the following code:
clc
clear
filename1= 'ascii.xlsx'
[d1,tex]= xlsread(filename1);
lat=d1(:,2)
lon=d1(:,1)
filename2= 'MYPOINT.xlsx'
[d2,tex]= xlsread(filename2);
lat1=d2(:,2)
lon1=d2(:,1)
I am attaching the files.
I am not sure how I could input your solution in my code ?
Thank you in advance
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);
Thank you. But I have one last question. I should convert degrees to km right?
I used to use the following function:
DISTANCE=(deg2km(distance(lat1,lon1,lat,lon)));
How could I input the above function in your solution?
Yes, you need to convert. I don't have the deg2km() function so you'll just have to read the help on it.

Sign in to comment.

Asked:

on 19 Dec 2020

Commented:

on 1 Jan 2021

Community Treasure Hunt

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

Start Hunting!