4

I'm trying to draw a square around a given point on the earth's surface.

I'm using information I retrieved from here and here and ultimately came up with this:-

        // Converting degrees to radians
        double latInDecimals = (Math.PI / 180) * latitude;
        double longInDecimals = (Math.PI / 180) * longitude;

        List<string> lstStrCoords = new List<string>();

        double changeInLat;
        double changeInLong;
        double lineOfLat;      

        // Calculating change in latitude for square of side 
        changeInLong = (side / 1000) * (360.0 / 40075);

        // Calculating length of longitude at that point of latitude
        lineOfLat = Math.Cos(longitude) * 40075;

        // Calculating change in longitude for square of side 'side'
        changeInLat = (side / 1000) * (360.0 / lineOfLat);

        // Converting changes into radians
        changeInLat = changeInLat * (Math.PI / 180);
        changeInLong = changeInLong * (Math.PI / 180);


        double nLat = changeInLat * (Math.Sqrt(2) / 2);
        double nLong = changeInLong * (Math.Sqrt(2) / 2);

        double coordLat1 = latInDecimals + nLat;
        double coordLong1 = longInDecimals + nLong;

        double coordLat2 = latInDecimals + nLat;
        double coordLong2 = longInDecimals - nLong;

        double coordLat3 = latInDecimals - nLat;
        double coordLong3 = longInDecimals - nLong;

        double coordLat4 = latInDecimals - nLat;
        double coordLong4 = longInDecimals + nLong;

        // Converting coords back to degrees

        coordLat1 = coordLat1 * (180 / Math.PI);
        coordLat2 = coordLat2 * (180 / Math.PI);
        coordLat3 = coordLat3 * (180 / Math.PI);
        coordLat4 = coordLat4 * (180 / Math.PI);

        coordLong1 = coordLong1 * (180 / Math.PI);
        coordLong2 = coordLong2 * (180 / Math.PI);
        coordLong3 = coordLong3 * (180 / Math.PI);
        coordLong4 = coordLong4 * (180 / Math.PI);

Now even though this works, the polygon that I get from joining these is a rectangle.

enter image description here

I'm confused as to what's wrong with my code.

Community
  • 1
  • 1
Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64

1 Answers1

7

A rectangle of one degree of latitude and longitude on the sphere has not the same length in km unless it is situated on the equator. It gets narrower towards the poles. If you want to make both sides the same size you have to make a correction

longitudinal_length = latitudinal_length / cos(latitude)

So you will need to divide your longitudinal length of your square by cos(latitude).

Now, your square might still be crooked, but this depends on how the map is projected and this is a completely different story. You would need to know the projection formulas used by Google to make a correction.

You may find more complicated formulas that take account of the fact that the earth is not a perfect sphere, but I think that this should be sufficient for your position marker. Note also that you will get a division by zero at +/-90 degree. So placing a rectangle on a pole requires another approach.

enter image description here
From: IBM Knowledge Center / Geographic coordinate system / Figure 4. Different dimensions between locations on the graticule

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thanks I will try this and let you know. And the reason I'm not really worried about the curvature of the earth is because the square that I'm creating are quite small - 3 metres. – Abijeet Patro Dec 13 '12 at 16:25
  • 1
    @AbijeetPatro - It doesn't matter how small your rectangle is. At 60 degrees latitude, longitudinal_length will always be double the latitudinal_length if you don't use this correction. – mbeckish Dec 13 '12 at 20:24
  • Yes, the longitudinal lines all meet at the poles and have a maximum distance at the equator. – Olivier Jacot-Descombes Dec 13 '12 at 20:33
  • So if for example I am at 60 degrees on the earth's surface and my latitude length is x then my longitudnal length will 2x ? – Abijeet Patro Dec 14 '12 at 06:39
  • Yes, you must make your longitudinal length in degrees longer by `1/cos(latitude)` (=2 for 60 degrees) in order to make it the same length in km (or pixels) than the latititudinal length. This is why you get a rectangle instead of a square now. Have alook at the red rectangle in [this picture](http://www.eas.purdue.edu/mesozoic/Lab_11/Latitude_Longitude.jpg). It gets narrower and narrower when you move it towards the north pole. – Olivier Jacot-Descombes Dec 14 '12 at 13:15
  • 1
    The link in my previous comment is no longer valid. Therefore I added an image to my answer. – Olivier Jacot-Descombes Mar 18 '19 at 13:24