Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify if point is in the polygon

As per my requirement, I am drawing polygons on google map shown in the image below.(using maps v2) enter image description here

Now I need to show an alert when user enters that particular polygons.

How to identify if my current location is with in the polygon. (Need optimized way without draining battery)

Thanks in advance.

like image 740
Supriya Avatar asked Sep 24 '14 10:09

Supriya


People also ask

How do you determine if a point is within a polygon?

Draw a horizontal line to the right of each point and extend it to infinity. Count the number of times the line intersects with polygon edges. A point is inside the polygon if either count of intersections is odd or point lies on an edge of polygon. If none of the conditions is true, then point lies outside.

How do you check if a point is in a polygon in Python?

How to check if a point is inside a polygon in Python. To perform a Point in Polygon (PIP) query in Python, we can resort to the Shapely library's functions . within(), to check if a point is within a polygon, or . contains(), to check if a polygon contains a point.

How Inside Outside text can be performed to check whether given point is inside the polygon or not explain the process by considering polygon with 6 vertices?

The idea of the algorithm is pretty simple: Draw a virtual ray from anywhere outside the polygon to your point and count how often it hits a side of the polygon. If the number of hits is even, it's outside of the polygon, if it's odd, it's inside.

How do you determine if a point is inside a triangle?

A simple way is to: find the vectors connecting the point to each of the triangle's three vertices and sum the angles between those vectors. If the sum of the angles is 2*pi then the point is inside the triangle.


2 Answers

Just tried Ray Casting algorithm which identifies point in polygon. This works perfect.

Refer http://en.wikipedia.org/wiki/Point_in_polygon for thesis of Ray-Casting

private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
        int intersectCount = 0;
        for (int j = 0; j < vertices.size() - 1; j++) {
            if (rayCastIntersect(tap, vertices.get(j), vertices.get(j + 1))) {
                intersectCount++;
            }
        }

        return ((intersectCount % 2) == 1); // odd = inside, even = outside;
    }

    private boolean rayCastIntersect(LatLng tap, LatLng vertA, LatLng vertB) {

        double aY = vertA.latitude;
        double bY = vertB.latitude;
        double aX = vertA.longitude;
        double bX = vertB.longitude;
        double pY = tap.latitude;
        double pX = tap.longitude;

        if ((aY > pY && bY > pY) || (aY < pY && bY < pY)
                || (aX < pX && bX < pX)) {
            return false; // a and b can't both be above or below pt.y, and a or
                            // b must be east of pt.x
        }

        double m = (aY - bY) / (aX - bX); // Rise over run
        double bee = (-aX) * m + aY; // y = mx + b
        double x = (pY - bee) / m; // algebra is neat!

        return x > pX;
    }
like image 82
Supriya Avatar answered Sep 30 '22 04:09

Supriya


I found ray-casting method unreliable but I ended up using the PolyUtil from google maps.

You need the dependency compile 'com.google.maps.android:android-maps-utils:0.5'

And then the method looks like this

PolyUtil.containsLocation(userLocation, polyPointsList, false);

EDIT

This is the description of this method found in source code

Computes whether the given point lies inside the specified polygon. The polygon is always considered closed, regardless of whether the last point equals the first or not. Inside is defined as not containing the South Pole -- the South Pole is always outside. The polygon is formed of great circle segments if geodesic is true, and of rhumb (loxodromic) segments otherwise.

like image 40
DoruChidean Avatar answered Sep 30 '22 04:09

DoruChidean