MarioC
MarioC

Reputation: 3228

Java - How to check if a lat/lng coords are inside a polygon

I want to check if a given point on a map (with its latitude and longitude) is inside a certain polygon. I have the vertex coords (in lat/long) of the polygon.

I thought of creating a Polygon and check if point is inside, but it gives me that the point is always outside... Maybe the polygon does not work with georeferential coords?

Double[] xCoords = {40.842226, 40.829498, 40.833394, 40.84768, 40.858716}
Double[] yCoords = {14.211753, 14.229262, 14.26617, 14.278701,  14.27715}
Double[] myPoint = {40.86141, 14.279932};


Path2D myPolygon = new Path2D.Double();
            myPolygon.moveTo(xCoords[0], yCoords[0]); //first point
            for(int i = 1; i < xCoords.length; i ++) {
                myPolygon.lineTo(xCoords[i], yCoords[i]); //draw lines
            }
            myPolygon.closePath(); //draw last line

               if(myPolygon.contains(myPoint{0}, myPoint{1})) {
                //it's inside;
}

This is how it looks like in google maps

enter image description here

It always return false... but the point it's inside the polygon...

Upvotes: 2

Views: 4606

Answers (2)

sajeeth
sajeeth

Reputation: 77

public class CoordinatesDTO {


private Long id;


private double latitude;


private double longnitude;

}

public static boolean isLocationInsideTheFencing(CoordinatesDTO location, List<CoordinatesDTO> fencingCoordinates) { //this is important method for Checking the point exist inside the fence or not.
    boolean blnIsinside = false;

    List<CoordinatesDTO> lstCoordinatesDTO = fencingCoordinates;

    Path2D myPolygon = new Path2D.Double();
    myPolygon.moveTo(lstCoordinatesDTO.get(0).getLatitude(), lstCoordinatesDTO.get(0).getLongnitude()); // first
                                                                                                        // point
    for (int i = 1; i < lstCoordinatesDTO.size(); i++) {
        myPolygon.lineTo(lstCoordinatesDTO.get(i).getLatitude(), lstCoordinatesDTO.get(i).getLongnitude()); // draw
                                                                                                            // lines
    }
    myPolygon.closePath(); // draw last line

    // myPolygon.contains(p);
    Point2D P2D2 = new Point2D.Double();
    P2D2.setLocation(location.getLatitude(), location.getLongnitude());

    if (myPolygon.contains(P2D2)) {
        blnIsinside = true;
    } else {
        blnIsinside = false;
    }

    return blnIsinside;
}

Upvotes: -1

lelloman
lelloman

Reputation: 14173

That point can't possibly be contained in that polygon, no matter what shape the polygon has.

Your right-most coordinate is at 40.858716 while the point has an x value of 40.86141, this means that the point lies on the right of your polygon. Same for y, max y coordinate in the polygon is 14.278701 while the point is at 14.279932. This means that the point is outside.

Also, you're inverting the coordinates, the coordinates of our beloved city are 40.8518° N, 14.2681° E, this means that 40 is the y and 14 the x.

Path2D will do just fine. My observation just tells you that the point is not in the polygon but checking the extremes is not a general solution for verifying that a point is inside a polygon.

Upvotes: 2

Related Questions