Location Calculation Based on longitude and latitude

Source: Internet
Author: User
Tags asin

Http://tech.idv2.com/2011/06/17/location-search/

Shortest distance formula of the sphere

For details about the distance calculation formula between any two points on the sphere, refer to the following articles on Wikipedia.

  • Great-circle distance
  • Haversine Formula

It is worth mentioning that the haversine formula is recommended in Wikipedia, because the great-circle distance formula uses a large number of Cosine functions, while the distance between two points is very short (for example, the distance between two points on the earth surface is several hundred meters), the cosine function will produce 0. 999... will lead to a large rounding error. Haversine uses a sine function to maintain sufficient valid numbers even if the distance is small. This problem exists when using a trigonometric table in the past, but it has been verified that the two formulas are not much different when using a computer. For safety, the haversine formula is used here.

Where

  • R is the earth's radius, with an average value of 6371 km;
  • 1, 2 represents the latitude of two points;
  • △λ represents the difference between two points of longitude.
Distance calculation function

The following is a function used to calculate the distance between two points (lat0, LNG)-(LAT1, lng1) on the sphere.

From math import sin, asin, cos, radians, FABS, sqrtearthur _radius = 6371 # average Earth radius, 6371 kmdef hav (theta): S = sin (theta/2) return S * sdef get_distance_hav (lat0, lng0, LAT1, lng1): "Use haversine formula to calculate the distance between two points on the sphere. "# Converts longitude and latitude to radians lat0 = radians (lat0) LAT1 = radians (LAT1) lng0 = radians (lng0) lng1 = radians (lng1) dlng = FABS (lng0-lng1) dlat = FABS (lat0-LAT1) H = hav (dlat) + cos (lat0) * Cos (LAT1) * hav (dlng) distance = 2 * earth_radius * asin (SQRT (H) return distance
Range Search Algorithm

It is important to search for locations in a large geographic database. However, we need to search for nearby locations, such as the coordinates (39.91, 116.37). What is the location within 500? The search condition is the distance between the Location Coordinate and the current coordinate. Obviously, the index cannot be applied.

In another way: first, calculate the coordinate range in the range of "500 meters near the given coordinate. Although it is a circle, we can first find the external square of the circle, and then use the latitude and longitude range of the square to search for the database.

, The red part is the required search range, and the green part is the actual search range.

First, we need to find the boundary between the East and the West. In the haversin formula, if the formula is? 1 =? 2, you can obtain

The Python code is

Dlng = 2 * asin (sin (distance/(2 * earth_radius)/cos (LAT) dlng = degrees (dlng) # converts radians to degrees

Then, obtain the range boundary of the north and south sides. In the haversin formula, make △λ = 0.

The Python code is

Dlat = distance/earth_radiusdlng = degrees (dlat) # converts radians to degrees.

In this way, based on the coordinates of the current vertex, we can obtain that the search range is

left-top    : (lat + dlat, lng - dlng)right-top   : (lat + dlat, lng + dlng)left-bottom : (lat - dlat, lng - dlng)right-bottom: (lat - dlat, lng + dlng)

Then, use this range to construct an SQL statement to implement Range Query:

SELECT * FROM place WHERE lat > lat1 AND lat < lat2 AND lng > lng1 AND lng < lng2;

Creating an index on the lat and LNG columns can improve the efficiency of range query to a certain extent.

However, the queried location is within the Square range, and the distance between some results and the current point may exceed the given distance. If the requirements are strict, you can traverse the results, calculate the distance from the current point, and filter out the results that do not meet the requirements.

Summary

The nearby location search condition is distance, while the database generally only stores the longitude and latitude of the location, so you cannot directly query the location. In this article, distance is converted to the latitude and longitude range, and the query efficiency is improved by using the index on the latitude and longitude.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.