1
votes

I am trying to develop a "divide and conquer" algorithm to determine squares of latitude and longitude.

I wish to start with an 800 mile x 800 mile box around the UK. Then divide this into four squares, and then recursively repeating this process until the squares are 25 miles square. This will then be used to filter out unwanted results from a proximity search in a database of locations.

However, the algorithm I have which applies an offset to a lat/long (see below code), is giving me problems. I have tried other ones, too, but with much the same results. I think it may be my understanding of applying a coordinate system to a sphere.

I would expect that with a starting point, moving east 800 miles, then moving west 800 miles would result in being returned to (approx) the same location. However, this is not the case:

var coord = 
{
    lat : 60.678413562308236, 
    lng : -12.5
};

// Apply a distance and bearing to a set of coords.
var addOffset = function(coord, distance, angle)
{
    // Earth Radius 
  //var radius = 6378.14; // KM
    var radius = 3958.8; // Miles

  // Degrees to Radians
  var lat = coord.lat * (Math.PI/180);
  var lng = coord.lng * (Math.PI/180);
  var bearing = angle * (Math.PI/180);

  var lat2 = Math.asin(Math.sin(lat)*Math.cos(distance/radius) + Math.cos(lat)*Math.sin(distance/radius)*Math.cos(bearing));
  var lng2 = lng + Math.atan2(Math.sin(bearing)*Math.sin(distance/radius)*Math.cos(lat), Math.cos(distance/radius)-Math.sin(lat)*Math.sin(lat2));

  // back to degrees
  lat2 = lat2 * (180/Math.PI);
  lng2 = lng2 * (180/Math.PI);

   return { lat : lat2, lng : lng2};
}

console.log("start");
console.log(coord);
console.log("Move 800 miles east");
coord = addOffset(coord, 800, 90.0) // East
console.log(coord);
console.log("Move 800 miles west");
coord = addOffset(coord, 800, 270.0) // West
console.log(coord);

Further, when "walking" an 800 mile square around the UK, and plotting the points on a Google Map, I get big problems! The below is starting north-west, going south 800 miles, then going east 800 miles, then north 800 miles, then west 800 miles. As you can see, it's gone quite wrong:

Google Maps "Square"

Here's a fiddle (couldn't work out how to get the map working on a code snippet here):

https://jsfiddle.net/leetaylor/aoxnsqeu/6/

Could anyone explain where I'm going wrong? Is it possible to divide up geography into squares using lat/longs?

1
It does make things difficult that the Earth isn't flat. You can choose 2 lines of lat and long around UK, but they cannot form a square. i.e. the sides will not be the same lengthsstark
@stark It's not a massive problem not having a traditional square. However, I do need the "shape" to have a target width and height so that they can be included/excluded from being "close" to a chosen lat/long.Lee Taylor
I haven't looked in depth, but even before you get to the map something is very wrong in the code snippet: Moving east and west shouldn't change your latitudeAakashM
@AakashM I agree with you. However, all the algorithms that I found exhibited this behaviour.Lee Taylor

1 Answers

1
votes

The problem is that your addOffset doesn't do what you think it does. Quoting the source site

Given a start point, initial bearing, and distance, this will calculate the destina­tion point and final bearing travelling along a (shortest distance) great circle arc.

My emphasis. So when you say you want an initial bearing of East, you aren't calculating where you end up if you go east; instead, you're calculating where you end up if you follow the great circle that going east to start with puts you on. If you start from a point in the northern hemisphere and head east or west using this rule, you will curve south towards the equator, exactly as you can see happening in your map.

Note that the due-north and due-south lines are perfect, because lines of longitude are great circles.

Now, since you just want lat-long 'squares', you don't need trigonometry at all - just move along lines of latitude and longitude. This means that the top and bottom edge of each 'square' won't actually be the same length, but at temperate latitudes like the UK it shouldn't be too bad.