Class Locations
- java.lang.Object
-
- gov.usgs.earthquake.nshmp.geo.Locations
-
public final class Locations extends Object
Static utility methods to operate on geographicLocationdata.See: Aviation Formulary for formulae implemented in this class as well as Moveable Type Scripts for other implementations.
- Author:
- U.S. Geological Survey
- See Also:
Location
-
-
Field Summary
Fields Modifier and Type Field Description static doubleTOLERANCETolerance used for location comparisons; 0.000000000001 which in decimal-degrees, radians, and km is comparable to micron-scale precision.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doubleangle(Location p1, Location p2)Calculates the angle between twoLocations using the Haversine formula.static doublearea(LocationList border)Compute the signed area of a closed path in km².static booleanareSimilar(Location p1, Location p2)Returnstrueif the suppliedLocations are very, very close to one another.static doubleazimuth(Location p1, Location p2)Computes the initial azimuth (bearing) when moving from oneLocationto another in degrees.static doubleazimuthRad(Location p1, Location p2)Computes the initial azimuth (bearing) when moving from oneLocationto another.static LocationVectorbisect(Location p1, Location p2, Location p3)Method returns a unitLocationVectorthat bisects the angle defined by the line segmentsp2p1andp2p3.static Boundsbounds(Iterable<Location> locs)Compute theBoundsof the supplied iterable.static Locationcentroid(Iterable<Location> locs)Computes a centroid for a group ofLocations as the average of latitude, longitude, and depth;static LocationclosestPoint(Location loc, LocationList locs)Return the point in aLocationListthat is that closest to the suppliedLocation.static Predicate<Location>distanceAndRectangleFilter(Location origin, double distance)Return a radial distanceLocationfilter that preprocessesLocations through arectangleFilter(Location, double).static Predicate<Location>distanceFilter(Location origin, double distance)Return a radial distanceLocationfilter.static doubledistanceToLine(Location p1, Location p2, Location p3)Computes the shortest distance between a point and a line (great-circle).static doubledistanceToLineFast(Location p1, Location p2, Location p3)Computes the shortest distance between a point and a line.static doubledistanceToSegment(Location p1, Location p2, Location p3)Computes the shortest distance between a point and a line segment (i.e.static doubledistanceToSegmentFast(Location p1, Location p2, Location p3)Computes the shortest distance between a point and a line segment.static doublehorzDistance(Location p1, Location p2)Calculates the great circle surface distance between twoLocations using the Haversine formula for computing the angle between two points.static doublehorzDistanceFast(Location p1, Location p2)Calculates approximate distance between twoLocations.static booleanisPole(Location p)Returns whether the suppliedLocationcoincides with one of the poles.static doublelinearDistance(Location p1, Location p2)Calculates the distance in three dimensions between twoLocations using spherical geometry.static doublelinearDistanceFast(Location p1, Location p2)Calculates the approximate linear distance in three dimensions between twoLocations.static Locationlocation(Location p, double azimuth, double distance)Computes aLocationgiven an origin point, bearing, and distance.static Locationlocation(Location p, LocationVector d)Computes aLocationgiven an origin point and aLocationVector.static intminDistanceIndex(Location loc, LocationList locs)Compute the segment index that is closest to aLocation.static doubleminDistanceToLine(Location loc, LocationList locs)Compute the shortest horizontal distance (in km) from aLocationto the line defined by connecting the points in aLocationList.static doubleminDistanceToLocations(Location loc, LocationList locs)Compute the horizontal distance (in km) from aLocationto the closest point in aLocationList.static doubleplunge(Location p1, Location p2)Returns the angle (in decimal degrees) of a line between the first and second location relative to horizontal.static Predicate<Location>rectangleFilter(Location origin, double distance)Return a rectangularLocationfilter.static Path2DtoPath(LocationList locs)Return a closed, straight-linePath2Drepresentation of the supplied list, ignoring depth.static doublevertDistance(Location p1, Location p2)Returns the vertical separation between twoLocations.
-
-
-
Field Detail
-
TOLERANCE
public static final double TOLERANCE
Tolerance used for location comparisons; 0.000000000001 which in decimal-degrees, radians, and km is comparable to micron-scale precision.- See Also:
- Constant Field Values
-
-
Method Detail
-
area
public static double area(LocationList border)
Compute the signed area of a closed path in km². Method returns zero if supplied border has less than three coordinates. Supplied polygon does not need to be closed (the first and last points do not need to be the same). Calculation usesCoordinates.EARTH_RADIUS_MEANto compute area and does not consider depths of border coordinates at this time.- Parameters:
border-radius-- See Also:
com.google.maps.android.SphericalUtil
-
angle
public static double angle(Location p1, Location p2)
Calculates the angle between twoLocations using the Haversine formula. This method properly handles values spanning ±180°. See Aviation Formulary for source. Result is returned in radians.- Parameters:
p1- the firstLocationpointp2- the secondLocationpoint- Returns:
- the angle between the points (in radians)
-
horzDistance
public static double horzDistance(Location p1, Location p2)
Calculates the great circle surface distance between twoLocations using the Haversine formula for computing the angle between two points. For a faster, but less accurate implementation at large separations, seehorzDistanceFast(Location, Location).- Parameters:
p1- the firstLocationpointp2- the secondLocationpoint- Returns:
- the distance between the points in km
- See Also:
angle(Location, Location),horzDistanceFast(Location, Location)
-
horzDistanceFast
public static double horzDistanceFast(Location p1, Location p2)
Calculates approximate distance between twoLocations. This method is about 2 orders of magnitude faster thanhorzDistance(), but is imprecise at large distances. Method uses the latitudinal and longitudinal differences between the points as the sides of a right triangle. The longitudinal distance is scaled by the cosine of the mean latitude.Note: This method does NOT support values spanning ±180° and fails where the numeric angle exceeds 180°. Convert data to the 0-360° interval or use
horzDistance(Location, Location)in such instances.- Parameters:
p1- the firstLocationpointp2- the secondLocationpoint- Returns:
- the distance between the points in km
- See Also:
horzDistance(Location, Location)
-
vertDistance
public static double vertDistance(Location p1, Location p2)
Returns the vertical separation between twoLocations. The returned value is not absolute and preserves the sign of the difference between the points.- Parameters:
p1- the firstLocationpointp2- the firstLocationpoint- Returns:
- the vertical separation between the points
-
linearDistance
public static double linearDistance(Location p1, Location p2)
Calculates the distance in three dimensions between twoLocations using spherical geometry. Method returns the straight line distance taking into account the depths of the points. For a faster, but less accurate implementation at large separations, seelinearDistanceFast(Location, Location).- Parameters:
p1- the firstLocationpointp2- the secondLocationpoint- Returns:
- the distance in km between the points
- See Also:
linearDistanceFast(Location, Location)
-
linearDistanceFast
public static double linearDistanceFast(Location p1, Location p2)
Calculates the approximate linear distance in three dimensions between twoLocations. This simple and speedy implementation uses the Pythagorean theorem, treating horizontal and vertical separations as orthogonal.Note: This method is very imprecise at large separations and should not be used for points >200km apart. If an estimate of separation distance is not known in advance use
linearDistance(Location, Location)for more reliable results.Note: This method fails for values spanning ±180°; see
horzDistanceFast(Location, Location).- Parameters:
p1- the firstLocationpointp2- the secondLocationpoint- Returns:
- the distance in km between the points
- See Also:
linearDistance(Location, Location)
-
distanceToLine
public static double distanceToLine(Location p1, Location p2, Location p3)
Computes the shortest distance between a point and a line (great-circle). that extends infinitely in both directions. Both the line and point are assumed to be at the earth's surface; the depth component of eachLocationis ignored. This method uses the true spherical geometric function for 'off-track distance'; See Aviation Formulary for source. The sign of the result indicates which side of the supplied linep3is on (right:[+] left:[-]).This method, though more accurate over longer distances and line lengths, is up to 20x slower than
distanceToLineFast(Location, Location, Location). However, this method returns accurate results for values spanning ±180°.If the line should instead be treated as a segment such that the result will be a distance to an endpoint if
p3does not project onto the segment, usedistanceToSegment(Location, Location, Location)instead.- Parameters:
p1- the firstLocationpoint on the linep2- the secondLocationpoint on the linep3- theLocationpoint for which distance will be calculated- Returns:
- the shortest distance in km between the supplied point and line
- See Also:
distanceToLineFast(Location, Location, Location),distanceToSegment(Location, Location, Location)
-
distanceToLineFast
public static double distanceToLineFast(Location p1, Location p2, Location p3)
Computes the shortest distance between a point and a line. Both the line and point are assumed to be at the earth's surface; the depth component of eachLocationis ignored. This is a fast, geometric, cartesion (flat-earth approximation) solution in which longitude of the line points are scaled by the cosine of latitude; it is only appropriate for use over short distances (e.g. <200 km). The sign of the result indicates which side of the supplied linep3is on (right:[+] left:[-]).Note: This method does NOT support values spanning ±180° and results for such input values are not guaranteed. Convert data to the 0-360° interval or use
distanceToLine(Location, Location, Location)in such instances.If the line should instead be treated as a segment such that the result will be a distance to an endpoint if
p3does not project onto the segment, usedistanceToSegmentFast(Location, Location, Location)instead.- Parameters:
p1- the firstLocationpoint on the linep2- the secondLocationpoint on the linep3- theLocationpoint for which distance will be calculated- Returns:
- the shortest distance in km between the supplied point and line
- See Also:
distanceToLine(Location, Location, Location),distanceToSegmentFast(Location, Location, Location)
-
distanceToSegment
public static double distanceToSegment(Location p1, Location p2, Location p3)
Computes the shortest distance between a point and a line segment (i.e. great-circle segment). Both the line and point are assumed to be at the earth's surface; the depth component of eachLocationis ignored. This method uses the true spherical geometric function for 'off-track distance'; See Aviation Formulary for source. This method always returns a positive result.This method, though more accurate over longer distances and line lengths, is up to 20x slower than
distanceToSegmentFast(Location, Location, Location). However, this method returns accurate results for values spanning ±180°.If the line should instead be treated as infinite, use
distanceToLine(Location, Location, Location)instead.- Parameters:
p1- the firstLocationpoint on the linep2- the secondLocationpoint on the linep3- theLocationpoint for which distance will be calculated- Returns:
- the shortest distance in km between the supplied point and line
- See Also:
distanceToLineFast(Location, Location, Location),distanceToLine(Location, Location, Location)
-
distanceToSegmentFast
public static double distanceToSegmentFast(Location p1, Location p2, Location p3)
Computes the shortest distance between a point and a line segment. Both the line and point are assumed to be at the earth's surface; the depth component of eachLocationis ignored. This is a fast, geometric, cartesion (flat-earth approximation) solution in which longitude of the line points are scaled by the cosine of latitude; it is only appropriate for use over short distances (e.g. <200 km).Note: This method fails for values spanning ±180°; see
distanceToSegment(Location, Location, Location).If the line should instead be treated as infinite, use
distanceToLineFast(Location, Location, Location)instead.- Parameters:
p1- the firstLocationpoint on the linep2- the secondLocationpoint on the linep3- theLocationpoint for which distance will be calculated- Returns:
- the shortest distance in km between the supplied point and line
- See Also:
distanceToSegment(Location, Location, Location),distanceToLineFast(Location, Location, Location)
-
azimuthRad
public static double azimuthRad(Location p1, Location p2)
Computes the initial azimuth (bearing) when moving from oneLocationto another. See Aviation Formulary for source. For back azimuth, reverse theLocationarguments. Result is returned in radians over the interval [0, 2π).Note: It is more efficient to use this method for computation because
Locations store lat and lon in radians internally. Useazimuth(Location, Location)for presentation.- Parameters:
p1- the firstLocationpointp2- the secondLocationpoint- Returns:
- the azimuth (bearing) from p1 to p2 in radians
- See Also:
azimuth(Location, Location)
-
azimuth
public static double azimuth(Location p1, Location p2)
Computes the initial azimuth (bearing) when moving from oneLocationto another in degrees. See Aviation Formulary for source. For back azimuth, reverse theLocationarguments. Result is returned in decimal degrees over the interval 0° to 360°.- Parameters:
p1- the firstLocationpointp2- the secondLocationpoint- Returns:
- the azimuth (bearing) from p1 to p2 in decimal degrees
- See Also:
azimuthRad(Location, Location)
-
location
public static Location location(Location p, double azimuth, double distance)
Computes aLocationgiven an origin point, bearing, and distance. See Aviation Formulary for source. Note thatazimuthis expected in radians.- Parameters:
p- starting location pointazimuth- (bearing) in radians away from origindistance- (horizontal) along bearing in km- Returns:
- the end location
-
location
public static Location location(Location p, LocationVector d)
- Parameters:
p- starting location pointd- distance along bearing- Returns:
- the end location
-
plunge
public static double plunge(Location p1, Location p2)
Returns the angle (in decimal degrees) of a line between the first and second location relative to horizontal. This method is intended for use at relatively short separations ( e.g. ≤200km) as it degrades at large distances where curvature is not considered. Note that positive angles are down, negative angles are up.- Parameters:
p1- the firstLocationpointp2- the secondLocationpoint- Returns:
- the plunge of this vector
-
bisect
public static LocationVector bisect(Location p1, Location p2, Location p3)
Method returns a unitLocationVectorthat bisects the angle defined by the line segmentsp2p1andp2p3.- Parameters:
p1- the first pointp2- the second pointp3- the third point- Returns:
- the bisecting
LocationVector
-
isPole
public static boolean isPole(Location p)
Returns whether the suppliedLocationcoincides with one of the poles. Any suppliedLocations that are very close (less than a mm) will returntrue.- Parameters:
p-Locationto check- Returns:
trueifloccoincides with one of the earth's poles,falseotherwise.
-
areSimilar
public static boolean areSimilar(Location p1, Location p2)
Returnstrueif the suppliedLocations are very, very close to one another. Internally, lat, lon, and depth values must be within <1mm of each other.- Parameters:
p1- the firstLocationto comparep2- the secondLocationto compare- Returns:
trueif the suppliedLocations are very close,falseotherwise.
-
bounds
public static Bounds bounds(Iterable<Location> locs)
Compute theBoundsof the supplied iterable. Iflocscontains only 1 element,Bounds.minandBounds.maxof the returned object will be the same.- Parameters:
locs- to compute bounds for- Throws:
IllegalArgumentException- iflocsis empty
-
centroid
public static Location centroid(Iterable<Location> locs)
Computes a centroid for a group ofLocations as the average of latitude, longitude, and depth;- Parameters:
locs- locations to process
-
toPath
public static Path2D toPath(LocationList locs)
Return a closed, straight-linePath2Drepresentation of the supplied list, ignoring depth.
-
closestPoint
public static Location closestPoint(Location loc, LocationList locs)
Return the point in aLocationListthat is that closest to the suppliedLocation. This method useshorzDistanceFast(Location, Location)to compute the distance.- Parameters:
loc-
-
minDistanceToLocations
public static double minDistanceToLocations(Location loc, LocationList locs)
Compute the horizontal distance (in km) from aLocationto the closest point in aLocationList. This method useshorzDistanceFast(Location, Location)to compute the distance.- Parameters:
loc-Locationof interestlocs-LocationListto compute distance to- See Also:
horzDistanceFast(Location, Location)
-
minDistanceToLine
public static double minDistanceToLine(Location loc, LocationList locs)
Compute the shortest horizontal distance (in km) from aLocationto the line defined by connecting the points in aLocationList. This method usesdistanceToSegmentFast(Location, Location, Location)and is inappropriate for for use at large separations (e.g. >200 km).- Parameters:
loc-Locationof interestlocs-LocationListto compute distance to- See Also:
distanceToSegmentFast(Location, Location, Location)
-
minDistanceIndex
public static int minDistanceIndex(Location loc, LocationList locs)
Compute the segment index that is closest to aLocation. There arelocs.size() - 1segment indices. The indices of the segment endpoints in the original location list are[n, n+1].- Parameters:
loc-Locationof interestlocs-LocationListfor which to compute the closest segment index- Throws:
IllegalArgumentException- iflocs.size() < 2
-
distanceFilter
public static Predicate<Location> distanceFilter(Location origin, double distance)
Return a radial distanceLocationfilter.- Parameters:
origin- of filterdistance- beyond which the filter will returnfalse
-
distanceAndRectangleFilter
public static Predicate<Location> distanceAndRectangleFilter(Location origin, double distance)
Return a radial distanceLocationfilter that preprocessesLocations through arectangleFilter(Location, double).- Parameters:
origin- of filterdistance- beyond which the filter will returnfalse- See Also:
rectangleFilter(Location, double)
-
rectangleFilter
public static Predicate<Location> rectangleFilter(Location origin, double distance)
Return a rectangularLocationfilter. The filter is definied in geographic (lat,lon) space and is constrained toCoordinates.LAT_RANGEandCoordinates.LON_RANGE. The filter has dimensions of2 * distancefor both height and width, and is centered on the suppliedLocation. This filter is for use as a fast, first-pass filter before more computationally intensive distance filtering.- Parameters:
origin- (center) of filterdistance- half-width and half-height of rectangle outside of which the filter will returnfalse,- See Also:
Coordinates
-
-