Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Selecting all coordinates withing a distance

Selecting all coordinates withing a distance

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasephprubycom
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeroen De Dauw
    wrote on last edited by
    #1

    This little gem is used to construct the where condition for an SQL query that selects all coordinates within a certain distance from a point (in a PHP app). The coordinates are stored in the db in a single field, where the lat and lon are separated by a comma. The SQL is splitting on that and running a whole bunch of calculations. The table can be millions of entries big. I kid you not. $distance = "ROUND(((ACOS( SIN({$latitude} * PI()/180 ) * SIN(SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',1) * PI()/180 ) + COS({$latitude} * PI()/180 ) * COS(SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',1) * PI()/180 ) * COS(({$longitude} - SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',-1)) * PI()/180))*180/PI())*60*1.1515),6)"; $where = "{$distance} <= " . $dbs->addQuotes( "5" );

    Jeroen De Dauw
    Forums ; Blog ; Wiki

    C B U 3 Replies Last reply
    0
    • J Jeroen De Dauw

      This little gem is used to construct the where condition for an SQL query that selects all coordinates within a certain distance from a point (in a PHP app). The coordinates are stored in the db in a single field, where the lat and lon are separated by a comma. The SQL is splitting on that and running a whole bunch of calculations. The table can be millions of entries big. I kid you not. $distance = "ROUND(((ACOS( SIN({$latitude} * PI()/180 ) * SIN(SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',1) * PI()/180 ) + COS({$latitude} * PI()/180 ) * COS(SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',1) * PI()/180 ) * COS(({$longitude} - SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',-1)) * PI()/180))*180/PI())*60*1.1515),6)"; $where = "{$distance} <= " . $dbs->addQuotes( "5" );

      Jeroen De Dauw
      Forums ; Blog ; Wiki

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      It's probably immaterial, but what does the constant 1.1515 represent in the equation? :confused: The great thing about generated SQL is that you never have to maintain the SQL. The horror about generated SQL is that you have to maintain the generator. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

      J E B 3 Replies Last reply
      0
      • C Chris Meech

        It's probably immaterial, but what does the constant 1.1515 represent in the equation? :confused: The great thing about generated SQL is that you never have to maintain the SQL. The horror about generated SQL is that you have to maintain the generator. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

        J Offline
        J Offline
        Jeroen De Dauw
        wrote on last edited by
        #3

        Somebody manually wrote this, not auto generated...

        Jeroen De Dauw
        Forums ; Blog ; Wiki

        1 Reply Last reply
        0
        • C Chris Meech

          It's probably immaterial, but what does the constant 1.1515 represent in the equation? :confused: The great thing about generated SQL is that you never have to maintain the SQL. The horror about generated SQL is that you have to maintain the generator. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

          E Offline
          E Offline
          elchupathingy
          wrote on last edited by
          #4

          Chris Meech wrote: It's probably immaterial, but what does the constant 1.1515 represent in the equation? Confused Its the distance of one nautical mile.

          C D 2 Replies Last reply
          0
          • E elchupathingy

            Chris Meech wrote: It's probably immaterial, but what does the constant 1.1515 represent in the equation? Confused Its the distance of one nautical mile.

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            Thanks. It was a constant that didn't ring any bells for me. Now that I know it's for nautical miles, it will be a ship's bell that should go off next time. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

            1 Reply Last reply
            0
            • E elchupathingy

              Chris Meech wrote: It's probably immaterial, but what does the constant 1.1515 represent in the equation? Confused Its the distance of one nautical mile.

              D Offline
              D Offline
              David Skelly
              wrote on last edited by
              #6

              elchupathingy wrote:

              Its the distance of one nautical mile

              In case anyone is wondering about the relevance of a nautical mile, it's because a nautical mile is defined as one minute of arc around the Earth's circumference. So this calculation is converting minutes of arc into miles. According to a quick search around the interweb, a nautical mile is actually defined as 1.1508 miles, not 1.1515. So for large distances, this calculation is going to be slightly off. If you sail half way round the world, you'll be 8 miles away from where you think you are with this calculation.

              J 1 Reply Last reply
              0
              • C Chris Meech

                It's probably immaterial, but what does the constant 1.1515 represent in the equation? :confused: The great thing about generated SQL is that you never have to maintain the SQL. The horror about generated SQL is that you have to maintain the generator. :)

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #7

                It's actually not 1.1515, but 60*1.1515, i.e. 69.09. One degree corresponds to 69.09 miles (non-nautical). I found that out by doing the maths behind that formula. He converts lots of degree-coordinates into radian-coordinates by that *PI/180, then calculates the shortest distance along a great circle in radians (that's the spherical geometry equivalent of the Pythagoras formula for orthogonal triangles), converts that back to degrees along that circle, and then transforms that to miles; for kilometers use 111.1. Nonetheless, I do not understand why he uses 60*1.1515 instead of 69.09.

                1 Reply Last reply
                0
                • J Jeroen De Dauw

                  This little gem is used to construct the where condition for an SQL query that selects all coordinates within a certain distance from a point (in a PHP app). The coordinates are stored in the db in a single field, where the lat and lon are separated by a comma. The SQL is splitting on that and running a whole bunch of calculations. The table can be millions of entries big. I kid you not. $distance = "ROUND(((ACOS( SIN({$latitude} * PI()/180 ) * SIN(SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',1) * PI()/180 ) + COS({$latitude} * PI()/180 ) * COS(SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',1) * PI()/180 ) * COS(({$longitude} - SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',-1)) * PI()/180))*180/PI())*60*1.1515),6)"; $where = "{$distance} <= " . $dbs->addQuotes( "5" );

                  Jeroen De Dauw
                  Forums ; Blog ; Wiki

                  B Offline
                  B Offline
                  Bernhard Hiller
                  wrote on last edited by
                  #8

                  A full circle means 360 degrees or 2*PI radians. Consequently, I would suggest the original programmer replace all PI()/180 by 2*PI()/360. That makes things much easier to understand!

                  1 Reply Last reply
                  0
                  • J Jeroen De Dauw

                    This little gem is used to construct the where condition for an SQL query that selects all coordinates within a certain distance from a point (in a PHP app). The coordinates are stored in the db in a single field, where the lat and lon are separated by a comma. The SQL is splitting on that and running a whole bunch of calculations. The table can be millions of entries big. I kid you not. $distance = "ROUND(((ACOS( SIN({$latitude} * PI()/180 ) * SIN(SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',1) * PI()/180 ) + COS({$latitude} * PI()/180 ) * COS(SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',1) * PI()/180 ) * COS(({$longitude} - SUBSTRING_INDEX({$tablename}.{$fieldname}, ',',-1)) * PI()/180))*180/PI())*60*1.1515),6)"; $where = "{$distance} <= " . $dbs->addQuotes( "5" );

                    Jeroen De Dauw
                    Forums ; Blog ; Wiki

                    U Offline
                    U Offline
                    User 357305
                    wrote on last edited by
                    #9

                    The ugliest part is database structure. The coordinates are stored in the db in a single field, where the lat and lon are separated by a comma I would TALK to the programmer, but I would FIRE the database architect immediately. I did similar thing (if distances you deal with are not that big, you can do it without acos and sin, and even without SQRT in SQL code if you calculate distance*distance before you run the query). Yet it works horribly slow. I made it run very fast by indexing lat and long and finding all locations within the lat,long square, then cutting off everything outside of a circle... Now you see why I suggest to fire the database architect. You cannot use index on lat, long, so any code you write is doomed to work sluggish.

                    1 Reply Last reply
                    0
                    • D David Skelly

                      elchupathingy wrote:

                      Its the distance of one nautical mile

                      In case anyone is wondering about the relevance of a nautical mile, it's because a nautical mile is defined as one minute of arc around the Earth's circumference. So this calculation is converting minutes of arc into miles. According to a quick search around the interweb, a nautical mile is actually defined as 1.1508 miles, not 1.1515. So for large distances, this calculation is going to be slightly off. If you sail half way round the world, you'll be 8 miles away from where you think you are with this calculation.

                      J Offline
                      J Offline
                      Jeremy Hutchinson
                      wrote on last edited by
                      #10

                      While we're all dragging out our ocean navigation knowledge...

                      David Skelly wrote:

                      a nautical mile is defined as one minute of arc around the Earth's circumference

                      It's worth highlighting that this works at the equator and along meridians, but is not accurate if you are traveling east/west along say 45 degrees north lattitude. Another reason this calculation probably isn't going to be accurate is that the shortest distance between two points is an arc. The further you get from the equator the more curve there is in that arc. Of course if they are just trying to locate stores within 50 miles of your location, their math is probably good enough...

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups