Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating Lat and Long from Bearing and Distance

Tags:

java

I'm having a hard time wrapping my head around some Trigonometry. I am trying to deduce a destination latitude and longitude from a start lat and log and distance and bearing.

Fortunately, I found an amazing site which describes exactly the function I need: http://www.movable-type.co.uk/scripts/latlong.html " Destination point given distance and bearing from start point " I tried it in my java program but it is not working for me. I deployed it as the website said. Here is my code:

double dist = 150/6371;
double brng = Math.toRadians(90);
double lat1 = Math.toRadians(26.88288045572338);
double lon1 = Math.toRadians(75.78369140625);

double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) + Math.cos(lat1)*Math.sin(dist)*Math.cos(brng) );
double a = Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1), Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
System.out.println("a = " +  a);
double lon2 = lon1 + a;

lon2 = (lon2+ 3*Math.PI) % (2*Math.PI) - Math.PI;

System.out.println("Latitude = "+Math.toDegrees(lat2)+"\nLongitude = "+Math.toDegrees(lon2));

But it shows the output is:

a = 0.0
Latitude = 26.882880455723377
Longitude = 75.78369140625

I am not getting where i am doing the mistake. Please anybody can help me to find out the problem.

Thanx in Advance. :-)

like image 408
Abhendra Singh Avatar asked Apr 12 '12 07:04

Abhendra Singh


People also ask

How do you find lat long from distance and bearing?

Here is the formula to find the second point, when first point, bearing and distance is known: latitude of second point = la2 = asin(sin la1 * cos Ad + cos la1 * sin Ad * cos θ), and. longitude of second point = lo2 = lo1 + atan2(sin θ * sin Ad * cos la1 , cos Ad – sin la1 * sin la2)

How do you calculate lat long distance?

from math import cos, asin, sqrt, pi def distance(lat1, lon1, lat2, lon2): p = pi/180 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2 return 12742 * asin(sqrt(a)) #2*R*asin...

Can you calculate distance from coordinates?

The distance formula is: √[(x₂ - x₁)² + (y₂ - y₁)²]. This works for any two points in 2D space with coordinates (x₁, y₁) for the first point and (x₂, y₂) for the second point.


2 Answers

Your problem is on your first line.

Try

double dist = 150.0 / 6371.0;

The reason is that 150/6371 gets calculated as 0, because it performs integer division (rather than floating point division). This is true even though the result is being stored in a double. You can force floating point division by making one of the two numbers a floating point literal.

like image 122
Simon Nickerson Avatar answered Oct 16 '22 08:10

Simon Nickerson


if anyone needs a function that calculates point coordinates from other point moved by some distance, below is the working code. For me, it's just moving a point by some distance.

import static java.lang.Math.*;

void movePoint(double latitude, double longitude, double distanceInMetres, double bearing) {
    double brngRad = toRadians(bearing);
    double latRad = toRadians(latitude);
    double lonRad = toRadians(longitude);
    int earthRadiusInMetres = 6371000;
    double distFrac = distanceInMetres / earthRadiusInMetres;

    double latitudeResult = asin(sin(latRad) * cos(distFrac) + cos(latRad) * sin(distFrac) * cos(brngRad));
    double a = atan2(sin(brngRad) * sin(distFrac) * cos(latRad), cos(distFrac) - sin(latRad) * sin(latitudeResult));
    double longitudeResult = (lonRad + a + 3 * PI) % (2 * PI) - PI;

    System.out.println("latitude: " + toDegrees(latitudeResult) + ", longitude: " + toDegrees(longitudeResult));
}
  • latitude, longitude - entry point coordinates
  • distanceInMetres - distance that you want to move the point by
  • bearing - an angle, direction towards which you want to move the point. 0 is towards the North, 90 - East, 180 - South, 270 - West. And all between, i.e. 45 is North East.
  • earthRadiusInMetres - Earth radius in metres.

You can change the radius to 6371 if you want to have the input in kilometres or to miles if you want to have input in miles.

like image 14
kiedysktos Avatar answered Oct 16 '22 09:10

kiedysktos