ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java实现判断一个经纬度坐标是否在一个多边形内(经自己亲测)

2021-06-02 23:52:38  阅读:488  来源: 互联网

标签:ps java 经纬度 Point Double isPtInPoly new public 亲测


1.在高德地图上绘制的多边形;经纬度逗号分隔格式;上面是用来方便存坐标的对象;下面是方法测试;直接复制代码即可运行

public class Point {
    
       private Double x;  
        private Double y;  
        public Point (Double x , Double y) {  
            this.x = x;  
            this.y = y;  
        }  
        public Double getX() {  
            return x;  
        }  
        public void setX(Double x) {  
            this.x = x;  
        }  
        public Double getY() {  
            return y;  
        }  
        public void setY(Double y) {  
            this.y = y;  
        }      
}
public class Test01 {
    
     public static void main(String[] args) {  
             //114.331951,30.64091#114.341049,30.610185#114.331436,30.588058#114.312038,30.56393#114.293498,30.558609#114.267922,30.563784#114.231185,30.57945#114.212303,30.601616#114.235649,30.626878#114.280624,30.646818#
         Map [] map=new Map[]{};
         Point[] ps = new Point[] { new Point(114.309914,30.599556),//114.309914,30.599556
                    new Point(114.295688,30.592879),//114.295688,30.592879
                    new Point(114.292812,30.587726), //114.292812,30.587726
                    new Point(114.292812,30.587726), //114.292812,30.587726
                    new Point(114.30058,30.580318),//114.30058,30.580318
                    new Point(114.303606,30.586959),//114.303606,30.586959
                    new Point(114.304534,30.594751),//114.304534,30.594751
                    new Point(114.30838,30.590131),//114.30838,30.590131
                    new Point(114.308651,30.584182),//114.308651,30.584182
                    new Point(114.304495,30.584015),//114.304495,30.584015
                    new Point(114.301301,30.578759),//114.301301,30.578759
                    new Point(114.309437,30.578528),//114.309437,30.578528
                    new Point(114.323282,30.592786)};//114.323282,30.592786
            Point n1 = new Point(114.303217,30.583553);  
            Point n2 = new Point(114.307336,30.597592);  
            Point n3 = new Point(114.286565,30.590056);  
            Point y1 = new Point(114.227342,30.587987);  
            Point y2 = new Point(120.1866 , 30.2672);  
            Point y4 = new Point(120.1869 , 30.2718);  
            System.out.println( "n1:" + isPtInPoly(n1.getX() , n1.getY() , ps));  
            System.out.println( "n2:" + isPtInPoly(n2.getX() , n2.getY() , ps));  
            System.out.println( "n3:" + isPtInPoly(n3.getX() , n3.getY() , ps));  
            System.out.println( "y1:" + isPtInPoly(y1.getX() , y1.getY() , ps));  
            System.out.println( "y2:" + isPtInPoly(y2.getX() , y2.getY() , ps));  
            System.out.println( "y4:" + isPtInPoly(y4.getX() , y4.getY() , ps));  
        }  
        public static boolean isPtInPoly (double ALon , double ALat , Point[] ps) {  
            int iSum, iCount, iIndex;  
            double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;  
            if (ps.length < 3) {  
                return false;  
            }  
            iSum = 0;  
            iCount = ps.length;  
            for (iIndex = 0; iIndex<iCount;iIndex++) {  
                if (iIndex == iCount - 1) {  
                    dLon1 = ps[iIndex].getX();  
                    dLat1 = ps[iIndex].getY();  
                    dLon2 = ps[0].getX();  
                    dLat2 = ps[0].getY();  
                } else {  
                    dLon1 = ps[iIndex].getX();  
                    dLat1 = ps[iIndex].getY();  
                    dLon2 = ps[iIndex + 1].getX();  
                    dLat2 = ps[iIndex + 1].getY();  
                }  
                // 以下语句判断A点是否在边的两端点的水平平行线之间,在则可能有交点,开始判断交点是否在左射线上  
                if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {  
                    if (Math.abs(dLat1 - dLat2) > 0) {  
                        //得到 A点向左射线与边的交点的x坐标:  
                        dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat) ) / (dLat1 - dLat2);  
                        // 如果交点在A点左侧(说明是做射线与 边的交点),则射线与边的全部交点数加一:  
                        if (dLon < ALon) {  
                            iSum++;  
                        }  
                    }  
                }  
            }  
            if ((iSum % 2) != 0) {  
                return true;  
            }  
            return false;  
        }  
    }  
    

 

标签:ps,java,经纬度,Point,Double,isPtInPoly,new,public,亲测
来源: https://blog.51cto.com/u_15242378/2848326

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有