Class Locations
- java.lang.Object
-
- gov.usgs.earthquake.nshmp.geo.Locations
-
public final class Locations extends Object
Static utility methods to operate on geographicLocation
data.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 double
TOLERANCE
Tolerance 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 double
angle(Location p1, Location p2)
Calculates the angle between twoLocation
s using the Haversine formula.static double
area(LocationList border)
Compute the signed area of a closed path in km².static boolean
areSimilar(Location p1, Location p2)
Returnstrue
if the suppliedLocation
s are very, very close to one another.static double
azimuth(Location p1, Location p2)
Computes the initial azimuth (bearing) when moving from oneLocation
to another in degrees.static double
azimuthRad(Location p1, Location p2)
Computes the initial azimuth (bearing) when moving from oneLocation
to another.static LocationVector
bisect(Location p1, Location p2, Location p3)
Method returns a unitLocationVector
that bisects the angle defined by the line segmentsp2p1
andp2p3
.static Bounds
bounds(Iterable<Location> locs)
Compute theBounds
of the supplied iterable.static Location
centroid(Iterable<Location> locs)
Computes a centroid for a group ofLocation
s as the average of latitude, longitude, and depth;static Location
closestPoint(Location loc, LocationList locs)
Return the point in aLocationList
that is that closest to the suppliedLocation
.static Predicate<Location>
distanceAndRectangleFilter(Location origin, double distance)
Return a radial distanceLocation
filter that preprocessesLocation
s through arectangleFilter(Location, double)
.static Predicate<Location>
distanceFilter(Location origin, double distance)
Return a radial distanceLocation
filter.static double
distanceToLine(Location p1, Location p2, Location p3)
Computes the shortest distance between a point and a line (great-circle).static double
distanceToLineFast(Location p1, Location p2, Location p3)
Computes the shortest distance between a point and a line.static double
distanceToSegment(Location p1, Location p2, Location p3)
Computes the shortest distance between a point and a line segment (i.e.static double
distanceToSegmentFast(Location p1, Location p2, Location p3)
Computes the shortest distance between a point and a line segment.static double
horzDistance(Location p1, Location p2)
Calculates the great circle surface distance between twoLocation
s using the Haversine formula for computing the angle between two points.static double
horzDistanceFast(Location p1, Location p2)
Calculates approximate distance between twoLocation
s.static boolean
isPole(Location p)
Returns whether the suppliedLocation
coincides with one of the poles.static double
linearDistance(Location p1, Location p2)
Calculates the distance in three dimensions between twoLocation
s using spherical geometry.static double
linearDistanceFast(Location p1, Location p2)
Calculates the approximate linear distance in three dimensions between twoLocation
s.static Location
location(Location p, double azimuth, double distance)
Computes aLocation
given an origin point, bearing, and distance.static Location
location(Location p, LocationVector d)
Computes aLocation
given an origin point and aLocationVector
.static int
minDistanceIndex(Location loc, LocationList locs)
Compute the segment index that is closest to aLocation
.static double
minDistanceToLine(Location loc, LocationList locs)
Compute the shortest horizontal distance (in km) from aLocation
to the line defined by connecting the points in aLocationList
.static double
minDistanceToLocations(Location loc, LocationList locs)
Compute the horizontal distance (in km) from aLocation
to the closest point in aLocationList
.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.static Predicate<Location>
rectangleFilter(Location origin, double distance)
Return a rectangularLocation
filter.static Path2D
toPath(LocationList locs)
Return a closed, straight-linePath2D
representation of the supplied list, ignoring depth.static double
vertDistance(Location p1, Location p2)
Returns the vertical separation between twoLocation
s.
-
-
-
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_MEAN
to 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 twoLocation
s using the Haversine formula. This method properly handles values spanning ±180°. See Aviation Formulary for source. Result is returned in radians.- Parameters:
p1
- the firstLocation
pointp2
- the secondLocation
point- Returns:
- the angle between the points (in radians)
-
horzDistance
public static double horzDistance(Location p1, Location p2)
Calculates the great circle surface distance between twoLocation
s 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 firstLocation
pointp2
- the secondLocation
point- 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 twoLocation
s. 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 firstLocation
pointp2
- the secondLocation
point- 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 twoLocation
s. The returned value is not absolute and preserves the sign of the difference between the points.- Parameters:
p1
- the firstLocation
pointp2
- the firstLocation
point- Returns:
- the vertical separation between the points
-
linearDistance
public static double linearDistance(Location p1, Location p2)
Calculates the distance in three dimensions between twoLocation
s 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 firstLocation
pointp2
- the secondLocation
point- 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 twoLocation
s. 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 firstLocation
pointp2
- the secondLocation
point- 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 eachLocation
is 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 linep3
is 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
p3
does not project onto the segment, usedistanceToSegment(Location, Location, Location)
instead.- Parameters:
p1
- the firstLocation
point on the linep2
- the secondLocation
point on the linep3
- theLocation
point 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 eachLocation
is 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 linep3
is 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
p3
does not project onto the segment, usedistanceToSegmentFast(Location, Location, Location)
instead.- Parameters:
p1
- the firstLocation
point on the linep2
- the secondLocation
point on the linep3
- theLocation
point 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 eachLocation
is 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 firstLocation
point on the linep2
- the secondLocation
point on the linep3
- theLocation
point 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 eachLocation
is 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 firstLocation
point on the linep2
- the secondLocation
point on the linep3
- theLocation
point 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 oneLocation
to another. See Aviation Formulary for source. For back azimuth, reverse theLocation
arguments. Result is returned in radians over the interval [0, 2π).Note: It is more efficient to use this method for computation because
Location
s store lat and lon in radians internally. Useazimuth(Location, Location)
for presentation.- Parameters:
p1
- the firstLocation
pointp2
- the secondLocation
point- 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 oneLocation
to another in degrees. See Aviation Formulary for source. For back azimuth, reverse theLocation
arguments. Result is returned in decimal degrees over the interval 0° to 360°.- Parameters:
p1
- the firstLocation
pointp2
- the secondLocation
point- 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 aLocation
given an origin point, bearing, and distance. See Aviation Formulary for source. Note thatazimuth
is 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 firstLocation
pointp2
- the secondLocation
point- Returns:
- the plunge of this vector
-
bisect
public static LocationVector bisect(Location p1, Location p2, Location p3)
Method returns a unitLocationVector
that bisects the angle defined by the line segmentsp2p1
andp2p3
.- 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 suppliedLocation
coincides with one of the poles. Any suppliedLocation
s that are very close (less than a mm) will returntrue
.- Parameters:
p
-Location
to check- Returns:
true
ifloc
coincides with one of the earth's poles,false
otherwise.
-
areSimilar
public static boolean areSimilar(Location p1, Location p2)
Returnstrue
if the suppliedLocation
s are very, very close to one another. Internally, lat, lon, and depth values must be within <1mm of each other.- Parameters:
p1
- the firstLocation
to comparep2
- the secondLocation
to compare- Returns:
true
if the suppliedLocation
s are very close,false
otherwise.
-
bounds
public static Bounds bounds(Iterable<Location> locs)
Compute theBounds
of the supplied iterable. Iflocs
contains only 1 element,Bounds.min
andBounds.max
of the returned object will be the same.- Parameters:
locs
- to compute bounds for- Throws:
IllegalArgumentException
- iflocs
is empty
-
centroid
public static Location centroid(Iterable<Location> locs)
Computes a centroid for a group ofLocation
s as the average of latitude, longitude, and depth;- Parameters:
locs
- locations to process
-
toPath
public static Path2D toPath(LocationList locs)
Return a closed, straight-linePath2D
representation of the supplied list, ignoring depth.
-
closestPoint
public static Location closestPoint(Location loc, LocationList locs)
Return the point in aLocationList
that 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 aLocation
to the closest point in aLocationList
. This method useshorzDistanceFast(Location, Location)
to compute the distance.- Parameters:
loc
-Location
of interestlocs
-LocationList
to 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 aLocation
to 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
-Location
of interestlocs
-LocationList
to 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() - 1
segment indices. The indices of the segment endpoints in the original location list are[n, n+1]
.- Parameters:
loc
-Location
of interestlocs
-LocationList
for 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 distanceLocation
filter.- Parameters:
origin
- of filterdistance
- beyond which the filter will returnfalse
-
distanceAndRectangleFilter
public static Predicate<Location> distanceAndRectangleFilter(Location origin, double distance)
Return a radial distanceLocation
filter that preprocessesLocation
s 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 rectangularLocation
filter. The filter is definied in geographic (lat,lon) space and is constrained toCoordinates.LAT_RANGE
andCoordinates.LON_RANGE
. The filter has dimensions of2 * distance
for 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
-
-