Class PolyUtil


  • public class PolyUtil
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double DEFAULT_TOLERANCE  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsLocation​(double latitude, double longitude, java.util.List<com.google.android.gms.maps.model.LatLng> polygon, boolean geodesic)
      Computes whether the given point lies inside the specified polygon.
      static boolean containsLocation​(com.google.android.gms.maps.model.LatLng point, java.util.List<com.google.android.gms.maps.model.LatLng> polygon, boolean geodesic)  
      static java.util.List<com.google.android.gms.maps.model.LatLng> decode​(java.lang.String encodedPath)
      Decodes an encoded path string into a sequence of LatLngs.
      static double distanceToLine​(com.google.android.gms.maps.model.LatLng p, com.google.android.gms.maps.model.LatLng start, com.google.android.gms.maps.model.LatLng end)
      Computes the distance on the sphere between the point p and the line segment start to end.
      static java.lang.String encode​(java.util.List<com.google.android.gms.maps.model.LatLng> path)
      Encodes a sequence of LatLngs into an encoded path string.
      static boolean isClosedPolygon​(java.util.List<com.google.android.gms.maps.model.LatLng> poly)
      Returns true if the provided list of points is a closed polygon (i.e., the first and last points are the same), and false if it is not
      static boolean isLocationOnEdge​(com.google.android.gms.maps.model.LatLng point, java.util.List<com.google.android.gms.maps.model.LatLng> polygon, boolean geodesic)
      Same as isLocationOnEdge(LatLng, List, boolean, double) with a default tolerance of 0.1 meters.
      static boolean isLocationOnEdge​(com.google.android.gms.maps.model.LatLng point, java.util.List<com.google.android.gms.maps.model.LatLng> polygon, boolean geodesic, double tolerance)
      Computes whether the given point lies on or near the edge of a polygon, within a specified tolerance in meters.
      static boolean isLocationOnPath​(com.google.android.gms.maps.model.LatLng point, java.util.List<com.google.android.gms.maps.model.LatLng> polyline, boolean geodesic)
      static boolean isLocationOnPath​(com.google.android.gms.maps.model.LatLng point, java.util.List<com.google.android.gms.maps.model.LatLng> polyline, boolean geodesic, double tolerance)
      Computes whether the given point lies on or near a polyline, within a specified tolerance in meters.
      static int locationIndexOnEdgeOrPath​(com.google.android.gms.maps.model.LatLng point, java.util.List<com.google.android.gms.maps.model.LatLng> poly, boolean closed, boolean geodesic, double toleranceEarth)
      Computes whether (and where) a given point lies on or near a polyline, within a specified tolerance.
      static int locationIndexOnPath​(com.google.android.gms.maps.model.LatLng point, java.util.List<com.google.android.gms.maps.model.LatLng> polyline, boolean geodesic)
      static int locationIndexOnPath​(com.google.android.gms.maps.model.LatLng point, java.util.List<com.google.android.gms.maps.model.LatLng> poly, boolean geodesic, double tolerance)
      Computes whether (and where) a given point lies on or near a polyline, within a specified tolerance.
      static java.util.List<com.google.android.gms.maps.model.LatLng> simplify​(java.util.List<com.google.android.gms.maps.model.LatLng> poly, double tolerance)
      Simplifies the given poly (polyline or polygon) using the Douglas-Peucker decimation algorithm.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • containsLocation

        public static boolean containsLocation​(com.google.android.gms.maps.model.LatLng point,
                                               java.util.List<com.google.android.gms.maps.model.LatLng> polygon,
                                               boolean geodesic)
      • containsLocation

        public static boolean containsLocation​(double latitude,
                                               double longitude,
                                               java.util.List<com.google.android.gms.maps.model.LatLng> polygon,
                                               boolean geodesic)
        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.
      • isLocationOnEdge

        public static boolean isLocationOnEdge​(com.google.android.gms.maps.model.LatLng point,
                                               java.util.List<com.google.android.gms.maps.model.LatLng> polygon,
                                               boolean geodesic,
                                               double tolerance)
        Computes whether the given point lies on or near the edge of a polygon, within a specified tolerance in meters. The polygon edge is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise. The polygon edge is implicitly closed -- the closing segment between the first point and the last point is included.
      • isLocationOnEdge

        public static boolean isLocationOnEdge​(com.google.android.gms.maps.model.LatLng point,
                                               java.util.List<com.google.android.gms.maps.model.LatLng> polygon,
                                               boolean geodesic)
        Same as isLocationOnEdge(LatLng, List, boolean, double) with a default tolerance of 0.1 meters.
      • isLocationOnPath

        public static boolean isLocationOnPath​(com.google.android.gms.maps.model.LatLng point,
                                               java.util.List<com.google.android.gms.maps.model.LatLng> polyline,
                                               boolean geodesic,
                                               double tolerance)
        Computes whether the given point lies on or near a polyline, within a specified tolerance in meters. The polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing segment between the first point and the last point is not included.
      • isLocationOnPath

        public static boolean isLocationOnPath​(com.google.android.gms.maps.model.LatLng point,
                                               java.util.List<com.google.android.gms.maps.model.LatLng> polyline,
                                               boolean geodesic)
        Same as isLocationOnPath(LatLng, List, boolean, double)

        with a default tolerance of 0.1 meters.

      • locationIndexOnPath

        public static int locationIndexOnPath​(com.google.android.gms.maps.model.LatLng point,
                                              java.util.List<com.google.android.gms.maps.model.LatLng> poly,
                                              boolean geodesic,
                                              double tolerance)
        Computes whether (and where) a given point lies on or near a polyline, within a specified tolerance. The polyline is not closed -- the closing segment between the first point and the last point is not included.
        Parameters:
        point - our needle
        poly - our haystack
        geodesic - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise
        tolerance - tolerance (in meters)
        Returns:
        -1 if point does not lie on or near the polyline. 0 if point is between poly[0] and poly[1] (inclusive), 1 if between poly[1] and poly[2], ..., poly.size()-2 if between poly[poly.size() - 2] and poly[poly.size() - 1]
      • locationIndexOnPath

        public static int locationIndexOnPath​(com.google.android.gms.maps.model.LatLng point,
                                              java.util.List<com.google.android.gms.maps.model.LatLng> polyline,
                                              boolean geodesic)
        Same as locationIndexOnPath(LatLng, List, boolean, double)

        with a default tolerance of 0.1 meters.

      • locationIndexOnEdgeOrPath

        public static int locationIndexOnEdgeOrPath​(com.google.android.gms.maps.model.LatLng point,
                                                    java.util.List<com.google.android.gms.maps.model.LatLng> poly,
                                                    boolean closed,
                                                    boolean geodesic,
                                                    double toleranceEarth)
        Computes whether (and where) a given point lies on or near a polyline, within a specified tolerance. If closed, the closing segment between the last and first points of the polyline is not considered.
        Parameters:
        point - our needle
        poly - our haystack
        closed - whether the polyline should be considered closed by a segment connecting the last point back to the first one
        geodesic - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise
        toleranceEarth - tolerance (in meters)
        Returns:
        -1 if point does not lie on or near the polyline. 0 if point is between poly[0] and poly[1] (inclusive), 1 if between poly[1] and poly[2], ..., poly.size()-2 if between poly[poly.size() - 2] and poly[poly.size() - 1]
      • simplify

        public static java.util.List<com.google.android.gms.maps.model.LatLng> simplify​(java.util.List<com.google.android.gms.maps.model.LatLng> poly,
                                                                                        double tolerance)
        Simplifies the given poly (polyline or polygon) using the Douglas-Peucker decimation algorithm. Increasing the tolerance will result in fewer points in the simplified polyline or polygon.

        When the providing a polygon as input, the first and last point of the list MUST have the same latitude and longitude (i.e., the polygon must be closed). If the input polygon is not closed, the resulting polygon may not be fully simplified.

        The time complexity of Douglas-Peucker is O(n^2), so take care that you do not call this algorithm too frequently in your code.

        Parameters:
        poly - polyline or polygon to be simplified. Polygon should be closed (i.e., first and last points should have the same latitude and longitude).
        tolerance - in meters. Increasing the tolerance will result in fewer points in the simplified poly.
        Returns:
        a simplified poly produced by the Douglas-Peucker algorithm
      • isClosedPolygon

        public static boolean isClosedPolygon​(java.util.List<com.google.android.gms.maps.model.LatLng> poly)
        Returns true if the provided list of points is a closed polygon (i.e., the first and last points are the same), and false if it is not
        Parameters:
        poly - polyline or polygon
        Returns:
        true if the provided list of points is a closed polygon (i.e., the first and last points are the same), and false if it is not
      • distanceToLine

        public static double distanceToLine​(com.google.android.gms.maps.model.LatLng p,
                                            com.google.android.gms.maps.model.LatLng start,
                                            com.google.android.gms.maps.model.LatLng end)
        Computes the distance on the sphere between the point p and the line segment start to end.
        Parameters:
        p - the point to be measured
        start - the beginning of the line segment
        end - the end of the line segment
        Returns:
        the distance in meters (assuming spherical earth)
      • decode

        public static java.util.List<com.google.android.gms.maps.model.LatLng> decode​(java.lang.String encodedPath)
        Decodes an encoded path string into a sequence of LatLngs.
      • encode

        public static java.lang.String encode​(java.util.List<com.google.android.gms.maps.model.LatLng> path)
        Encodes a sequence of LatLngs into an encoded path string.