.csv file read and computation of content of file

I have two .csv file which contains latitude and longitude in both the files. I need to read those values and calculate Haversine formula and plot.

1 comentario

Manoj Pai
Manoj Pai el 9 de Mayo de 2017
Editada: Walter Roberson el 9 de Mayo de 2017
lat1=15.850388;
lon1=74.498652;
lat2=15.850254;
lon2=74.498539;
lat3=15.850404;
lon3=74.498500;
R = 6371; % Earth's radius in km
delta_lat = lat2 - lat1; % difference in latitude
%fprintf(' %d \n',delta_lat);
delta_lon = lon2 - lon1; % difference in longitude
%fprintf(' %d \n',delta_lon);
a = sin(delta_lat/2)* sin(delta_lat/2) + cos(lat1) * cos(lat2) * sin(delta_lon/2)* sin(delta_lon/2);
c = 2 * atan2(sqrt(a), sqrt(1-a));
km = R * c;
fprintf(' %d \n',km);
delta_lat1 = lat3 lat2;
delta_lon1 = lon3 lon2;
a1 = sin(delta_lat1/2)* sin(delta_lat1/2) + cos(lat2) * cos(lat3) * sin(delta_lon1/2)* sin(delta_lon1/2);
c1 = 2 * atan2(sqrt(a1), sqrt(1-a1));
km1 = R * c1;
fprintf(' %d \n',km1);

Iniciar sesión para comentar.

 Respuesta aceptada

latlong1 = csvread('FirstFile.csv');
latlong2 = csvread('SecondFile.csv');
lat1 = latlong1(:,1);
lon1 = latlong1(:,2);
lat2 = latlong2(:,1);
lon2 = latlong2(:,2);
R = 6371; % Earth's radius in km
delta_lat = lat2 - lat1; % difference in latitude
%fprintf(' %d \n',delta_lat);
delta_lon = lon2 - lon1; % difference in longitude
%fprintf(' %d \n',delta_lon);
a = sin(delta_lat/2) .* sin(delta_lat/2) + cos(lat1) .* cos(lat2) .* sin(delta_lon/2) .* sin(delta_lon/2); %notice each * was replaced by .*
c = 2 * atan2(sqrt(a), sqrt(1-a));
km = R * c;
fprintf(' %d \n',km);

30 comentarios

