Create a rectangle with coordinates (latitude and longitude)

2.7k Views Asked by At

I have two points on a map, I want to create a rectangle where the two points are the line that intersect the rectangle. I understand that there is no true rectangle on a sphere, but the areas I am dealing with are small, the length of the rectangles are no more than a few km and the heights a few hundred meters. So if the calculation is approximate that's fine. Any help appreciated! Thanks, Philip

2

There are 2 best solutions below

3
On

The simplest case: Let the two points be $(x_1,y_1)$ and $(x_2,y_1)$ and the thickness of rectangle is $t$. The coordinates of the rectangle are: $(x_1,y_1+t/2),(x_1,y_1-t/2),(x_2,y_1-t/2),(x_2,y_1+t/2)$

0
On

My algorithm/calculation almost works, however in some directions the rectangles aren't right angled. I want to know why, there is a fair amount of approximation in my code, which should be fine based on the scales I use (a few km). I shouldn't have to worry about the curvature of the earth.

Anybody know how I can improve my code? Here are my references:

calculate points

lat and long to meters

here is the image :map retangles

        public static MapRectangle CreatMapRectangle(BalingZone zone, double thickness)
    {
        MapsCoordinate rectpoint2;
        MapsCoordinate rectPoint1 ;
        MapsCoordinate rectPoint3 ;
        MapsCoordinate rectPoint4 ;

        var point1 = zone.Coordinates[0];
        var point2 = zone.Coordinates[1];

        var latitudeDiff = LatitudeDiffToMeters(point2.Latitude - point1.Latitude);
        var longitudeDiff = LongitudeDiffToMeters(point1.Longitude - point2.Longitude, point1.Latitude);
        var slopeB = longitudeDiff / latitudeDiff;

        double latOffset = thickness * (slopeB / Math.Sqrt(1 + slopeB * slopeB));
        double longOffset = thickness * (1 / Math.Sqrt(1 + slopeB * slopeB));


        double p3Lat = CalculateLatitude(point1.Latitude, latOffset);
        double p3Long = CalculateLongitude( point1.Longitude, p3Lat , longOffset);
        rectPoint1 = new MapsCoordinate(p3Lat, p3Long);


        double p4Lat = CalculateLatitude(point1.Latitude, -latOffset);
        double p4Long = CalculateLongitude(point1.Longitude, p4Lat, -longOffset);
        rectpoint2 = new MapsCoordinate(p4Lat, p4Long);


        double p5Lat = CalculateLatitude(point2.Latitude, latOffset);
        double p5Long = CalculateLongitude( point2.Longitude, p5Lat , longOffset);
        rectPoint4 = new MapsCoordinate(p5Lat, p5Long);

        double p6Lat = CalculateLatitude(point2.Latitude, -latOffset);
        double p6Long = CalculateLongitude( point2.Longitude, p6Lat , -longOffset);
        rectPoint3 = new MapsCoordinate(p6Lat, p6Long);

        return new MapRectangle(rectPoint4, rectPoint3, rectPoint1, rectpoint2, thickness);
    }

    //use the quick and dirty estimate that 111,111 meters (111.111 km) in the y direction is 1 degree (of latitude)
    // and 111,111 * cos(latitude) meters in the x direction is 1 degree (of longitude).

    private static double LatitudeDiffToMeters(double latitudeDiff)
    {
        return 111111.0 * latitudeDiff;
    }

    private static double LongitudeDiffToMeters(double longitudeDiff, double latitude)
    {
        return 111111.0*Math.Cos(latitude)*longitudeDiff;
    }


    private static double CalculateLatitude(double latitude, double offset)
    {
        return latitude + offset/111111.0;
    }

    private static double CalculateLongitude(double longitude, double latitude, double offset)
    {
        return longitude + offset/(111111.0*Math.Cos(latitude));
    }
}