判斷一個點是否在多邊形內部的GIS工具類:PipUtil

jopen 10年前發布 | 27K 次閱讀 PipUtil

PipUtil是用來判斷一個點是否在多邊形內部的,可以用于GIS開發。

    public class PipUtil {  
        /** 
         * @author Paul Hallett 10/05/2004 GIS Developer 
         * @return boolean true if point is outside a region, false if it is fixed 
         *         in the region 
         * @param X,Y 
         *            The cordinates of the point being located 
         * @param Xg,Yg 
         *            Double arrays listing the x,y pointlist of the region you are 
         *            searching in 
         * @param ncoord 
         *            is the amount of points there are in the search region 
         */  
        public boolean inPolygon(double X, double Y, double[] Xg, double[] Yg,  
                int ncoord) {  
            int count = 0;  
            double Gradient, Intercept, Yintercept;  
            double maxX, minX;  
            int i, j;  
            for (i = 0; i < ncoord; i++) {  
                j = (i + 1) % ncoord;  
                maxX = Xg[i] > Xg[j] ? Xg[i] : Xg[j];  
                minX = Xg[i] < Xg[j] ? Xg[i] : Xg[j];  
                if (X < maxX && X >= minX) {  
                    Gradient = (Yg[i] - Yg[j]) / (Xg[i] - Xg[j]);  
                    Intercept = Yg[i] - Gradient * Xg[i];  
                    Yintercept = X * Gradient + Intercept;  
                    if (Yintercept > Y) {  
                        count++;  
                    }  
                }  
            }  
            return (count % 2 == 1);  
        }  

        public static boolean isPointInsidePolygon(double x,double y, double[] xpoly,  
                double[] ypoly) {  
            int i1;  
            boolean isInside = false;  
            double X = x;  
            double Y = y;  
            for (int i = 0; i < xpoly.length - 1; i++) {  
                i1 = (i % xpoly.length) + 1; // modulus  
                if (ypoly[i] == ypoly[i1])  
                    if (Y == ypoly[i])  
                        if (!(X < xpoly[i] ^ X > xpoly[i1]))  
                            return true;  

                // If point is on the boundary return true  
                if (X == xpoly[i] | X == xpoly[i1]) {  
                    return true;  
                } else {  
                    if (!(Y < ypoly[i] ^ Y >= ypoly[i1])) {  
                        /* point is within y limits */  
                        double exp = (X - xpoly[i] - (Y - ypoly[i])  
                                * (xpoly[i1] - xpoly[i]) / (ypoly[i1] - ypoly[i]));  
                        if (exp < 0)  
                            isInside = !isInside;  
                        else if (exp == 0)  
                            return true; // point is on boundary  
                    }  
                }  
            }  
            return isInside;  
        }  

        public static void main(String []args){  
            double x = 113.678316;  
            double y = 34.781784;  
            double[] xp = {113.658912,113.676878,113.671632,113.659918,113.652444};  
            double[] yp = {34.793227,34.789373,34.780301,34.77953,34.784333};  
            System.out.println(isPointInsidePolygon(x,y,xp,yp));  
        }  
    }  

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!