Coordinates Algorithm – Calculating Square Coordinates from Center Point

algorithmcoordinates

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

Best Answer

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.