# Calculating coordinates of square x miles from center point?

I am trying to create a x mile square (or circle) around a central point, where all sides of the square would be x miles from the centre. I need the 4 corner coordinates.

It is scrambling my brain trying to get my head round it? I can work out the distance between two points using the haversine formula but maths is seriously not my strong point and I don't understand sin,cos etc.. and trying sort this out has lost me!

I have come across Calculating Latitude/Longitude X miles from point? but I just don't get it!

Would anyone be kind enough to explain how I do this in apples and pears terms?

To explain exactly what I trying to do;

I have a website, where users can search for buildings in a specific area. They will enter a town or place (which I will know the lat long of) and they search within a specific radius of say 10 miles of the place.

I need to find the min/max lat and longs of the 10mile radius so I can query my database using a where clause similar to:

``````Where buildingLat <= maxLat
and buildingLat <= minLat
and buildingLong >= minLong
or buildingLong >= maxLong
``````

I need some kind of formula!

My coordinates are in decimal degrees

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.

• Shouldn't the calculation of `dl` be `df * cos(f)`, as you mention in the second sentence, "... but east or west, it is only 69*cos(latitude) miles"? Aug 31, 2021 at 20:12
• @Black this transformation is going the other way: it is computing the map coordinates. It therefore must distort the east-west distances by `1/cos(f)`. Aug 31, 2021 at 20:31
• I seem to be lost using the formula described. The bounding box formula seems to break down, given these lat/long `(f, l)` points: `(48.71626872816001, -124.76410018717496)` and `(48.70177597453682, -124.76410018717496)`. Using `r = 1` and the formulae described above, the longitudinal values for the second point creates a much wider box than would be expected (full math redacted for brevity. It seems to happen because `cos(f)` gets very small for the second point. Any ideas as to whether this is expected? Happy to pose a separate question if need be. Aug 31, 2021 at 21:08

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)
{

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)

• that won't be a square. Oct 11, 2011 at 15:21
• I have amended my bearings, it was a diamond.. it's now square
– Bex
Oct 11, 2011 at 15:41
• Concerning the edit, the Pythagorean Theorem is ok for small squares, but in general it does not hold for spherical right triangles. It's strange, then, that your code uses spherical trigonometry together with this approximation based on plane geometry. Oct 16, 2011 at 14:49
• My distance will never be more than 100 miles so I don't think it matters. As the inaccuracies will be small with that smaller distance.
– Bex
Oct 17, 2011 at 7:37
• I think the point @whuber is making is that there is no benefit to using a complex calculation of distance along Lat and Long, given that you then combine those with pythagorean theorem. You could instead use a much simpler calculation, such as whuber did, or as Ewan Todd does. Use simple linear formulas to convert longitude to miles and latitude to miles, or vice versa. The only "tricky" part is knowing that longitude formula needs the adjustment `* cos(lat)` when converting degrees to miles, and `/ cos(lat)` when converting miles to degrees. Nov 24, 2018 at 20:40

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.

• Are you referring to the example page? How do I subtract miles from a coordinate and do I not need to take the curvature of the earth into account?
– Bex
Oct 11, 2011 at 13:21
• No it's so simple there is no actual need to have a worked example. Maybe you need to specify what software you are using? As I said my solution assumes you have projected your coordinate to a local SRS (if you haven't then that should be your first question) Oct 11, 2011 at 13:27
• I am really confused now! I have updated my question to say exactly what I am trying to do. I kind of need to create a bounding box so I can find the min and max lat and long.
– Bex
Oct 11, 2011 at 13:35
• proj4js (proj4js.org) should help then Oct 11, 2011 at 14:14
• My coordinates look like this: 51.498485,-0.129089 that is degrees isn't it?
– Bex
Oct 11, 2011 at 14:46

If you are using a spatially aware database you can convert your area of interest into the same coordinate system your data is stored in and then do an apples to apples comparison.

For example:

1. User picks a location, resulting in lat/lon.
2. Ask spatial database to convert this point into a projected coordinate system appropriate to the area (units of feet or meters, etc.).
3. Build your area of interest around the projected point.
4. Ask spatial database to convert this area of interest back to lat/lon.
5. Do whatever comparisons you need to do.

Destination point given distance and bearing from start point

Formula:
lat2 = asin(sin(lat1)*cos(d/R) + cos(lat1)*sin(d/R)*cos(θ))
lon2 = lon1 + atan2(sin(θ)*sin(d/R)*cos(lat1), cos(d/R)−sin(lat1)*sin(lat2))

θ is the bearing (in radians, clockwise from north); d/R is the angular distance (in radians), where d is the distance travelled and R is the earth’s radius

For θ I used -45 degrees (in radians) for the "upper-left point" and 135 degrees for the "bottom-right" one

(I recently asked the same question in the math site)

• The problem with this solution is you first have to figure out the distance for the diagonals of the square. It's easier instead to move along geodesics from the center to the midpoints of the sides and then, turning 90 degrees, to move along the square's sides. Oct 16, 2011 at 14:52
• @whuber, If the distance is small enough, can't you use simple trigonometry for it? (d = opposing side / sin(adjacent side). I do that because I don't need that distance to be exact. Otherwise, you could apply this formula twice to do exactly what you say (first with θ=0 then with θ=-90 for the upper left for example)
– juan
Oct 16, 2011 at 21:46
• That is correct, Juan. But one is left wondering why you use the more complicated formulas of spherical geometry when you start out with an approximation that supposes Euclidean formulas will work in the first place. There's nothing wrong with using the spherical formulas, but it's unnecessary and computationally inefficient. Oct 16, 2011 at 23:43
• @whuber, for the second case it isn't approximate (applying the formula twice for each side). You're right that it wouldn't make any sense to mix them though. I actually have this implemented, but I'll change it (oh, and the reason is because I'm not that good at math :) )
– juan
Oct 17, 2011 at 0:14