java 坐标系运算 判断一个地理坐标是否在电子围栏 圆、矩形、多边形区域内

转载自:https://blog.csdn.net/Deepak192/article/details/79402694

测试没问题,我用的是原始坐标;要注意的是坐标转换问题,要看当前是属于什么坐标系

1     /**
2      * 地球半径
3      */
4     private static double EARTH_RADIUS = 6378138.0;
5     
6     private static double rad(double d)
7     {    
8         return d * Math.PI / 180.0;
9     }
 1    /**
 2     * 计算是否在圆上(单位/千米)
 3     * 
 4     * @Title: GetDistance 
 5     * @Description: TODO() 
 6     * @param radius 半径
 7     * @param lat1  纬度
 8     * @param lng1  经度
 9     * @return    
10     * @return double   
11     * @throws
12     */
13     public static boolean isInCircle(double radius,double lat1, double lng1, double lat2, double lng2)
14     {    
15         double radLat1 = rad(lat1);    
16         double radLat2 = rad(lat2);    
17         double a = radLat1 - radLat2;    
18         double b = rad(lng1) - rad(lng2);    
19         double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));    
21         s = s * EARTH_RADIUS;    
22         s = Math.round(s * 10000) / 10000;    
23         if(s > radius) {//不在圆上
24             return false;
25         }else {
26             return true;
27         }
28     }
 1   /**
 2     * 是否在矩形区域内
 3     * @Title: isInArea 
 4     * @Description: TODO() 
 5     * @param lat 测试点经度
 6     * @param lng 测试点纬度
 7     * @param minLat 纬度范围限制1 
 8     * @param maxLat 纬度范围限制2 
 9     * @param minLng 经度限制范围1 
10     * @param maxLng 经度范围限制2 
11     * @return    
12     * @return boolean   
13     * @throws
14     */
15     public static boolean isInRectangleArea(double lat,double lng,double minLat,
16             double maxLat,double minLng,double maxLng){  
17         if(isInRange(lat, minLat, maxLat)){//如果在纬度的范围内  
18             if(minLng*maxLng>0){
19                 if(isInRange(lng, minLng, maxLng)){  
20                     return true;  
21                 }else {  
22                     return false;  
23                 }  
24             }else {
25                 if(Math.abs(minLng)+Math.abs(maxLng)<180){
26                     if(isInRange(lng, minLng, maxLng)){  
27                         return true;  
28                     }else {  
29                         return false;  
30                     }  
31                 }else{
32                     double left = Math.max(minLng, maxLng);
33                     double right = Math.min(minLng, maxLng);  
34                     if(isInRange(lng, left, 180)||isInRange(lng, right,-180)){  
35                         return true;  
36                     }else {  
37                         return false;  
38                     }  
39                 }  
40             }  
41         }else{  
42             return false;  
43         }  
44     } 
 1   /**
 2     * 判断是否在经纬度范围内 
 3     * @Title: isInRange 
 4     * @Description: TODO() 
 5     * @param point
 6     * @param left
 7     * @param right
 8     * @return    
 9     * @return boolean   
10     * @throws
11     */
12     public static boolean isInRange(double point, double left,double right){  
13         if(point>=Math.min(left, right)&&point<=Math.max(left, right)){  
14             return true;  
15         }else {  
16             return false;  
17         }  
18     }
 1   /**
 2     * 判断点是否在多边形内 
 3     * @Title: IsPointInPoly 
 4     * @Description: TODO() 
 5     * @param point 测试点
 6     * @param pts 多边形的点 
 7     * @return    
 8     * @return boolean
 9     * @throws
10     */
11     public static boolean isInPolygon(Point2D.Double point, List<Point2D.Double> pts){  
12         
13         int N = pts.size();  
14         boolean boundOrVertex = true; 
15         int intersectCount = 0;//交叉点数量  
16         double precision = 2e-10; //浮点类型计算时候与0比较时候的容差  
17         Point2D.Double p1, p2;//临近顶点  
18         Point2D.Double p = point; //当前点  
19           
20         p1 = pts.get(0);      
21         for(int i = 1; i <= N; ++i){      
22             if(p.equals(p1)){  
23                 return boundOrVertex;
24             }  
25               
26             p2 = pts.get(i % N);           
27             if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){                  
28                 p1 = p2;   
29                 continue;
30             }  
31             
32             //射线穿过算法
33             if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){
34                 if(p.y <= Math.max(p1.y, p2.y)){               
35                     if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){  
36                         return boundOrVertex;  
37                     }  
38                       
39                     if(p1.y == p2.y){                   
40                         if(p1.y == p.y){
41                             return boundOrVertex;  
42                         }else{
43                             ++intersectCount;  
44                         }   
45                     }else{                     
46                         double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;                       
47                         if(Math.abs(p.y - xinters) < precision){ 
48                             return boundOrVertex;  
49                         }  
50                           
51                         if(p.y < xinters){
52                             ++intersectCount;  
53                         }   
54                     }  
55                 }  
56             }else{            
57                 if(p.x == p2.x && p.y <= p2.y){                 
58                     Point2D.Double p3 = pts.get((i+1) % N);                 
59                     if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){
60                         ++intersectCount;  
61                     }else{  
62                         intersectCount += 2;  
63                     }  
64                 }  
65             }              
66             p1 = p2;
67         }  
68         if(intersectCount % 2 == 0){//偶数在多边形外  
69             return false;  
70         } else { //奇数在多边形内  
71             return true;
72         } 
73     }

猜你喜欢

转载自www.cnblogs.com/songxiaotong/p/10852615.html