1

I have a line with a,

  • Mid Point
  • End Point

I'm trying to return a coordinate that's LENGTH away from Mid point (on the same line) and between end and mid (oppose to between Midpoint and the Startpoint).

private static Coordinate direction(Coordinate end, Coordinate mid){
   double dy1 = (mid.y + LENGTH); 
   double dx1 = (mid.x + LENGTH); 

   double dy2 = (mid.y - LENGTH); 
   double dx2 = (mid.x - LENGTH);

   GeodesicData g1 = Geodesic.WGS84.Inverse(end.getY(), end.getX(), dy1, dx1);  
   GeodesicData g2 = Geodesic.WGS84.Inverse(end.getY(), end.getX(), dy2, dx2);  

   if(g2.s12 > g1.s12)
   {
      return new Coordinate(dx1, dy1); 
   } 
   else 
   {
      return new Coordinate(dx2, dy2); 
   }
}

The problem is when mid.x / mid.y are around 0 I get really weird behavior from the lat/lon arithmetic.
I think that's the problem, not sure how to fix it.

0

1 Answer 1

2

I don't know Java, but I tried to understand your code.

I see, first of all, that you are applying Cartesian geometry with geographic coordinates. That is a known error for all non-terraplanist people.

But let's believe for a moment that the Earth is flat and that the geographic coordinates form a rectangular grid on a plane, in which you can add or subtract degrees and meters without noticing the difference.

You are getting the coordinates of a point far away from de middle point of a line, a distance of LENGTH * sqrt(2), in a North-East direction, and other point at the same distance in a South-West direction.

Then, you are getting the ellipsoidal distance from the end point of a line, to both coordinates pairs that you got before. And using a logic that the smaller distance will be to the point that you want.

But is the line SW - NE oriented? If the line is NW - SE oriented instead, you must have the same distance to both points (not really true in a non-terraplanist environment). If it is oriented S - N, the points are not on the line. Indeed, the points are not on the line except that the line is SW - NE oriented.

If the orientation of the line is fixed at what we say 45 degrees azimuth, then the only errors with your code is the distance to the points, and the addition and subtraction between angular and linear units. The second one can't be fixed.


I don't know geotools neither. But I see that geographiclib is implemented there to solve the Inverse problem of geodesy: knowing two points, get the geodesic line that unites them.

There is also a Direct problem of geodesy: knowing one point, the forward azimuth and a length over a geodesic line, get the second point.

So the logic that you need is: get the initial azimuth of the geodesic line that starts at the middle point and ends at the end point, solving the Inverse problem.

Then, get the point that you want solving the Direct problem, from the middle point, with the forward azimuth that you got before, and the given distance.

The code may look like:

GeodesicData inverse_solution = Geodesic.WGS84.Inverse(mid.y, mid.x, end.getY(), end.getX());
double forward_azimuth = inverse_solution.az1

GeodesicData direct_solution = Geodesic.WGS84.Direct(mid.y, mid.x, forward_azimuth, LENGTH);

double wanted_x = direct_solution.lon2;
double wanted_y = direct_solution.lat2;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.