Java – how to define whether the determination point is in the area lat, long?

I have an area defined by a set of geographical locations. I need to know whether a coordinate is in this area

public class Region{
    List<Coordinate> boundary;

}

public class Coordinate{

    private double latitude;
    private double longitude;

}

public static boolean isInsideRegion(Region region,Coordinate coordinate){


}

Solution

You can apply the point in polygon algorithm in computational geometry problems

Paul Bourke wrote four algorithms in C language. You can see the code here There is an adaptation of Java in the processing forum in case you can't use Java 7:

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;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>