关于指定的经纬度是否落在多边形内

这个想法算法就是判断一个点向左的射线跟一个多边形的交叉点有几个,如果结果为奇数的话那么说明这个点落在多边形中,反之则不在。直接贴代码吧


A:


B:


C:


D:


E:


no1:


no2:


y1:


y2:


以上的ABCDE,分别是以下数组里面的数据

Point[] ps = new Point[] { new Point(120.2043 , 30.2795), new Point(120.2030 , 30.2511), new Point(120.1810 , 30.2543), new Point(120.1798 , 30.2781), new Point(120.1926,30.2752) };


实际生产环境的贴图:



package com.cmcc.monitor.test;

public class GisTest {

	public static void main(String[] args) {
		Point[] ps = new Point[] { new Point(120.2043 , 30.2795), new Point(120.2030 , 30.2511), new Point(120.1810 , 30.2543), new Point(120.1798 , 30.2781), new Point(120.1926,30.2752) };
		Point n1 = new Point(120.1936 , 30.2846);
		Point n2 = new Point(120.1823 , 30.2863);
		Point n3 = new Point(120.2189 , 30.2712);
		Point y1 = new Point(120.1902 , 30.2712);
		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;
	}
}


package com.cmcc.monitor.test;

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;
	}
    
}



判断一个经纬度是否指定多边形区域内,通常需要使用以下步骤: 1. 将多边形区域的经纬度坐标点转换为一个Polygon对象。 2. 创建一个Point对象,该对象表示输入的经纬度值。 3. 使用Polygon对象的contains方法检查Point对象是否多边形内。 以下是一个Java代码示例,用于判断一个经纬度是否指定多边形区域内: ```java public boolean isWithinPolygon(double lat, double lon, List<Double> polyLats, List<Double> polyLons) { // 将多边形区域的经纬度坐标点转换为Polygon对象 Coordinate[] coordinates = new Coordinate[polyLats.size()]; for (int i = 0; i < polyLats.size(); i++) { coordinates[i] = new Coordinate(polyLons.get(i), polyLats.get(i)); } GeometryFactory geometryFactory = new GeometryFactory(); Polygon polygon = geometryFactory.createPolygon(coordinates); // 创建一个Point对象 Point point = geometryFactory.createPoint(new Coordinate(lon, lat)); // 使用Polygon对象的contains方法检查Point对象是否多边形内 if (polygon.contains(point)) { // 经纬度多边形内 return true; } else { // 经纬度不在多边形内 return false; } } ``` 在这个示例中,我们首先将多边形区域的经纬度坐标点转换为一个Polygon对象。然后,我们创建了一个Point对象,该对象表示输入的经纬度值。最后,我们使用Polygon对象的contains方法检查Point对象是否多边形内。如果在多边形内,则返回true,否则返回false。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值