我有一个由一组地理位置定义的区域,我需要知道一个坐标是否在这个区域内
public class Region{ List<Coordinate> boundary; } public class Coordinate{ private double latitude; private double longitude; } public static boolean isInsideRegion(Region region,Coordinate coordinate){ }
解决方法
您可以应用
Computational Geometry问题中的
Point in polygon算法.
Paul Bourke用C语言编写了四种算法,你可以看到代码here.在Processing Forum中有一个对Java的改编,以防万一你不能使用Java7:
public class RegionUtil { boolean coordinateInRegion(Region region,Coordinate coord) { int i,j; boolean isInside = false; //create an array of coordinates from the region boundary list Coordinate[] verts = (Coordinate)region.getBoundary().toArray(new Coordinate[region.size()]); int sides = verts.length; for (i = 0,j = sides - 1; i < sides; j = i++) { //verifying if your coordinate is inside your region if ( ( ( (verts[i].getLongitude() <= coord.getLongitude()) && (coord.getLongitude() < verts[j].getLongitude()) ) || ( (verts[j].getLongitude() <= coord.getLongitude()) && (coord.getLongitude() < verts[i].getLongitude()) ) ) && (coord.getLatitude() < (verts[j].getLatitude() - verts[i].getLatitude()) * (coord.getLongitude() - verts[i].getLongitude()) / (verts[j].getLongitude() - verts[i].getLongitude()) + verts[i].getLatitude()) ) { isInside = !isInside; } } return isInside; } }