Object PolyUtil

  • All Implemented Interfaces:

    
    public class PolyUtil
    
                        

    A utility class containing geometric calculations for polygons and polylines. This class provides methods for determining if a point is inside a polygon, on the edge of a polygon, simplifying polylines, and encoding/decoding polylines.

    The methods in this class are designed to be used with the Google Maps Android API, and they operate on {@link LatLng} objects. The calculations can be performed using either geodesic (great circle) or rhumb (loxodromic) paths.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final static Boolean containsLocation(LatLng point, List<LatLng> polygon, Boolean geodesic) Computes whether the given point lies inside the specified polygon.
      final static Boolean containsLocation(Double latitude, Double longitude, List<LatLng> polygon, Boolean geodesic) Overload of {@link #containsLocation(LatLng, List, boolean)} that takes latitude and longitude as separate arguments.
      final static Boolean isLocationOnEdge(LatLng point, List<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.
      final static Boolean isLocationOnEdge(LatLng point, List<LatLng> polygon, Boolean geodesic) Computes whether the given point lies on or near the edge of a polygon, within a specified tolerance in meters.
      final static Boolean isLocationOnPath(LatLng point, List<LatLng> polyline, Boolean geodesic, Double tolerance) Computes whether the given point lies on or near a polyline, within a specified tolerance in meters.
      final static Boolean isLocationOnPath(LatLng point, List<LatLng> polyline, Boolean geodesic) Computes whether the given point lies on or near a polyline, within a specified tolerance in meters.
      final static Integer locationIndexOnPath(LatLng point, List<LatLng> poly, Boolean geodesic, Double tolerance) Computes whether (and where) a given point lies on or near a polyline, within a specified tolerance.
      final static Integer locationIndexOnPath(LatLng point, List<LatLng> poly, Boolean geodesic) Computes whether (and where) a given point lies on or near a polyline, within a specified tolerance.
      final static Integer locationIndexOnEdgeOrPath(LatLng point, List<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.
      final static List<LatLng> simplify(List<LatLng> poly, Double tolerance) Simplifies the given poly (polyline or polygon) using the Douglas-Peucker decimation algorithm.
      final static Boolean isClosedPolygon(List<LatLng> poly) Returns true if the provided list of points is a closed polygon (i.e.
      final static Double distanceToLine(LatLng p, LatLng start, LatLng end) Computes the distance on the sphere between the point p and the line segment start to end.
      final static List<LatLng> decode(String encodedPath) Decodes an encoded path string into a sequence of LatLngs.
      final static String encode(List<LatLng> path) Encodes a sequence of LatLngs into an encoded path string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • containsLocation

         final static Boolean containsLocation(LatLng point, List<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.

        Parameters:
        point - The point to check.
        polygon - The polygon to check against.
        geodesic - Whether to treat the polygon segments as geodesic or rhumb lines.
        Returns:

        true if the point is inside the polygon, false otherwise.

      • containsLocation

         final static Boolean containsLocation(Double latitude, Double longitude, List<LatLng> polygon, Boolean geodesic)

        Overload of {@link #containsLocation(LatLng, List, boolean)} that takes latitude and longitude as separate arguments.

      • isLocationOnEdge

        @JvmOverloads() final static Boolean isLocationOnEdge(LatLng point, List<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.

        Parameters:
        point - The point to check.
        polygon - The polygon to check against.
        geodesic - Whether to treat the polygon segments as geodesic or rhumb lines.
        tolerance - The tolerance in meters.
        Returns:

        true if the point is on the edge of the polygon, false otherwise.

      • isLocationOnEdge

        @JvmOverloads() final static Boolean isLocationOnEdge(LatLng point, List<LatLng> polygon, Boolean geodesic)

        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.

        Parameters:
        point - The point to check.
        polygon - The polygon to check against.
        geodesic - Whether to treat the polygon segments as geodesic or rhumb lines.
        Returns:

        true if the point is on the edge of the polygon, false otherwise.

      • isLocationOnPath

        @JvmOverloads() final static Boolean isLocationOnPath(LatLng point, List<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.

        Parameters:
        point - The point to check.
        polyline - The polyline to check against.
        geodesic - Whether to treat the polyline segments as geodesic or rhumb lines.
        tolerance - The tolerance in meters.
        Returns:

        true if the point is on the polyline, false otherwise.

      • isLocationOnPath

        @JvmOverloads() final static Boolean isLocationOnPath(LatLng point, List<LatLng> polyline, Boolean geodesic)

        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.

        Parameters:
        point - The point to check.
        polyline - The polyline to check against.
        geodesic - Whether to treat the polyline segments as geodesic or rhumb lines.
        Returns:

        true if the point is on the polyline, false otherwise.

      • locationIndexOnPath

        @JvmOverloads() final static Integer locationIndexOnPath(LatLng point, List<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 poly0 and poly1 (inclusive), 1 if between poly1 and poly2, ..., poly.size()-2 if between polypoly.size() - 2 and polypoly.size() - 1

      • locationIndexOnPath

        @JvmOverloads() final static Integer locationIndexOnPath(LatLng point, List<LatLng> poly, Boolean geodesic)

        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
        Returns:

        -1 if point does not lie on or near the polyline. 0 if point is between poly0 and poly1 (inclusive), 1 if between poly1 and poly2, ..., poly.size()-2 if between polypoly.size() - 2 and polypoly.size() - 1

      • locationIndexOnEdgeOrPath

         final static Integer locationIndexOnEdgeOrPath(LatLng point, List<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 poly0 and poly1 (inclusive), 1 if between poly1 and poly2, ..., poly.size()-2 if between polypoly.size() - 2 and polypoly.size() - 1

      • simplify

         final static List<LatLng> simplify(List<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.
        tolerance - in meters.
        Returns:

        a simplified poly produced by the Douglas-Peucker algorithm

      • isClosedPolygon

         final static Boolean isClosedPolygon(List<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

         final static Double distanceToLine(LatLng p, LatLng start, 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

         final static List<LatLng> decode(String encodedPath)

        Decodes an encoded path string into a sequence of LatLngs.

      • encode

         final static String encode(List<LatLng> path)

        Encodes a sequence of LatLngs into an encoded path string.