-
public class PolyUtil
-
-
Field Summary
Fields Modifier and Type Field Description public final static doubleDEFAULT_TOLERANCE
-
Method Summary
Modifier and Type Method Description static booleancontainsLocation(LatLng point, List<LatLng> polygon, boolean geodesic)static booleancontainsLocation(double latitude, double longitude, 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 equalsthe 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. static booleanisLocationOnEdge(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 specifiedtolerance in meters. static booleanisLocationOnEdge(LatLng point, List<LatLng> polygon, boolean geodesic)Same as isLocationOnEdge with a default tolerance of 0.1 meters. static booleanisLocationOnPath(LatLng point, List<LatLng> polyline, boolean geodesic, double tolerance)Computes whether the given point lies on or near a polyline, within a specifiedtolerance in meters. static booleanisLocationOnPath(LatLng point, List<LatLng> polyline, boolean geodesic)Same as isLocationOnPathwith a default tolerance of 0.1 meters. static intlocationIndexOnPath(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. static intlocationIndexOnPath(LatLng point, List<LatLng> polyline, boolean geodesic)Same as locationIndexOnPathwith a default tolerance of 0.1 meters. static intlocationIndexOnEdgeOrPath(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. static List<LatLng>simplify(List<LatLng> poly, double tolerance)Simplifies the given poly (polyline or polygon) using the Douglas-Peucker decimationalgorithm. static booleanisClosedPolygon(List<LatLng> poly)Returns true if the provided list of points is a closed polygon (i.e. static doubledistanceToLine(LatLng p, LatLng start, LatLng end)Computes the distance on the sphere between the point p and the line segment start to end. static List<LatLng>decode(String encodedPath)Decodes an encoded path string into a sequence of LatLngs. static Stringencode(List<LatLng> path)Encodes a sequence of LatLngs into an encoded path string. -
-
Method Detail
-
containsLocation
static boolean containsLocation(LatLng point, List<LatLng> polygon, boolean geodesic)
-
containsLocation
static boolean containsLocation(double latitude, double longitude, 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 equalsthe 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
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 specifiedtolerance in meters. The polygon edge is composed of great circle segments if geodesicis true, and of Rhumb segments otherwise. The polygon edge is implicitly closed -- theclosing segment between the first point and the last point is included.
-
isLocationOnEdge
static boolean isLocationOnEdge(LatLng point, List<LatLng> polygon, boolean geodesic)
Same as isLocationOnEdge with a default tolerance of 0.1 meters.
-
isLocationOnPath
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 specifiedtolerance in meters. The polyline is composed of great circle segments if geodesicis true, and of Rhumb segments otherwise. The polyline is not closed -- the closingsegment between the first point and the last point is not included.
-
isLocationOnPath
static boolean isLocationOnPath(LatLng point, List<LatLng> polyline, boolean geodesic)
Same as isLocationOnPath
with a default tolerance of 0.1 meters.
-
locationIndexOnPath
static int 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 needlepoly- our haystackgeodesic- the polyline is composed of great circle segments if geodesicis true, and of Rhumb segments otherwisetolerance- tolerance (in meters)
-
locationIndexOnPath
static int locationIndexOnPath(LatLng point, List<LatLng> polyline, boolean geodesic)
Same as locationIndexOnPath
with a default tolerance of 0.1 meters.
-
locationIndexOnEdgeOrPath
static int 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 needlepoly- our haystackclosed- whether the polyline should be considered closed by a segment connecting the last point back to the first onegeodesic- the polyline is composed of great circle segments if geodesicis true, and of Rhumb segments otherwisetoleranceEarth- tolerance (in meters)
-
simplify
static List<LatLng> simplify(List<LatLng> poly, double tolerance)
Simplifies the given poly (polyline or polygon) using the Douglas-Peucker decimationalgorithm. Increasing the tolerance will result in fewer points in the simplified polylineor polygon.
When the providing a polygon as input, the first and last point of the list MUST have thesame latitude and longitude (i.e., the polygon must be closed). If the input polygon is notclosed, 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 thisalgorithm too frequently in your code.
- Parameters:
poly- polyline or polygon to be simplified.tolerance- in meters.
-
isClosedPolygon
static boolean isClosedPolygon(List<LatLng> poly)
Returns true if the provided list of points is a closed polygon (i.e., the first and lastpoints are the same), and false if it is not
- Parameters:
poly- polyline or polygon
-
distanceToLine
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 measuredstart- the beginning of the line segmentend- the end of the line segment
-
decode
static List<LatLng> decode(String encodedPath)
Decodes an encoded path string into a sequence of LatLngs.
-
-
-
-