-1

So I am trying to understand the decimal longitude and latitude system and I was wondering how they are being quantified. I have been using a Long Lat Finder but unable to understand how these 2 values are quantified.

My goal is given long-lat A, for example (37.349852, -122.023752), I want to design a database query that fetches all the long-lats that are within a 1-mile distance using <= and >= for comparing the long and lat values. Is this possible?

Edit: Ultimately, I want to design an efficient algorithm that compares long-lat at the database level so that given your current location, only the markers placed into the map that are 1 mile within your current location are identified. Imagine 1000s of long-lats in the database and determine how to narrow down the search so that it isn't computationally expensive to determine the markers that are X miles away from you.

btramisetty
  • 119
  • 1
  • 9
  • You'd need to convert the "1 mile" value to long/lat somehow like described [here](http://stackoverflow.com/a/1253545/515948) – Laur Ivan Jan 05 '17 at 07:33
  • 1
    Possible duplicate of [Simple calculations for working with lat/lon + km distance?](http://stackoverflow.com/questions/1253499/simple-calculations-for-working-with-lat-lon-km-distance) – Laur Ivan Jan 05 '17 at 07:33
  • Hi, I'm not trying to simply calculate the distance here. My overall goal is given 1000s (or even millions) of long-lats in my database, is there a way to narrow the search so that the distance equation isn't computed between every single long-lat and the user's current location? Any way to filter out a significant subset of the long lats in my database where 100,000 stored in them comes down to computing distances between 1000 only or something? – btramisetty Jan 05 '17 at 07:40
  • What database? PostgreSQL, Mongo, and MySQL would all be entirely different in this regard. – Chris Travers Jan 05 '17 at 09:34
  • I'd go the other way around: compute the bounding box from user's loong/lat and use the bounds in your query. – Laur Ivan Jan 05 '17 at 13:14

1 Answers1

0

Instead of using a "radius", limit your results by using "square" boundary. For a rectangle with side A (A = 2d, where d is the desired distance), all points with long and lat closer to the long and lat of the center than A/(2*R) (keep in mind this formula gives radians, you need to convert them) will belong to the rectangle. R is the Earth radius. This filter will require calculating the min and max angles only once for the entire scan and can benefit of indexes on lat/long.

If you like, you can further filter only the points in the given "radius"/distance by calculating the distance from the center for the points returned by the first filter. With this approach you will compute the distance only for the points that are bounded by the square. This will limit the distance calculation by a factor of ~1000

Further improvement could be to use two squares - one bounding the circle outside, one bounded by the circle. Points from the inner square are "good". Compute the distance to the center only for the points in the range between the two squares. With this improvement you will further limit the distance calculation by a factor of 10-100.

Ivan Georgiev
  • 1,115
  • 7
  • 11