public static boolean isInPolygon(double pointLon, double pointLat,List lon,List lat) {// List lon,List 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.size(); i++) {
//System.out.println(lon.get(i));
//System.out.println(lat.get(i));
polygonPoint_x = lon.get(i);
polygonPoint_y = lat.get(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);
}