Analysis of geo-fencing algorithm
http://www.cnblogs.com/LBSer/p/4471742.html
Geo-fencing (geo-fencing) is an application of lbs, which uses a virtual fence to surround a virtual geographic boundary that can receive automatic notifications and warnings when a cell phone enters, leaves a particular geographic area, or moves within the region. As shown, suppose there are three malls on the map, and when the user enters a shopping mall, the phone automatically receives the coupon push message sent by the corresponding store. Geo-fence application is very extensive, today's mobile internet major apps such as the United States, the public, hand-scouring and so can see its application figure.
Figure 1 Geo Fence
The core issue of geo-fencing is to determine whether a user falls inside a polygon fence . This article will introduce the solutions that are commonly used in practical applications.
1 How to tell if a point is inside a polygon
Geo-fencing is generally a polygon, how to determine the point inside the polygon? The Ray method can be used to determine whether a point is inside a polygon. As shown, draw a ray along the x-axis from that point, in order to determine the intersection of the Ray and each edge, and count the number of intersections, if the number of intersections is odd, then within the Polygon (3 intersection), if the focal number is even, then outside, the ray method for convex and non-convex polygon, the complexity is O (n), the other N is the number of sides. Source can be referenced (http://alienryderflex.com/polygon/)
Figure 2 X-ray method to determine the point inside and outside the polygon
When the number of geo-fenced polygons is small, we can traverse each polygon (the brute-force traversal method) in turn, and then use the Ray method to judge it, which is also very efficient. And when the number of polygons, such as 100,000 polygons, this time need to perform 100,000-ray method, response time of 3.9 seconds, which is almost intolerable in the Internet application. The following table is my simple test, polygon edge number is 7.
Table 1 X-ray method performance test
2 R-Tree index accelerated judgment
The reason for the inefficiency of the brute force traversal method is that it is judged by the Ray method with each polygon, so our optimization idea is very direct, first of all the coarse sieve, find the small number of polygons that meet the condition, and then use the Ray method to judge the polygon after coarse sieve, so the number of the Ray method is greatly reduced and the efficiency can be greatly The solution to the R-tree is described below.
1) Outsource rectangle to represent polygon
Because polygon shapes are different, we need to approximate the polygons in a uniform way, and the simplest way to do this is to use the smallest outsourced rectangle to represent the polygon.
Figure 3 Minimum Outsourced rectangle (MBR) expression polygon
2) Set up R-Tree index for the minimum outsourced rectangle
Figure 4 R-Tree index of the smallest outsourced rectangle
3) Query
A) The R tree is the first to quickly determine whether the user's location (coarse red dot) is covered by the outsourced rectangle (Figure 5, the red dot represents the user location);
b) If not covered by any of the outsourced rectangles, the return is not within the geo-fenced polygon;
c) If the outer rectangle is covered, further judgment is needed to determine if the polygon inside the rectangle is outsourced, using the Ray method mentioned above (Figure 2).
Figure 5 R-Tree Query example
3 Practice
Online application service has 300,000 geo-fence polygons, by building R-Tree index in memory, the average response time of online real-time geo-fence query is within 1ms, and the response time of brute-force query is about 9 seconds.
Spatial index related blog posts:
Spatial index: Why spatial indexes are needed
The spatial index of the layman: 2
Geohash Core Principle Analysis
Geo-fencing algorithm analysis (geo-fencing)