Manoj Pai
Manoj Pai el 9 de Mayo de 2017
Thank you Walter
Manoj Pai
Manoj Pai el 9 de Mayo de 2017
I need to plot these values. Please guide me..
Manoj Pai
Manoj Pai el 9 de Mayo de 2017
Editada: Walter Roberson el 9 de Mayo de 2017
f1=figure(1);
cla;
hold on;
plot(lon1,lat1,lon2,lat2,'k-','marker','+')
%plot(lon2,lat2,'.g','MarkerSize',10)
plot_google_map
%is this way we can do?
That looks possible. I do not know plot_google_map though.
Manoj Pai
Manoj Pai el 9 de Mayo de 2017
ok. It works plot_google_map. If we run the plot_google_map function. The .csv file has (ie FirstFile.csv and SecondFile.csv) contains multiple data(it contains of about 500 values of lat and long). I need to calculate Haversine formula for each data(lat long) of the .csv file. Does the solution provided above by you works?
Yes. With the change from * to .* that I pointed out, all of the values will be computed at the same time, provided that you are comparing the corresponding entries in each set. (If you want to take the distance from each member of the first file to every member of the second file, and so on, then the code would have to change a bit.)
Manoj Pai
Manoj Pai el 9 de Mayo de 2017
Samples of csv file data First file lat lon 15 74 16 75 .. ..
Second file lat lon 17 76 18 77 .. .. I need to calculate first entry of lat lon of firstfile.csv (15 74) with first entry of secondfile.csv(17,76) and second entry of firstfle.csv (16 75) with second entry of secondfile.csv(18 77) and so on... Could you please tell me what changes needs to be done?
Manoj Pai
Manoj Pai el 9 de Mayo de 2017
Editada: Manoj Pai el 9 de Mayo de 2017
These are the two .csv files
"and second entry of firstfle.csv (16 75) with second entry of secondfile.csv(18 77)"
That is already what the code does.
Manoj Pai
Manoj Pai el 9 de Mayo de 2017
Thank you...
Manoj Pai
Manoj Pai el 11 de Mayo de 2017
beep; msgbox('Danger, Will Robinson!') I need this message when there is the intersection of lat, long of both the files. I have shared the output of the code. The point at which there is intersection an alert message should be popped up.
if any(km == 0); beep; msgbox('Danger, Will Robinson!'); end
Manoj Pai
Manoj Pai el 12 de Mayo de 2017
Editada: Walter Roberson el 12 de Mayo de 2017
I have those lat, long values in radians.
How to calculate the trigonometric functions for radians values.
delta_lat = lat2 - lat1; % difference in latitude
%fprintf(' %d \n',delta_lat);
delta_lon = lon2 - lon1; % difference in longitude
a = sin(delta_lat/2) .* sin(delta_lat/2) + cos(lat1) .* cos(lat2) .* sin(delta_lon/2) .* sin(delta_lon/2);
c = 2 * atan2(sqrt(a), sqrt(1-a));
sin() and cos() and atan2() automatically work with radians.
You would have needed to do something special to work with degrees -- you would have had to use sind() and cosd() and atan2d()
Manoj Pai
Manoj Pai el 12 de Mayo de 2017
ok Thank You
Manoj Pai
Manoj Pai el 6 de Sept. de 2017
How to plot lat1 lon1 and lat2 lon2 on a single graph
plot(lon1,lat1,lon2,lat2,'k-','marker','+')
hold on
plot(lon2,lat2,'.g','MarkerSize',10)
Manoj Pai
Manoj Pai el 7 de Sept. de 2017
Thanks a lot
In this file first row consists of title as lat1, lon1...... Should I need to do some changes in this part of the code?
lat1 = latlong1(:,1);
lon1 = latlong1(:,2);
lat2 = latlong2(:,1);
lon2 = latlong2(:,2);
latlong1 = csvread('FirstFile.csv', 1, 0);
Note that this requires R2014a or so (not sure of exact first release)
Manoj Pai
Manoj Pai el 8 de Sept. de 2017
Editada: Manoj Pai el 8 de Sept. de 2017
latlong1= csvread('C:\Users\Desktop\FirstFile .csv',1,0);
Manoj Pai
Manoj Pai el 8 de Sept. de 2017
Can we use this for file in different directory
Sure, reading from a different directory is no problem, in exactly the way you posted. Just watch out: I see a space between 'FirstFile' and '.csv' that should probably not be there.
Manoj Pai
Manoj Pai el 9 de Sept. de 2017
I am new to Matlab.Thank you I learnt a lot from you.
Manoj Pai
Manoj Pai el 9 de Sept. de 2017
Editada: Walter Roberson el 9 de Sept. de 2017
clear
clc
HV = csvread('FirstFile.csv',1,0);
RV = csvread('SecondFile.csv',1,0);
lat1 = HV(:,1);
lon1 = HV(:,2);
speed1 = HV(:,3);
heading1 = HV(:,4);
lat2 = RV(:,1);
lon2 = RV(:,2);
speed2 = RV(:,3);
heading2 = RV(:,4);
R = 6378.137; %Earth's radius in km
KMPH_TO_MPS = 0.277778; %conversion from kmph to mps
iteration = 200; %iteration for 200 values
T = 1;
for i=0:iteration
% %Calculation of future points for HV
Vx = sind(heading1).*speed1;
Vy = cosd(heading1).*speed1 ;
Lat2_HV = (lat1 + (T * Vy/111));% T is in hours
lat1=Lat2_HV;
Long2_HV = lon1 +( T * (Vx / cosd(Lat2_HV))/111);% 1 degree of lattitude/longitude corresponds to 111km
lon1=Long2_HV;
%Calculation of fiture points for RV
Vx1 = sind(heading2).* speed2;
Vy1 = cosd(heading2).*speed2;
Lat2_RV = (lat2 + (T * Vy1/111)); % T is in hours
lat2=Lat2_RV;
Long2_RV = lon2 +( T * (Vx1 / cosd(Lat2_RV))/111); % 1 degree of lattitude/longitude corresponds to 111km
lon2=Long2_RV;
delta_lat = (Lat2_HV - Lat2_RV ) ; % Difference between latitudes of HV and RV
delta_lon = (Lon2_HV - lon2_RV) ; % Difference between longitudes of HV and RV
a = sind(delta_lat/2) .* sind(delta_lat/2) + cosd(Lat2_RV) .* cosd(Lat2_HV) .* sind(delta_lon/2) .* sind(delta_lon/2); %notice each * was replaced by .*
c = 2 * atan2(sqrt(a), sqrt(1-a));
km = R * c;
m = km * 1000;
plot(lat1,lon1,'*',lat2,lon2,'+',Lat2_HV,Lat2_RV,'b--',Lon2_HV,Lon2_RV,'r--'); hold on
drawnow
fprintf('Distance between vehicles= %f \n',m);
if(m<=30) % Checking safety distance for alert condition
% Calculation for relative velocity
HV1=((speed1)*(cosd(heading1)));
HV2=((speed1)*(sind(heading1 )));
RV1=((speed2)*(cosd(heading2)));
RV2=((speed2)*(sind(heading2)));
s1=(HV1)+(RV1);
s2=(HV2)+(RV2);
R_speed=(s1.^2)+(s2.^2);
R_speed1=(R_speed.^(1/2));
fprintf('RELATIVE_SPEED= %f \n',R_speed1); %Display relative velocity of HV and RV
% calculation of TTC with predicted trajectory of HV and RV with relative speed of HV and RV
TTC=dm2/(R_speed1* KMPH_TO_MPS);
fprintf('TTC= %f \n',TTC);
msgbox('IMA ALERT');
fprintf('ALERT');
else
fprintf('safe');
end
end
%plot(lon1,lat1,lon2,lat2,'k-','marker','+')
%hold on
%plot(lon2,lat2,'.g','MarkerSize',10)
Manoj Pai
Manoj Pai el 9 de Sept. de 2017
Editada: Walter Roberson el 9 de Sept. de 2017
Error using +
Matrix dimensions must agree.
Error in calculation (line 29)
Long2_HV = lon1 +( T * (Vx / cosd(Lat2_HV))/111);% 1 degree of lattitude/longitude corresponds to 111km
I am getting this error
The files that you posted first time around only have two columns. You posted a new version of FirstFile.csv with more columns, but you did not post a new version of SecondFile.csv
Manoj Pai
Manoj Pai el 9 de Sept. de 2017
Yes it contains 4 columns in both the files.. lat1, lon1, speed1, heading1
You had / in two places where you needed ./
You had * in some places where you needed .*
However: you are treating m (the vector of distances) as if it were a scalar, but it is a vector. When you go to print out the distances, you are printing out 599 distances at the same time, which is pretty much useless. When you test m<=30 you are testing whether all m<=30 because of the way that vectorized "if" works; you can change that to any(m<=30) to at least be more correct.
In the above code I am predicting the future points from previous points(which is in csv file) and if future points distance is less than 30m then printing alert and msgbox. Vx = sind(heading1).*speed1; Vy = cosd(heading1).*speed1 ; Lat2_HV = (lat1 + (T .* Vy/111));% T is in hours lat1=Lat2_HV;
Lon2_HV = lon1 +( T .* (Vx ./ cosd(Lat2_HV))./111);% 1 degree of lattitude/longitude corresponds to 111km
lon1=Lon2_HV;
%Calculation of fiture points for RV
Vx1 = sind(heading2).* speed2;
Vy1 = cosd(heading2).*speed2;
Lat2_RV = (lat2 + (T .* Vy1./111)); % T is in hours
lat2=Lat2_RV;
Lon2_RV = lon2 +( T * (Vx1 ./ cosd(Lat2_RV))./111); % 1 degree of lattitude/longitude corresponds to 111km
lon2=Lon2_RV;

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 9 de Mayo de 2017

Comentada:

el 9 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by