GIS助手--PipUtil

本文介绍了一种用于GIS开发的方法,通过PipUtil工具判断一个点是否位于指定的多边形区域内。提供了两种实现方式:一种是通过计算梯度和截距,另一种则考虑了边界上的特殊情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PipUtil是用来判断一个点是否在多边形内部的,可以用于GIS开发,呵呵,说是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));
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值