Thursday 23 November 2017

java - Calculate new coordinate based on distance traveled between two other coordinates


We have two coordinates (latitude, longitude) and let's assume we want to travel along a straight line between them. After traveling a certain distance along this line, we would like to calculate a new coordinate for this location. How can this be done?


Point startCoord = new Point(52.515343254180486, 13.384435940499088);
Point endCoord = new Point(52.51544771840784, 13.386055994744083);
double totalDistance = 110.23868303918874; // in meters
double intervalDistance = 50; // in meters

Note: Distances between points will always be fairly small.


Possible solution:


Based on this post we can find the angle between startCoord and endCoord.



Using the intervalDistance as hypotenuse, we can find x and y distances away from the startCoord using simple trig:


double dx = intervalDistance * Math.sin(angle);
double dy = intervalDistance * Math.cos(angle);

And based on this post, we can calculate the new coordinate based on the x and y distance away (in meters) from the startCoord.



Answer



This is actually both of the Geodetic Problems, used serially.


First you solve the Inverse (aka Reverse) problem, to obtain distance and bearing from two points, then you use the Forward (aka Direct) problem to travel distance and bearing from the first point.


I'd recommend you find a library that implements these functions, but it's certainly possible to port the US National Geodetic Survey FORTRAN code to Java yourself (I did, long ago, first to 'C' then to Java, but NGS has tweaked the code since then).


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...