Calculating coordinates of square x miles from center point?
For this purpose simple approximations are more than good enough. North or south, one degree is about 69 miles but east or west, it is only 69*cos(latitude) miles. Because latitudes do not change much over a ten mile span, you can safely use the cosine of the central latitude of the "square." Therefore the desired coordinates for square vertices at distance r miles from a central location (f,l), given as lat-lon, are computed as
df = r/69 // North-south distance in degrees
dl = df / cos(f) // East-west distance in degrees
{(f-df,l-dl), (f+df,l-dl), (f+df,l+dl), (f-df,l+dl)} // List of vertices
For example, suppose r = 10 miles and the central location is at latitude 50 degrees north, longitude 1 degree west, so that (f,l) = (50,-1) degrees. Then
df = 10/69 = 0.145
dl = 0.145 / cos(50 degrees) = 0.145 / 0.6428 = 0.225
f - df = 50 - 0.145 = 49.855 (southernmost latitude)
f + df = 50 + 0.145 = 50.145 (northernmost latitude)
l - dl = -1 - 0.225 = -1.225 (western longitude)
l + dl = -1 + 0.225 = -0.775 (eastern longitude)
and the coordinates are (49.855,-1.225), (50.145,-1.225), (50.145, -0.775), and (49.855, -0.775) as you march clockwise around the square starting at its southwestern corner.
Don't use this approximation near the poles or for squares larger than a few degrees on a side. Also, depending on limitations of the GIS, some care might be needed around the global cut in longitude, usually taken at +-180 degrees.
Take the X coordinate of the center and subtract x miles from it this is the left side of your square. Then take the Y coordinate of the center and subtract X miles from it, this is the bottom of your square. Repeat these steps but adding instead of subtracting to get the right hand and top edges. You can now construct the four corners of your square.
Note the above assumes that your center point is in miles. If it is not first reproject it. Other wise all bets are off and your square will not be square.
Finally my answer is: (in c#)
I probably don't need the 4 coordinates but I think they are fairly accurate.
public static void GetBoundingCoords(double centerLat, double centerLong, double distance)
{
Coordinate top= MaxLatLongOnBearing(centerLat, centerLong,45,10);
Coordinate right = MaxLatLongOnBearing(centerLat, centerLong, 135, 10);
Coordinate bottom = MaxLatLongOnBearing(centerLat, centerLong, 225, 10);
Coordinate left = MaxLatLongOnBearing(centerLat, centerLong, 315, 10);
}
public static Coordinate MaxLatLongOnBearing(double centerLat, double centerLong, double bearing, double distance)
{
var lonRads = ToRadian(centerLong);
var latRads = ToRadian(centerLat);
var bearingRads = ToRadian(bearing);
var maxLatRads = Math.Asin(Math.Sin(latRads) * Math.Cos(distance / 6371) + Math.Cos(latRads) * Math.Sin(distance / 6371) * Math.Cos(bearingRads));
var maxLonRads = lonRads + Math.Atan2((Math.Sin(bearingRads) * Math.Sin(distance / 6371) * Math.Cos(latRads)), (Math.Cos(distance / 6371) - Math.Sin(latRads) * Math.Sin(maxLatRads)));
var maxLat = RadiansToDegrees(maxLatRads);
var maxLong = RadiansToDegrees(maxLonRads);
return new Coordinate(){Latitude=maxLat, Longitude=maxLong};
}
EDIT
Having just realised if I set the corners of my square x miles from the center point, the edges of my square won't be the same x miles. (said maths wasn't my strong point) So to get the corner points distance from the center point if I want my squares edges to x miles I used Pythagoras' Theorem to work out the distance of the diagonal. (on a right angled triangle, the square on the hypotenuse (the diagonal) is equal to he square of the other two sides)