Querying for addresses within a given radius within SpannerCalculate distance between two latitude-longitude points? (Haversine formula)Cloud Spanner streaming vs non-streaming queries performance differenceCloud Spanner query performance regressionQuery a Array in google cloud spannerGoogle Spanner multiple queries in one round tripRecursive queries in Google Spanner?Query plan in SpannerInterpolating Query with Cloud Spanner in Node.jscopy data from one spanner db to an existing spanner dbGoogle Cloud Spanner: Long Running QueryFlyway with Spanner
Do I have an "anti-research" personality?
How to limit Drive Letters Windows assigns to new removable USB drives
How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?
Can I criticise the more senior developers around me for not writing clean code?
How to fry ground beef so it is well-browned
Why does Mind Blank stop the Feeblemind spell?
Is it idiomatic to construct against `this`
Relationship between strut and baselineskip
I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?
Alignment of various blocks in tikz
How to not starve gigantic beasts
a sore throat vs a strep throat vs strep throat
Is there really no use for MD5 anymore?
Critique of timeline aesthetic
Can I grease a crank spindle/bracket without disassembling the crank set?
Get consecutive integer number ranges from list of int
Was there a Viking Exchange as well as a Columbian one?
Does a large simulator bay have standard public address announcements?
How to pronounce 'c++' in Spanish
How would 10 generations of living underground change the human body?
How to write a column outside the braces in a matrix?
Rivers without rain
How to have a sharp product image?
Which big number is bigger?
Querying for addresses within a given radius within Spanner
Calculate distance between two latitude-longitude points? (Haversine formula)Cloud Spanner streaming vs non-streaming queries performance differenceCloud Spanner query performance regressionQuery a Array in google cloud spannerGoogle Spanner multiple queries in one round tripRecursive queries in Google Spanner?Query plan in SpannerInterpolating Query with Cloud Spanner in Node.jscopy data from one spanner db to an existing spanner dbGoogle Cloud Spanner: Long Running QueryFlyway with Spanner
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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?
google-cloud-spanner
add a comment |
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?
google-cloud-spanner
add a comment |
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?
google-cloud-spanner
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?
google-cloud-spanner
google-cloud-spanner
asked Mar 22 at 17:22
Amber B.Amber B.
492211
492211
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
So I am working on publishing a doc for this. You are correct that Spanner does not have geospatial support internally, but here are some tips:
1) don't query using haversine at the top level select - it means that you have to do a full table scan over all rows with complex calculations on each one, so will be very slow on large tables
2) start by calculating the corner coordinates of a bounding rectangle that has sides of 20 miles with your requested coordinates in the center.
3) query for addresses where the lat-long is inside your bounding box using simple >/< operators comparing the lat-long to the corner points.
As this is a simple query you can take advantage of secondary indexes on latitude and longitude to make your query much faster... (Take care at the poles and across 180° longitude!)
4) you now have a limited set of addresses that are approximately 20miles from your requested position (some are slightly further away) You can now filter these addresses by calculating the exact distance using either haversine or spherical law of cosine
This fine distance calculation / filtering can be done in SQL, but it may be easier to do it in your application where you have more math functions available and can use local variables to simplify things. As you only have a few rows to work with (due to the coarse filtering on the bounding box) this should be quick.
Here is a useful web page with easier to read formulas:
https://www.movable-type.co.uk/scripts/latlong.html
This is definitely helpful information! I'd love to see that doc once it's published, if there's some way I could trouble you to comment about it here when it's ready.
– Amber B.
Mar 22 at 18:40
1
Will do. No idea on timings though !
– RedPandaCurios
Mar 27 at 14:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304826%2fquerying-for-addresses-within-a-given-radius-within-spanner%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So I am working on publishing a doc for this. You are correct that Spanner does not have geospatial support internally, but here are some tips:
1) don't query using haversine at the top level select - it means that you have to do a full table scan over all rows with complex calculations on each one, so will be very slow on large tables
2) start by calculating the corner coordinates of a bounding rectangle that has sides of 20 miles with your requested coordinates in the center.
3) query for addresses where the lat-long is inside your bounding box using simple >/< operators comparing the lat-long to the corner points.
As this is a simple query you can take advantage of secondary indexes on latitude and longitude to make your query much faster... (Take care at the poles and across 180° longitude!)
4) you now have a limited set of addresses that are approximately 20miles from your requested position (some are slightly further away) You can now filter these addresses by calculating the exact distance using either haversine or spherical law of cosine
This fine distance calculation / filtering can be done in SQL, but it may be easier to do it in your application where you have more math functions available and can use local variables to simplify things. As you only have a few rows to work with (due to the coarse filtering on the bounding box) this should be quick.
Here is a useful web page with easier to read formulas:
https://www.movable-type.co.uk/scripts/latlong.html
This is definitely helpful information! I'd love to see that doc once it's published, if there's some way I could trouble you to comment about it here when it's ready.
– Amber B.
Mar 22 at 18:40
1
Will do. No idea on timings though !
– RedPandaCurios
Mar 27 at 14:06
add a comment |
So I am working on publishing a doc for this. You are correct that Spanner does not have geospatial support internally, but here are some tips:
1) don't query using haversine at the top level select - it means that you have to do a full table scan over all rows with complex calculations on each one, so will be very slow on large tables
2) start by calculating the corner coordinates of a bounding rectangle that has sides of 20 miles with your requested coordinates in the center.
3) query for addresses where the lat-long is inside your bounding box using simple >/< operators comparing the lat-long to the corner points.
As this is a simple query you can take advantage of secondary indexes on latitude and longitude to make your query much faster... (Take care at the poles and across 180° longitude!)
4) you now have a limited set of addresses that are approximately 20miles from your requested position (some are slightly further away) You can now filter these addresses by calculating the exact distance using either haversine or spherical law of cosine
This fine distance calculation / filtering can be done in SQL, but it may be easier to do it in your application where you have more math functions available and can use local variables to simplify things. As you only have a few rows to work with (due to the coarse filtering on the bounding box) this should be quick.
Here is a useful web page with easier to read formulas:
https://www.movable-type.co.uk/scripts/latlong.html
This is definitely helpful information! I'd love to see that doc once it's published, if there's some way I could trouble you to comment about it here when it's ready.
– Amber B.
Mar 22 at 18:40
1
Will do. No idea on timings though !
– RedPandaCurios
Mar 27 at 14:06
add a comment |
So I am working on publishing a doc for this. You are correct that Spanner does not have geospatial support internally, but here are some tips:
1) don't query using haversine at the top level select - it means that you have to do a full table scan over all rows with complex calculations on each one, so will be very slow on large tables
2) start by calculating the corner coordinates of a bounding rectangle that has sides of 20 miles with your requested coordinates in the center.
3) query for addresses where the lat-long is inside your bounding box using simple >/< operators comparing the lat-long to the corner points.
As this is a simple query you can take advantage of secondary indexes on latitude and longitude to make your query much faster... (Take care at the poles and across 180° longitude!)
4) you now have a limited set of addresses that are approximately 20miles from your requested position (some are slightly further away) You can now filter these addresses by calculating the exact distance using either haversine or spherical law of cosine
This fine distance calculation / filtering can be done in SQL, but it may be easier to do it in your application where you have more math functions available and can use local variables to simplify things. As you only have a few rows to work with (due to the coarse filtering on the bounding box) this should be quick.
Here is a useful web page with easier to read formulas:
https://www.movable-type.co.uk/scripts/latlong.html
So I am working on publishing a doc for this. You are correct that Spanner does not have geospatial support internally, but here are some tips:
1) don't query using haversine at the top level select - it means that you have to do a full table scan over all rows with complex calculations on each one, so will be very slow on large tables
2) start by calculating the corner coordinates of a bounding rectangle that has sides of 20 miles with your requested coordinates in the center.
3) query for addresses where the lat-long is inside your bounding box using simple >/< operators comparing the lat-long to the corner points.
As this is a simple query you can take advantage of secondary indexes on latitude and longitude to make your query much faster... (Take care at the poles and across 180° longitude!)
4) you now have a limited set of addresses that are approximately 20miles from your requested position (some are slightly further away) You can now filter these addresses by calculating the exact distance using either haversine or spherical law of cosine
This fine distance calculation / filtering can be done in SQL, but it may be easier to do it in your application where you have more math functions available and can use local variables to simplify things. As you only have a few rows to work with (due to the coarse filtering on the bounding box) this should be quick.
Here is a useful web page with easier to read formulas:
https://www.movable-type.co.uk/scripts/latlong.html
answered Mar 22 at 18:20
RedPandaCuriosRedPandaCurios
1,581818
1,581818
This is definitely helpful information! I'd love to see that doc once it's published, if there's some way I could trouble you to comment about it here when it's ready.
– Amber B.
Mar 22 at 18:40
1
Will do. No idea on timings though !
– RedPandaCurios
Mar 27 at 14:06
add a comment |
This is definitely helpful information! I'd love to see that doc once it's published, if there's some way I could trouble you to comment about it here when it's ready.
– Amber B.
Mar 22 at 18:40
1
Will do. No idea on timings though !
– RedPandaCurios
Mar 27 at 14:06
This is definitely helpful information! I'd love to see that doc once it's published, if there's some way I could trouble you to comment about it here when it's ready.
– Amber B.
Mar 22 at 18:40
This is definitely helpful information! I'd love to see that doc once it's published, if there's some way I could trouble you to comment about it here when it's ready.
– Amber B.
Mar 22 at 18:40
1
1
Will do. No idea on timings though !
– RedPandaCurios
Mar 27 at 14:06
Will do. No idea on timings though !
– RedPandaCurios
Mar 27 at 14:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304826%2fquerying-for-addresses-within-a-given-radius-within-spanner%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown