Package 

Object PolyUtil


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

    • 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.
      final Double getDEFAULT_TOLERANCE()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.
      • 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.
      • 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.
      • 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.
      • 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.
      • 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)
      • 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
      • 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)
      • 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.
      • 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
      • 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
      • 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.