What it says on the tin: how do I query for addresses in my Spanner database which are within a certain radius of a given long and lat?
For an example use case, let's say I have a database of restaurants and I'm looking for ones within ten miles of my apartment. I currently have the lat and long for each restaurant stored in the Address_geolng
and Address_geolat
fields as degrees. For the sake of simple data, we'll say I'm somehow living in the middle of the hot restaurant scene that is null island (e.g. 0,0).
A lot of databases have a built-in geography type or some type of prebuilt geodistance functionality, but I don't see either one in Spanner.
I've been trying to just brute force implement the Haversine formula in lieu of all else, but in all honesty my eyes are crossing here and either I'm unable to track down the relevant documentation for my use case or Spanner is missing a lot of things to help implement this more simply. (For example, it appears that their trig functions work solely in radians, but I don't see any reference anywhere to either a degree to radian conversion function or the ability to reference PI... there's gotta be something better than just grabbing ACOS(-1)
, I'm sure....)
So far the best effort I've got is
COS(0) * COS(DIV(ACOS(-1),180) * Address_geolat) *
SIN(DIV(DIV(ACOS(-1),180) * (Address1_geolng - 0)), 2) * SIN(DIV(DIV(ACOS(-1),180) * (Address1_geolng - 0), 2)) AS a FROM restaurants WHERE (3959 * 2 * ATAN2(SQRT(a), SQRT(1 - a)) <= 10)
Which I'm positive isn't even right -- my eyes are just crossing trying to sort through all of this.
Has anyone already developed a solution for this? What did you use?