[Math] How to get a square starting with a latitude and longitude point

spherical coordinates

Is there a formula that given one latitude/longitude point and a radius (in kilometers ideally) would give me a square around the center?

A picture is worth some quantity of words:

Description in pic

I have the latitude/longitude value of point A, and I have D in kilometers; how can I get point B and C in latitude/longitude values?

Best Answer

For the corners of the square, we have this diagram

corner of square

To get $r$ from $D$ (converted to an angle by dividing the distance by the radius of the sphere), we use $\cos(\frac{\pi}{4})=\frac{\tan(D)}{\tan(r)}$ to get $$ \tan(r)=\sqrt{2}\tan(D)\tag{1} $$ Then use the Law of Cosines to get the latitudes of the top and bottom corners $$ \sin(lat_1)=\sin(lat)\cos(r)+\cos(lat)\sin(r)/\sqrt{2}\tag{2} $$ $$ \sin(lat_2)=\sin(lat)\cos(r)-\cos(lat)\sin(r)/\sqrt{2}\tag{3} $$ Then solve the Law of Cosines to get the differences in longitude of the top and bottom corners $$ \cos(\Delta long_1)=\frac{\cos(r)-\sin(lat_1)\sin(lat)}{\cos(lat_1)\cos(lat)}\tag{4} $$ $$ \cos(\Delta long_2)=\frac{\cos(r)-\sin(lat_2)\sin(lat)}{\cos(lat_2)\cos(lat)}\tag{5} $$