MATLAB: Calculate LatLon Distance out of array coordinates - arrays

I've got an 1700 x 3 array, with latitude, longitude and metres above sea level. Like this:
51.2551649606487 7.15089717516404 153.110000000000
51.2552453948075 7.15086528446721 150.160000000000
51.2552903318980 7.15086348124900 150.200000000000
I want to calculate the distance between successive Lat Lon Coordinates, using the Haversine formula, since i have no access to the MATLAB mapping toolbox( https://de.mathworks.com/matlabcentral/fileexchange/38812-latlon-distance).
My question is, how i should change the given code, to read all of the 1700 coordinates directly from my array?
I read/checked the following link: MATLAB function to calculate distance between two coordinates (latitude and longitude). But it doesn't tell me how to read all the 1700 coordinates from my array at once.
Thank you in advance for everybody who is willing to help me!
Best regards

If I understand the question correctly, you should be able to split the data into 3 1700 x 1 arrays: latitude, longitude, and elevation. Then apply the haversine formula on those three arrays. (Although the typical haversine function doesn't account for elevation)
coordinates; %1700 x 3 array
latitudes=coordinates(:,1);
longitudes=coordinates(:,2);
elevations=coordinates(:,3);
lat1=0; %coordinates to compare to
long1=0;
earthRadius=6371000;
a=sind((latitudes-lat1)./2).^2 + cosd(latitudes).*cosd(lat1).*sind((longitudes-long1)./2).^2;
c=atan2(sqrt(a),sqrt(1-a));
distances=c*earthRadius;%in meters

Related

I have a list of 15 to 30 coordinates. Given any X,Y coordinates, what is the way to find the closest coordinates on the list from front end side?

I have a list of more than 20 latitude and longitude coordinates. Given any X,Y coordinates, then what is the way to find the closest coordinates on the list from front end side?
Think of the distance between any two points as a line. The length of this line can be found using the distance formula:
Here x2 and x1 are the latitudes and y1 and y2 are the longitude of the two locations.
You can iterate through the list and find the shortest one to find the closest location.

Add distance to a latitude or longitude

Say I have a latitude of 38.802610 and a longitude of -116.419389 and I want to be able to add a distance to that, 20 miles for example.
So far I have came up with a rough idea on how I would do this
Firstly, work out how many miles are in 1° of latitude, say for example it was 30
Then, divide one by it:
1 / 30 = 0.033333
Add it to my original latitude to get my maximum ° of latitude:
38.802610 + 0.033333 = 38.8355943
Subtract it to my original latitude to get my minimum ° of latitude:
38.802610 - 0.033333 = 38.769277
But this is flawed because there seems to be no direct conversion for longitude as from what I've read the calculation varies.
Ultimately, I need to be able to find out the:
maximum latitude (my current latitude + given distance e.g 20 miles)
minimum latitude (my current latitude - given distance e.g 20 miles)
maximum longitude (my current longitude + given distance e.g 20 miles)
minimum longitude (my current longitude - given distance e.g 20 miles)
Any help would be greatly appreciated, thank you.
Linear distances (e.g. 20 miles or 32186.88 m) cannot directly be converted to distances in degrees of latitude or longitude, since the Earth is not flat. However, there are two direct conversion you can try to project one point given a linear distance and azimuth to another point.
Method 1 is to assume a spherical Earth, using Movable Type's "destination point given distance and bearing from start point". The underling equations are described on the website. The difference of latitude is the same in North and South directions, and the difference of longitude is the same in East and West directions, so you can do a minimum of two calculations to determine all four min/max and lat/long combinations.
Method 2 is to assumes a spheroid Earth (also called an ellipsoid of revolution), and calculate a direct geodesic with GeographicLib, which has bindings to several programming languages. The difference of latitude is slightly different in North and South directions, but the same in East and West directions, so you can do a minimum of three calculations. Or just assume the difference in North or South directions is approximately the same and just do two calculations. This method has sub-millimetre accuracy.

Matching sets of coordinates

I have a set of points (500 points), each has its own coordinate. These points are represented by two different 2-D coordinate systems (e.g., (X, Y) and (x, y)). I have a list of 500 (X,Y)-type of coordinates and 500 (x,y)-type of coordinates. My goal is to match them pairwise. There should be 1-1 mapping between the two sets. I can also convert a distance from one coordinate system to the other.
I am thinking about using pairwise distances as a means to match. Please let me know if there is a simple way to do that. Thank you.

converting distance into coordinates [duplicate]

This question already has an answer here:
Closed 11 years ago.
How do i convert a given distance (in metres) into geographic coordinates.
What I need is to draw a polygon on the map (a regular polygon, like a circle), however, it's radius is given in meters, how do I calculate the radius in degrees or radians?
I'll probally code it in c
Oh man, geographic coordinates can be a pain in the behind. First of all, I'm assuming that by geographic coordinates, you're talking about geodetic coordinates (lat/lon).
Second of all, you can't find a "radius" in radians or degrees. Why, you ask? Well, one degree of longitude at the equator is WAY longer than one degree of longitude close to the north or south pole. The arc of one degree latitude also changes based on your location on the earth since the earth is not a perfect sphere. It's usually modeled as an ellipsoid.
That being said, here are two ways to map the coordinates of a polygon onto lat-lon coordinates:
1) If you're feeling like a complete badass, you can do the math in lat-lon. Lots of trig, easy to make mistakes... DON'T DO IT. I'm just including this option here to let you know that it is possible.
2) Convert your geodetic coordinates to UTM. Then, you can do whatever you need to do in meters (i.e. find the vertices of a polygon), and then convert the resulting UTM back to geodetic. Personally, I think this is the way to go.
Well, consider that at the equator (0 degrees latitude) one degree of longitude is equal to appximately 60 nautical miles. At either pole (90 degrees latitude) a single degree of longitude equals 0 nautical miles. As I remember the cosine of the latitude times 60 will give you the approximate distance in nautical miles at that latitude of a single degree of longitude.
However, how accurate you would be would have to account for the map projection you're using. For aeronautical maps, they use the Lambert Conformal Conic projection, which means distances are only exactly accurate along the two latitudes that the cone cuts the sphere of the earth. But if an approximation is good enough, you may not need the accuracy.
For conversion, one nautical mile equals 1.852 km. If I did the arithmetic properly (no guarantee, I'm in my 70s), that means that a meter equals (except as you get really close to the poles) 0.0000009 degrees latitude. It also equals 0.0000009 degrees longitude on the equator. If you're not at the equator, divide the 0.0000009 by the cosine of the latitude to get the degrees of longitude.
So, a 1000 meter radius circle at 45 degrees latitude would mean a radius of 0.0009 degrees latitude and 0.0009/0.707 degrees longitude. Approximately of course.
All this is from memory, so take it with a grain of salt. If you really want to get involved, Google geographic equations or some such.
Check out http://trac.osgeo.org/proj/wiki/GeodesicCalculations. Depending on the accuracy you need, this can get pretty complicated, so you're probably best off starting from some existing code.

Angle at corner of two lines

I search for the fastest or simplest method to compute the outer angle at any point of a convex polygon. That means, always the bigger angle, whereas the two angles in question add up to 360 degrees.
Here is an illustration:
Now I know that I may compute the angles between the two vectors A-B and C-B which involves the dot product, normalization and cosine. Then I would still have to determine which of the two resulting angles (second one is 180 degrees minus first one) I want to take two times added to the other one.
However, I thought there might be a much simpler, less tricky solution, probably using the mighty atan2() function. I got stuck and ask you about this :-)
UPDATE:
I was asked what I need the angle for. I need to calculate the area of this specific circle around B, but only of the polygon that is described by A, B, C, ...
So to calculate the area, I need the angle to use the formula 0.5*angle*r*r.
Use the inner-product (dot product) of the vectors describing the lines to get the inner angle and subtract from 360 degrees?
Works best if you already have the lines in point-vector form, but you can get vectors from point-to-point form pretty easily (i.e. by subtraction).
Taking . to be the dot product we have
v . w = |v| * |w| * cos(theta)
where v and w are vectors and theta is the angle between the lines. And the dot product can be computed from the components of the vectors by
v . w = SUM(v_i * w_i : i=0..3) // 3 for three dimensions. Use more or fewer as needed
here the subscripts indicate components.
Having actually read the question:
The angle returned from inverting the dot-product will always be less than 180 degrees, so it is always the inner angle.
Use this formula:
beta = 360° - arccos(<BA,BC>/|BA||BC|)
Where <,> is the scalar product and BA (BC) are the vectors from B to A (B to C).
I need to calculate the area of the circle outside of the polygon that is described by A, B, C, ...
It sounds like you're taking the wrong approach, then. You need to calculate the area of the circumcircle, then subtract the area of the polygon.
If you need the angle there is no way around normalizing the vectors and do a dot or cross-product. You often have a choice if you want to calculate the angle via asin, acos or atan but in the end that does not make a difference to execution speed.
However, It would be nice if you could tell us what you're trying to archive. If we have a better picture of what you're doing we might be able to give you some hints how to solve the problem without calculating the angle at the first place.
Lots of geometric algorithms can be rewritten to work with cross and dot-products only. Euler-angles are rarely needed.

Resources