Matlab GPS + distance in meters - matlab

I have to calculate for a given Latitude (lat0) and Longitude (lon0) the new latitude (lat1) and longitude (lon1) if I move a distance x[m] and y[m] away from the initial position. For example:
clear all; clc;
%initial coordinates:
lat0=56;
lon0=5;
%moving away from lat0,lon0,
xcor=200; %[m]
ycor=100; %[m]
First I use this code to calculate lat1 and lon1: (source: Adding distance to a GPS coordinate)
lat1=lat0+rad2deg((xcor/6372800))
lon1=lon0+rad2deg((ycor/6372800)/(cos(lat0)))
distance=sqrt(xcor^2+ycor^2)
Now i want to check my answer with the haversine equation:
dlat = deg2rad(lat1-lat0);
dlon = deg2rad(lon1-lon0);
lat0 = deg2rad(lat0);
lat1 = deg2rad(lat1);
a = (sin(dlat./2)).^2 + cos(lat0) .* cos(lat1) .* (sin(dlon./2)).^2;
c = 2 .* asin(sqrt(a));
distance_check=6372800*c
However, most of the time the distance from haversine is different with 100+ meters compared to the calculated distance in the first 3 lines of code.
What is going wrong in this code?

In the first code, 2nd line, cos(lat0) has to be cos(deg2rad(lat0)).

Related

Compute coordinates position with projection

Given 2 coordinates (point 1 and 2 in red) in WGS84 I need to find the coordinates of the point perpendicular (point 3) to the line at a given distance.
I could manage to make the math to compute this perpendicular point, but when displayed on the map, the point seems to be at a wrong place, probably because of the projection.
What I want on a map:
And what I have instead on the map:
How can I take into account the projection so that the point on the map appears perpendicular to the line? The algorithm below to compute the point comes from here: https://math.stackexchange.com/questions/93424/calculate-rectangle-coordinates-from-line-and-height
public static Coords ComputePerpendicularPoint(Coords first, Coords last, double distance)
{
double slope = -(last.Lon.Value - first.Lon.Value) / (last.Lat.Value - first.Lat.Value);
// number of km per degree = ~111km (111.32 in google maps, but range varies between 110.567km at the equator and 111.699km at the poles)
// 1km in degree = 1 / 111.32km = 0.0089
// 1m in degree = 0.0089 / 1000 = 0.0000089
distance = distance * 0.0000089 / 100; //0.0000089 => represents around 1m in wgs84. /100 because distance is in cm
double t = distance / Math.Sqrt(1 + (slope * slope));
Coords perp_coord = new Coords();
perp_coord.Lon = first.Lon + t;
perp_coord.Lat = first.Lat + (t * slope);
return perp_coord;
}
Thank you in advance!

Calculate Latitude and longitude more between Latitude/Longitude points?

Latitude: 22.744812,
Longitude: 75.892578
The above would be considered my center point.
And now I need to determine the latitude and longitude points from center point 1000 meter outward to each NSWE corners. So I would have a central long/lat, N, S, E and W long/lat..
So I would end up with 4 additional lat/long pairs.
What I am trying to resolve is a formula, preferably that can be done on a standard calculator to determine these 4 NSWE points based on the central point.
You could use MapKit for that:
- (CLLocationCoordinate2D *) calculateSquareCoordinates:(CLLocation*)center withRadius:(float)radius{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center.coordinate, radius*2, radius*2);
CLLocationCoordinate2D points[4];
points[0] = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2);
points[1] = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2);
points[2] = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
points[3] = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
return points;
}
and just call
CLLocationCoordinate2D *fourPoints = [self calculateSquareCoordinates:center withRadius:1000];
on your code.
you will have to use the Haversine formula to calculate the Lat/Long based on distance from a starting Lat/Long. have a look at this Link
The average radius of the earth is around 6371000 metres. This means that
1 degree of lattitude is equivalent to 6371000 * PI / 180 metres
(NB: PI = 3.14159... etc). However, 1 degree of longitude depends on the lattitude that you are. At the equator, one degree of longitude corresponds to the same distance in metres as 1 degree of lattitude. However, at the north and south poles, all longitude values are the same point (i.e. the pole itself), so 1 degree of longitude at the poles is zero metres. The formula for longitude is
1 degree of longitude is equivalent to 637100 * PI / 180 * COS(Lattitude)
where COS is the trigonometric cosine function. If you make these conversions, then you can do the calculation on a standard calculator. However, be aware that these are approximations that work well over short distances (e.g. less than a few hundred kilometers), but over long distances (e.g. thousands of kilometers) they become more and more inaccurate.

Latitude / Longitude Distance Calculation

