1.判断某坐标 位置是否在多边形区域内
/**
* 判断当前位置是否在多边形区域内
* @param orderLocation 当前点
* @param partitionLocation 区域顶点
* @return
*/
public static boolean isInPolygon(String X,String Y,String partitionLocation){
double p_x =Double.parseDouble(X);
double p_y =Double.parseDouble(Y);
Point2D.Double point = new Point2D.Double(p_x, p_y);
List<Point2D.Double> pointList= new ArrayList<Point2D.Double>();
String[] strList = partitionLocation.split("\\],");
for (String str : strList){
String[] points = str.split(",");
double polygonPoint_x=Double.parseDouble(points[0]);
double polygonPoint_y=Double.parseDouble(points[1]);
Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x,polygonPoint_y);
pointList.add(polygonPoint);
}
return IsPtInPoly(point,pointList);
}
/**
* 判断点是否在多边形内,如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
* @param point 检测点
* @param pts 多边形的顶点
* @return 点在多边形内返回true,否则返回false
*/
public static boolean IsPtInPoly(Point2D.Double point, List<Point2D.Double> pts){
int N = pts.size();
boolean boundOrVertex = true; //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
int intersectCount = 0;//cross points count of x
double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
Point2D.Double p1, p2;//neighbour bound vertices
Point2D.Double p = point; //当前点
p1 = pts.get(0);//left vertex
for(int i = 1; i <= N; ++i){//check all rays
if(p.equals(p1)){
return boundOrVertex;//p is an vertex
}
p2 = pts.get(i % N);
if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){
p1 = p2;
continue;
}
if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){
if(p.y <= Math.max(p1.y, p2.y)){
if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){
return boundOrVertex;
}
if(p1.y == p2.y){
if(p1.y == p.y){
return boundOrVertex;
}else{//before ray
++intersectCount;
}
}else{
double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;
if(Math.abs(p.y - xinters) < precision){
return boundOrVertex;
}
if(p.y < xinters){
++intersectCount;
}
}
}
}else{
if(p.x == p2.x && p.y <= p2.y){
Point2D.Double p3 = pts.get((i+1) % N);
if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){
++intersectCount;
}else{
intersectCount += 2;
}
}
}
p1 = p2;
}
if(intersectCount % 2 == 0){//偶数在多边形外
return false;
} else { //奇数在多边形内
return true;
}
}
-
判断是否在多边形区域内
/** * 判断是否在多边形区域内 * * @param pointLon * 要判断的点的纵坐标 经 * @param pointLat * 要判断的点的横坐标 纬 * @param lon * 区域各顶点的纵坐标数组 * @param lat * 区域各顶点的横坐标数组 * @return */ public static boolean isScope(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 = 0.0, polygonPoint_y = 0.0; 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); } /** * 一个点是否在多边形内 * * @param point * 要判断的点的横纵坐标 * @param polygon * 组成的顶点坐标集合 * @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); }
3.判断该地理坐标是否在最大范围区域内
/** * 判断该地理坐标是否在最大范围区域内 * * @param pointLon * 要判断的点的纵坐标 * @param pointLat * 要判断的点的横坐标 * @param lon * 指定区域的纵坐标组成的数组 * @param lat * 指定区域的横坐标组成的数组 * @return */ private static boolean isInMaxArea(double pointLon, double pointLat, double[] lon, double[] lat) { // 获取区域横纵坐标最大值和最小值 double temp = 0.0; for (int i = 0; i < lon.length; i++) { for (int j = 0; j < lon.length - i - 1; j++) { if (lon[j] > lon[j + 1]) { temp = lon[j]; lon[j] = lon[j + 1]; lon[j + 1] = temp; } } } for (int i = 0; i < lat.length; i++) { for (int j = 0; j < lat.length - i - 1; j++) { if (lat[j] > lat[j + 1]) { temp = lat[j]; lat[j] = lat[j + 1]; lat[j + 1] = temp; } } } // 如果在最值组成的区域外,那肯定不在重点区域内 return (pointLon < lon[0] || pointLon > lon[lon.length - 1] || pointLat < lat[0] || pointLat > lat[lat.length - 1]); } /** * 判断坐标是否在重点区域内 * * @param pointLon * 要判断的点的纵坐标 * @param pointLat * 要判断的点的横坐标 * @param lon * 指定区域的纵坐标组成的数组 * @param lat * 指定区域的横坐标组成的数组 * @return */ private static boolean isInAccurateArea(double pointLon, double pointLat, double[] lon, double[] lat) { // 代表有几个点 int vertexNum = lon.length; boolean result = false; for (int i = 0, j = vertexNum - 1; i < vertexNum; j = i++) { // 满足条件,与多边形相交一次,result布尔值取反一次,奇数个则在区域内 if ((lon[i] > pointLon) != (lon[j] > pointLon) && (pointLat < (lat[j] - lat[i]) * (pointLon - lon[i]) / (lon[j] - lon[i]) + lat[i])) { result = !result; } } return result; }