Sorting results on a mysql table along longitude and then latitude, NOT a circular radius

StackOverflow https://stackoverflow.com/questions/4209051

  •  25-09-2019
  •  | 
  •  

Question

Hey all... I've been tasked with a global search project. They want to search a database of pictures, all with long/lat references storied per image in a mysql DB.

However, their search criteria is a little specific.

A simplified table structure is: id INT auto inc primary imagePath varchar 255 long DECIMAL 12,7 lat DECIMAL 12,7

I need to provide the SQL statement with a start long/lat position. They then want to receive 34 images, going south from their initial long reference.. once they get to -90 (the bottom of the earth) to pop over 1 degree to the right/East and search up to the north to 90 and repeat until 34 images are found.

What's confusing me is how to calculate the minus to plus changes.. ensuring that we loop up and down along the earths 'long' reference.

Has anyone done this before..? I'd LOVE to do it in a single query... (I've seen how one can fetch results based on a circle radius, but my client.. bless em.. doesn't want to do that)

Ok, back to google!!

Thoughts...

Tris...

Was it helpful?

Solution

Inputs are $the_lat and $the_long

SELECT * 
FROM table
WHERE long > $the_long
OR ( long = $the_long AND lat >= $the_lat)
ORDER BY long ASC, lat ASC
LIMIT 34

As is, it's a uni-directional search -- it won't wrap around to longs < $the_long.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top