A quick question about a Lat / Long calculation.
I want to take a value set e.g. Lat: 55.123456 Long -6.123456 and work out the four points that are an arbitrary distance away.
As the given square, I want to work out the value for Latitude on the left and right side. Thus the red lines are 1.5km from the start point. Likewise for the longitude, the blue lines will be 1.5km from the start point. The output will be 4 points, all distances in kilometres.
In short: Latitude + Y = Latitude Value X kilometers away
Working with iPhone at the moment and its for a very rough database calculation.
EDIT: Just to clarify, the distance is so short that curvature (And hence accuracy) is not an issue.
In OBJ-C this should be a decent solution:
float r_earth = 6378 * 1000; //Work in meters for everything
float dy = 3000; //A point 3km away
float dx = 3000; //A point 3km away
float new_latitude = latitude + (dy / r_earth) * (180 / M_PI);
float new_longitude = longitude + (dx / r_earth) * (180 / M_PI) / cos(latitude * 180/M_PI);
Well, for rough calculation with relatively small distances (less than 100km) you may assume that there is 40_000_000/360=111 111 meters per degree of latitude and 111 111*cos(latitude) meters per degree of longitude. This is because a meter was defined as 1/40_000_000 part of the Paris meridian;).
Otherwise you should use great circle distances, as noted in the comments. For high precision you also need to take into account that Earth is slightly oblate spheroid rather than a sphere.
// parameter: offset in meters
float offsetM = 1500; // 1.5km
// degrees / earth circumfence
float degreesPerMeter = 360.0 / 40 000 000;
float toRad = 180 / M_PI;
float latOffsetMeters = offsetM * degreesPerMeter;
float lonOffsetMeters = offsetM * degreesPerMeter * cos (centerLatitude * toRad);
Now simply add +/- latOffsetMeters and +/- lonOffsetMeters to your centerLatitude/ centerLongitude.
Formula is usefull up to hundred kilometers.

Calculate Distance in Km and Miles

I have two points whose latitude and longitude i know.
How can i calculate the distance(in Km and Miles) between them. What is the formulae?
You can use the haversine formula to calculate such distances.
Use the haversine Formula for this...
Here is the link having java script code to calculate distance
http://www.movable-type.co.uk/scripts/latlong.html
A = LAT1, B = LONG1
C = LAT2, D = LONG2 (all converted to radians: degree/57.29577951)
IF A = C AND B = D THEN DISTANCE = 0;
ELSE
IF [SIN(A)SIN(C)+COS(A)COS(C)COS(B-D)] > 1 THEN DISTANCE = 3963.1*ARCOS[1];
ELSE
DISTANCE=3963.1*ARCOS[SIN(A)SIN(C)+COS(A)COS(C)COS(B-D)];
For an accurate and complete (works with any pair of points) solution
use my geodesic calculator at
http://geographiclib.sf.net/cgi-bin/GeodSolve. The formulas are given in
http://arxiv.org/abs/1102.1215.

How do I calculate the distance between two points of latitude and longitude? [duplicate]

This question already has answers here:
Distance between two coordinates with CoreLocation
(4 answers)
Closed 8 years ago.
i have latitude and longitude of particular place and i want to calculate the distance so how can i calculate it?
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
NSLog(#"Distance i meters: %f", [location1 distanceFromLocation:location2]);
[location1 release];
[location2 release];
You also need to add CoreLocation.framework to your project, and add the import statement:
#import <CoreLocation/CoreLocation.h>
This might not be the most efficient method of doing it, but it will work.
Your two locations specified by latitude and longitude can be considered vectors. Assuming that the coordinates have been converted into cartesion coordinates, calculate the dot product of the two vectors.
Given v1 = (x1, y1, z1) and v2 = (x2, y2, z2), then ...
v1 dot v2 = magnitude(v1) * magnitude(v2) * cos (theta)
Conveniently, the magnitude of v1 and v2 will be the same ... the radius of the earth (R).
x1*x2 + y1*y2 + z1*z2 = R*R*cos(theta)
Solve for theta.
theta = acos ((x1*x2 + y1*y2 + z1*z2) / (R * R));
Now you have angle between the two vectors in radians. The distance betwen the two points when travelling across the surface of earth is thus ...
distance = theta * R.
There is probably an easier way to do this entirely within the context of spherical coordinates, but my math in that area is too fuzzy--hence the conversion to cartesian coordinates.
To convert to cartesian coordinates ...
Let alpha be the latitude, and beta be the longitude.
x = R * cos (alpha) * cos (beta)
y = R * sin (alpha)
z = R * cos (alpha) * sin (beta)
Don't forget that the math function typically deal in radians, and the latitude/longitude deal in degrees.
I've cranked through the math, and can now greatly simplify the solution.
Imagine if we spin the earth so that our first vector is at 0 degrees latitude and 0 degrees longitude. The second vector would be at (alpha2 - alpha1) degrees latitude and (beta2 - beta1) degrees latitude.
Since ...
sin(0) = 0 and cos(0) = 1
our dot product simplies to ...
cos(delta_alpha) * cos(delta_beta) = cos(theta)
The rest of the math remains unchanged.
theta = acos (cos(delta_alpha) * cos(delta_beta))
distance = radius * theta
Hope this helps.