Home > Blockchain >  Convert latitude / longitude to X/Y
Convert latitude / longitude to X/Y

Time:11-15

I'm working on a geo-localisation project. I have an image of my campus , I found the 4 Long/Lat of the 4 corners but now I'm stuck to find a way to convert this to a pixel localisation. I found a lot of topics about this but it's not very clear to me.

CodePudding user response:

Here's the method that can be used to get distance (in meters) between two LatLon coordinates. Once you have that it's then just some simple math to map it into pixels of your image.

public static double distance(LatLon loc1, LatLon loc2) {
    double EARTH_RADIUS = 6371000; // in meters

    double lat1 = Math.toRadians(loc1.latitude);
    double lon1 = Math.toRadians(loc1.longitude);
    double lat2 = Math.toRadians(loc2.latitude);
    double lon2 = Math.toRadians(loc2.longitude);

    // based on https://www.movable-type.co.uk/scripts/latlong.html

    double a = Math.sin((lat2 - lat1) / 2.0);
    double b = Math.sin((lon2 - lon1) / 2.0);
    double c = a * a   Math.cos(lat1) * Math.cos(lat2) * b * b;
    double distanceRadians = 2.0 * Math.asin(Math.sqrt(c));

    return EARTH_RADIUS * distanceRadians;
}

latitude is from range [-90,90] and longitude is from range [-180,180]

  • Related