1

Hi I'm currently trying to calculate a longitude/latitude point from 2 longitude/latitude points (bilateration) with distance & I've currenty done that :

    double EARTH_RADIUS = 6378137.0;

    double longitude1 = 4.062787;
    double latitude1 = 49.243828;
    double x1 = EARTH_RADIUS * (Math.cos(Math.toRadians(latitude1)) * Math.cos(Math.toRadians(longitude1)));
    double y1 = EARTH_RADIUS *(Math.cos(Math.toRadians(latitude1)) * Math.sin(Math.toRadians(longitude1)));

    double longitude2 = 4.062023;
    double latitude2 = 49.243851;
    double x2 = EARTH_RADIUS * (Math.cos(Math.toRadians(latitude2)) * Math.cos(Math.toRadians(longitude2)));
    double y2 = EARTH_RADIUS *(Math.cos(Math.toRadians(latitude2)) * Math.sin(Math.toRadians(longitude2)));

    System.out.println(x1 + " " + y1);
    System.out.println(x2 + " " + y2);

    double[][] positions = new double[][] { { x1, y1 }, { x2, y2 } };
    double[] distances = new double[] { 10.0, 10.0};

    NonLinearLeastSquaresSolver solver = new NonLinearLeastSquaresSolver(new TrilaterationFunction(positions, distances), new LevenbergMarquardtOptimizer());
    Optimum optimum = solver.solve();

    double[] centroid = optimum.getPoint().toArray();
    System.out.println(Arrays.toString(centroid));

I'm using this library https://github.com/lemmingapex/trilateration to trilaterate my point.

Converting longitude & latitude points to a cartesian plan & using a library to get a point on it, giving me this output :

[INFO] GCLOUD: 4153447.729890433 295011.4801210027
[INFO] GCLOUD: 4153449.72871882 294955.95932889543
[INFO] GCLOUD: [4153448.7293046266, 294983.7197249491]

So now I'm trying to convert this point into latitude & longitude point to put it on Google Map but I have no clue how to do that & if a Java library already exist for bilateration?

EDIT :

So I've done that :

private static final double EARTH_RADIUS = 6371; 

private static final int X = 0;
private static final int Y = 1;
private static final int Z = 2;

public static double[] triangulation(double lat0, double lon0, double r0, double lat1, double lon1, double r1) {
    double[] p1 = latlon2cartesian(lat0, lon0);
    double[] p2 = latlon2cartesian(lat1, lon1);

    double[][] positions = new double[][] { { p1[X], p1[Y], p1[Z] }, { p2[X], p2[Y], p2[Z] } };
    double[] distances = new double[] { r0, r1};

    NonLinearLeastSquaresSolver solver = new NonLinearLeastSquaresSolver(new TrilaterationFunction(positions, distances), new LevenbergMarquardtOptimizer());
    Optimum optimum = solver.solve();

    double[] centroid = optimum.getPoint().toArray();
    System.out.println(Arrays.toString(p1));
    System.out.println(Arrays.toString(p2));
    System.out.println(Arrays.toString(centroid));
    return cartesian2latlon(centroid[X], centroid[Y], centroid[Z]);
}

private static double[] latlon2cartesian(double lat, double lon) {
    lat = Math.toRadians(lat);
    lon = Math.toRadians(lon);
    return new double[] { Math.cos(lon) * Math.cos(lat) * EARTH_RADIUS, Math.sin(lon) * Math.cos(lat) * EARTH_RADIUS, Math.sin(lat) * EARTH_RADIUS };
}

private static double[] cartesian2latlon(double x, double y, double z) {
    return new double[] { Math.toDegrees(Math.asin(z / EARTH_RADIUS)), Math.toDegrees(Math.atan2(y, x)) };
}

But I don't get correct values with :

System.out.println(Arrays.toString(Bilateration.triangulation(49.243828, 4.062787, 5.0, 49.243851, 4.062023, 6.5)));

I get :

[49.2453096100026, 3.9213007384886387]

Near (2km away) but the point should be around 49.24385234064716, 4.062335368930235.

  • Aren't you already long + lat to cartesian coordinates? If so, can't you just reverse that process? – Thomas Mar 18 '19 at 11:14
  • Yes but I don't know how. Moreover , I'd like to know if there is a lib to do that directly? – Geoffrey Migliacci Mar 18 '19 at 12:28
  • Post edit with function. – Geoffrey Migliacci Mar 18 '19 at 13:49
  • Did you try to call `cartesian2latlon()` with the coordinates returned by `latlon2cartesian()` to check the results of both methods without the centroid calculations in between? Also note that in `latlon2cartesian()` you're calling `Math.toRadians()` but don't call `Math.toDegrees()` in `cartesian2latlon()` so the result you get will still be in radians. – Thomas Mar 18 '19 at 14:04
  • Your `cartesian2latlon()` seems to be wrong. Use `Math.toDegrees()` to convert the values back to degrees and have a look at daphshez' answer here: https://stackoverflow.com/questions/1185408/converting-from-longitude-latitude-to-cartesian-coordinates (the one with the most upvotes) – Thomas Mar 18 '19 at 14:18
  • @Thomas I'm getting values near (2km) the desired point, the thing is that I don't know which mesure I should use for the distance (r0, r1) currently I've put it in meters. The desired point should be near 49.243839, 4.062003. – Geoffrey Migliacci Mar 18 '19 at 14:35
  • I found out that the distance between the 2 points are 0.05561(in the cartesian plan) so I took a distance of around 0.02561 for each point & then it gave me a coordinate near the point thanks for your help! – Geoffrey Migliacci Mar 18 '19 at 14:52

0 Answers0