2

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...

8
  • 1
    looking at the numbers it seems to me that myPoint is not in the polygon, the highest y coordinate of the polygon is 14.26 while your point has a y of 14.27 Commented Mar 18, 2018 at 16:55
  • 1
    same for x coordinate, the highest value in the polygon is 40.85 while myPoint has 40.86, how could you say that the point is inside the polygon Commented Mar 18, 2018 at 17:01
  • thanks, i changed the coordinates, added also the google maps situation, the point is inside the Area, i only thing that the polygon with geo coords does not fit... Commented Mar 18, 2018 at 17:13
  • 1
    ci credi che abito a pasquale scura? sono nel poligono loooool Commented Mar 18, 2018 at 17:15
  • 1
    your point on the map is wrong, google maps shows that 40.86141, 14.279932 is just in front of poggioreale, not chiaia (super lol), but also the numbers don't lie, if both coordinates of your point have greater value than any of the vertex of the polygon, how could possibly the point be contained? Commented Mar 18, 2018 at 17:22

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This answer only works for that specific point and a subset of other points. For other points, they might lay in a surrounding rectangle, but not be in the polygone though.
@userunknown my answer just points out that the point is not in the polygon, if you want to check if the point is in the polygon Path2D would do just fine. Also the provided x and y values are flipped, I just by coincidence happen to live inside that polygon xD
You should consider making that recommendation part of your answer.
@userunknown thanks for the suggestion, I see that somebody already thought that one can verify if a point is inside a polygon just with the extremes coordinates, which is wrong
-1
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.