Determine longitudes and latitudes within a range

旧巷老猫 提交于 2019-11-30 07:46:47

Just in case you're using MySQL as your DBMS1, you may be interested in checking out the following presentation:

The author describes how you can use the Haversine Formula in MySQL to order spatial data by proximity and limit the results to a defined radius. More importantly, he also describes how to avoid a full table scan for such queries, using traditional indexes on the latitude and longitude columns.


1 Even if you aren't, this is still interesting and applicable.
2 There is also a pdf version of the presentation.

The calculation you want, i think, is called the great circle distance:

http://en.wikipedia.org/wiki/Great-circle_distance

Søren Randrup

You would need a distance function.

For SQL Server it would look something like this (note that distance is in kilometers),

    CREATE FUNCTION distance
    (
      @startLatitude float, 
      @startLongitude float, 
      @endLatitude float,
      @endLongitude float
    )
    RETURNS float
    AS
    BEGIN

      DECLARE @distance float;

      set @distance = 
        6371 * 2 * atn2(sqrt(power(sin(pi() / 180 * (@endLatitude - @startLatitude) / 2), 2) +
        power(cos(@startLatitude * pi() / 180), 2) *
        power(sin(pi() / 180 * (@endLongitude - @startLongitude) / 2), 2)),
        sqrt(1 - power(sin(pi() / 180 * (@endLatitude - @startLatitude) / 2), 2) +
        power(cos(@startLatitude * pi() / 180), 2) *
        power(sin(pi() / 180 * (@endLongitude - @startLongitude) / 2), 2)));
      RETURN @distance
    END
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!