0

I've a database (MySQL) of geographic (lat, long) points and I need to query a list of points from one rectangle (which have 2 coordinates for each vertex). Is there any query or algorithm to resolve this problem fast.

Thanks.

Fabrizio A
  • 3,072
  • 5
  • 24
  • 35

1 Answers1

1

assuming that your lat/long points are indexed, then any SQL query will be pretty fast

SELECT point.lat, point.long FROM point where rect1.lat <= point.lat and point.lat <= rect2.lat and rect1.long <= point.long and point.long <= rect2.long

rect1.lat/rect1.long and rect2.lat/rect2.long are the lat/long of the north western most and south eastern most location.

I am hoping that you are storing lat/long for both your Points and rectangles as positive and negative (rather than E/W N/S) - if you have then as E/W and N/S the query will be much more complicated.

This also assumes that the rectangle is such that the entire edge track along a line of lattitude/longtitude.

To be honest this one is so simple I guess I am missing something....

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
  • This is a good answer. I've posted this question because I thought there is some geolocation framework or a special algorithm to speed up the query. – Fabrizio A Sep 11 '14 at 21:31
  • but you aren't asking for geo-location (not really), you just asking if number a fits between numbers b & c, SQL is great for that with the right indexes. If you had asked the question with geographic points (towns, mountains lakes etc - and you wanted to identify if a specific relationship between them without knowing (in advance their lat/long) - then that is more of a geographic framework type problem. – Tony Suffolk 66 Sep 11 '14 at 21:43
  • Yes, maybe I have overrated the problem. Thanks! – Fabrizio A Sep 11 '14 at 21:49