【转】Java工具类 - 判断一个点是否在区域内
如何判断地球上的一个点是否在一个区域内呢?
思路如下:
1.判断是否在在圆形区域内可以通过点到圆心的距离小于半径来确定。
2.判断是否在矩形内可以利用jdk自带的java.awt.geom.Point2D,将输入的点的集合连接成线,继而利用其相关api即可确定。
下面即为一个完整的工具类整理。
public class MapUtils {
public static final class MapU{
public static final double EARTH_RADIUS = 6378.137;
public static final int N_2 = 2;
public static final int N_1000 = 1000;
public static final double N_180 = 180.0;
}
public static void main(String[] args) {
/* double pointLon=112.86788;
double pointLat=35.48655;
double centerLon=112.8619;
double centerLat=35.4858;
double radius=899.6044;
System.out.println("改点是否在圆内:"+isInCircle(pointLon,pointLat,centerLon,centerLat,radius));
*/
/* double[] lon={112.5709,112.5709,112.5873,112.5873};
double[] lat={37.8618,37.8683,37.8683,37.8618};
System.out.println("改点是否在形状内:"+isInPolygon(pointLon,pointLat,lon,lat));
/* double pointLon=112.61013;
double pointLat=37.80791;
double centerLon=112.53373;
double centerLat=37.82151;
System.out.println(getDistance(pointLon,pointLat,centerLon,centerLat)); */
}
/* *
* @description: // 判断是否在一个圆形内
* @param pointLon 判断点经度
* @param pointLat 判断点维度
* @param centerLon 圆心经度
* @param centerLat 圆心维度
* @param radius 半径
**/
public static boolean isInCircle(double pointLon, double pointLat, double centerLon, double centerLat, double radius) {
return getDistance(pointLon, pointLat, centerLon, centerLat) < radius;
}
/**
* 判断是否在多边形区域内
* @param pointLon 要判断的点的经度
* @param pointLat 要判断的点的维度
* @param lon 区域各顶点的经度数组
* @param lat 区域各顶点的维度数组
* @return
*/
public static boolean isInPolygon(double pointLon, double pointLat, double[] lon, double[] lat) {
// 将要判断的横纵坐标组成一个点
Point2D.Double point = new Point2D.Double(pointLon, pointLat);
// 将区域各顶点的横纵坐标放到一个点集合里面
List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
double polygonPoint_x , polygonPoint_y;
for (int i = 0; i < lon.length; i++) {
polygonPoint_x = lon[i];
polygonPoint_y = lat[i];
Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
pointList.add(polygonPoint);
}
return check(point, pointList);
}
/**
* 判断是否在多边形区域内
* @return
*/
private static boolean check(Point2D.Double point, List<Point2D.Double> polygon) {
java.awt.geom.GeneralPath peneralPath = new java.awt.geom.GeneralPath();
Point2D.Double first = polygon.get(0);
// 通过移动到指定坐标(以双精度指定),将一个点添加到路径中
peneralPath.moveTo(first.x, first.y);
polygon.remove(0);
for (Point2D.Double d : polygon) {
// 通过绘制一条从当前坐标到新指定坐标(以双精度指定)的直线,将一个点添加到路径中。
peneralPath.lineTo(d.x, d.y);
}
// 将几何多边形封闭
peneralPath.lineTo(first.x, first.y);
peneralPath.closePath();
// 测试指定的 Point2D 是否在 Shape 的边界内。
return peneralPath.contains(point);
}
/**
* 获取两个点间的距离
* @param lonA1 A1点的经度
* @param latA1 A1点的纬度
* @param lonA2 A2点的经度
* @param latA2 A2点的纬度
* @return
*/
private static double getDistance (double lonA1,double latA1,double lonA2,double latA2) {
// 单位(米)
double lon1 = lonA1* Math.PI /MapU.N_180;
double lat1 = latA1* Math.PI /MapU.N_180;
double lon2 = lonA2* Math.PI /MapU.N_180;
double lat2 = latA2* Math.PI /MapU.N_180;
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.pow(Math.sin(dlat / MapU.N_2),MapU.N_2) +
Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / MapU.N_2), MapU.N_2);
double c = MapU.N_2 * Math.asin(Math.sqrt(a));
return c * MapU.EARTH_RADIUS * MapU.N_1000;
}
}
原文链接
https://blog.csdn.net/bk120/article/details/109032885?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_baidulandingword-2&spm=1001.2101.3001.4242