Cleaned up code
This commit is contained in:
@@ -31,6 +31,7 @@ import com.google.android.maps.MapView;
|
||||
|
||||
/**
|
||||
* This class contains common tools for computing common geological problems
|
||||
*
|
||||
* @author ricky barrette
|
||||
* @author Google Inc.
|
||||
*/
|
||||
@@ -40,12 +41,19 @@ public class GeoUtils {
|
||||
public static final double MILLION = 1000000;
|
||||
|
||||
/**
|
||||
* computes the bearing of lat2/lon2 in relationship from lat1/lon1 in degrees East
|
||||
* @param lat1 source lat
|
||||
* @param lon1 source lon
|
||||
* @param lat2 destination lat
|
||||
* @param lon2 destination lon
|
||||
* @return the bearing of lat2/lon2 in relationship from lat1/lon1 in degrees East of true north
|
||||
* computes the bearing of lat2/lon2 in relationship from lat1/lon1 in
|
||||
* degrees East
|
||||
*
|
||||
* @param lat1
|
||||
* source lat
|
||||
* @param lon1
|
||||
* source lon
|
||||
* @param lat2
|
||||
* destination lat
|
||||
* @param lon2
|
||||
* destination lon
|
||||
* @return the bearing of lat2/lon2 in relationship from lat1/lon1 in
|
||||
* degrees East of true north
|
||||
* @author Google Inc.
|
||||
*/
|
||||
public static double bearing(final double lat1, final double lon1, final double lat2, final double lon2) {
|
||||
@@ -58,9 +66,13 @@ public class GeoUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* computes the bearing of lat2/lon2 in relationship from lat1/lon1 in degrees East of true north
|
||||
* @param p1 source geopoint
|
||||
* @param p2 destination geopoint
|
||||
* computes the bearing of lat2/lon2 in relationship from lat1/lon1 in
|
||||
* degrees East of true north
|
||||
*
|
||||
* @param p1
|
||||
* source geopoint
|
||||
* @param p2
|
||||
* destination geopoint
|
||||
* @return the bearing of p2 in relationship from p1 in degrees East
|
||||
* @author Google Inc.
|
||||
*/
|
||||
@@ -73,22 +85,28 @@ public class GeoUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the bearing from the user location to the destination location, or returns the bearing for north if there is no destination.
|
||||
* This method is awesome for making a compass point toward the destination rather than North.
|
||||
* @param user location
|
||||
* @param dest location
|
||||
* @param bearing Degrees East from compass
|
||||
* Calculates the bearing from the user location to the destination
|
||||
* location, or returns the bearing for north if there is no destination.
|
||||
* This method is awesome for making a compass point toward the destination
|
||||
* rather than North.
|
||||
*
|
||||
* @param user
|
||||
* location
|
||||
* @param dest
|
||||
* location
|
||||
* @param bearing
|
||||
* Degrees East from compass
|
||||
* @return Degrees East of dest location
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static float calculateBearing(final GeoPoint user, final GeoPoint dest, float bearing) {
|
||||
|
||||
if( user == null || dest == null )
|
||||
if (user == null || dest == null)
|
||||
return bearing;
|
||||
|
||||
final float heading = bearing(user, dest).floatValue();
|
||||
|
||||
bearing = 360 - heading + bearing;
|
||||
bearing = 360 - heading + bearing;
|
||||
|
||||
if (bearing > 360)
|
||||
return bearing - 360;
|
||||
@@ -97,16 +115,19 @@ public class GeoUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a geopoint x meters away of the geopoint supplied. The new geopoint
|
||||
* shares the same latitude as geopoint point, this way they are on the same latitude arc.
|
||||
* Calculates a geopoint x meters away of the geopoint supplied. The new
|
||||
* geopoint shares the same latitude as geopoint point, this way they are on
|
||||
* the same latitude arc.
|
||||
*
|
||||
* @param point central geopoint
|
||||
* @param distance in meters from the geopoint
|
||||
* @param point
|
||||
* central geopoint
|
||||
* @param distance
|
||||
* in meters from the geopoint
|
||||
* @return geopoint that is x meters away from the geopoint supplied
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static GeoPoint distanceFrom(final GeoPoint point, double distance){
|
||||
//convert meters into kilometers
|
||||
public static GeoPoint distanceFrom(final GeoPoint point, double distance) {
|
||||
// convert meters into kilometers
|
||||
distance = distance / 1000;
|
||||
|
||||
// convert lat and lon of geopoint to radians
|
||||
@@ -114,34 +135,45 @@ public class GeoUtils {
|
||||
final double lon1Rad = Math.toRadians(point.getLongitudeE6() / 1e6);
|
||||
|
||||
/*
|
||||
* kilometers = acos(sin(lat1Rad)sin(lat2Rad)+cos(lat1Rad)cos(lat2Rad)cos(lon2Rad-lon1Rad)6371
|
||||
* kilometers =
|
||||
* acos(sin(lat1Rad)sin(lat2Rad)+cos(lat1Rad)cos(lat2Rad)cos
|
||||
* (lon2Rad-lon1Rad)6371
|
||||
*
|
||||
* we are solving this equation for lon2Rad
|
||||
*
|
||||
* lon2Rad = lon1Rad+acos(cos(meters/6371)sec(lat1Rad)sec(lat2Rad)-tan(lat1Rad)tan(lat2Rad))
|
||||
*
|
||||
* lon2Rad =
|
||||
* lon1Rad+acos(cos(meters/6371)sec(lat1Rad)sec(lat2Rad)-tan(lat1Rad
|
||||
* )tan(lat2Rad))
|
||||
*
|
||||
* NOTE: sec(x) = 1/cos(x)
|
||||
*
|
||||
* NOTE: that lat2Rad is = lat1Rad because we want to keep the new geopoint on the same lat arc
|
||||
* therefore i saw no need to create a new variable for lat2Rad,
|
||||
* and simply inputed lat1Rad in place of lat2Rad in the equation
|
||||
* NOTE: that lat2Rad is = lat1Rad because we want to keep the new
|
||||
* geopoint on the same lat arc therefore i saw no need to create a new
|
||||
* variable for lat2Rad, and simply inputed lat1Rad in place of lat2Rad
|
||||
* in the equation
|
||||
*
|
||||
* NOTE: this equation has be tested in the field against another gps device, and the distanceKm() from google
|
||||
* and has been proven to be damn close
|
||||
* NOTE: this equation has be tested in the field against another gps
|
||||
* device, and the distanceKm() from google and has been proven to be
|
||||
* damn close
|
||||
*/
|
||||
final double lon2Rad = lon1Rad + Math.acos( Math.cos(distance/6371) * (1 / Math.cos(lat1Rad))
|
||||
* (1 / Math.cos(lat1Rad)) - Math.tan(lat1Rad) * Math.tan(lat1Rad));
|
||||
final double lon2Rad = lon1Rad + Math.acos(Math.cos(distance / 6371) * (1 / Math.cos(lat1Rad)) * (1 / Math.cos(lat1Rad)) - Math.tan(lat1Rad) * Math.tan(lat1Rad));
|
||||
|
||||
//return a geopoint that is x meters away from the geopoint supplied
|
||||
// return a geopoint that is x meters away from the geopoint supplied
|
||||
return new GeoPoint(point.getLatitudeE6(), (int) (Math.toDegrees(lon2Rad) * 1e6));
|
||||
}
|
||||
|
||||
/**
|
||||
* computes the distance between to lat1/lon1 and lat2/lon2 based on the curve of the earth
|
||||
* @param lat1 source lat
|
||||
* @param lon1 source lon
|
||||
* @param lat2 destination lat
|
||||
* @param lon2 destination lon
|
||||
* computes the distance between to lat1/lon1 and lat2/lon2 based on the
|
||||
* curve of the earth
|
||||
*
|
||||
* @param lat1
|
||||
* source lat
|
||||
* @param lon1
|
||||
* source lon
|
||||
* @param lat2
|
||||
* destination lat
|
||||
* @param lon2
|
||||
* destination lon
|
||||
* @return the distance between to lat1/lon1 and lat2/lon2
|
||||
* @author Google Inc.
|
||||
*/
|
||||
@@ -153,15 +185,17 @@ public class GeoUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* computes the distance between to p1 and p2 based on the curve of the earth
|
||||
* computes the distance between to p1 and p2 based on the curve of the
|
||||
* earth
|
||||
*
|
||||
* @param p1
|
||||
* @param p2
|
||||
* @return the distance between to p1 and p2
|
||||
* @author Google Inc.
|
||||
*/
|
||||
public static double distanceKm(final GeoPoint p1, final GeoPoint p2) {
|
||||
//if we are handed a null, return -1 so we don't break
|
||||
if(p1 == null || p2 == null)
|
||||
// if we are handed a null, return -1 so we don't break
|
||||
if (p1 == null || p2 == null)
|
||||
return -1;
|
||||
|
||||
final double lat1 = p1.getLatitudeE6() / MILLION;
|
||||
@@ -173,8 +207,11 @@ public class GeoUtils {
|
||||
|
||||
/**
|
||||
* Converts distance into a human readbale string
|
||||
* @param distance in kilometers
|
||||
* @param returnMetric true if metric, false for US
|
||||
*
|
||||
* @param distance
|
||||
* in kilometers
|
||||
* @param returnMetric
|
||||
* true if metric, false for US
|
||||
* @return string distance
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@@ -198,31 +235,40 @@ public class GeoUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* a convince method for testing if 2 circles on the the surface of the earth intersect.
|
||||
* we will use this method to test if the users accuracy circle intersects a marked locaton's radius
|
||||
* if ( (accuracyCircleRadius + locationRadius) - fudgeFactor) > acos(sin(lat1Rad)sin(lat2Rad)+cos(lat1Rad)cos(lat2Rad)cos(lon2Rad-lon1Rad)6371
|
||||
* a convince method for testing if 2 circles on the the surface of the
|
||||
* earth intersect. we will use this method to test if the users accuracy
|
||||
* circle intersects a marked locaton's radius if ( (accuracyCircleRadius +
|
||||
* locationRadius) - fudgeFactor) >
|
||||
* acos(sin(lat1Rad)sin(lat2Rad)+cos(lat1Rad
|
||||
* )cos(lat2Rad)cos(lon2Rad-lon1Rad)6371
|
||||
*
|
||||
* @param userPoint
|
||||
* @param accuracyRadius in KM
|
||||
* @param accuracyRadius
|
||||
* in KM
|
||||
* @param locationPoint
|
||||
* @param locationRadius in KM
|
||||
* @param fudgeFactor how many KM the circles have to intersect
|
||||
* @param locationRadius
|
||||
* in KM
|
||||
* @param fudgeFactor
|
||||
* how many KM the circles have to intersect
|
||||
* @return true if the circles intersect
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static boolean isIntersecting(final GeoPoint userPoint, final float accuracyRadius, final GeoPoint locationPoint, final float locationRadius, final float fudgeFactor){
|
||||
if(accuracyRadius + locationRadius - fudgeFactor > distanceKm(locationPoint, userPoint))
|
||||
public static boolean isIntersecting(final GeoPoint userPoint, final float accuracyRadius, final GeoPoint locationPoint, final float locationRadius,
|
||||
final float fudgeFactor) {
|
||||
if (accuracyRadius + locationRadius - fudgeFactor > distanceKm(locationPoint, userPoint))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* determines when the specified point is off the map
|
||||
*
|
||||
* @param point
|
||||
* @return true is the point is off the map
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static boolean isPointOffMap(final MapView map , final GeoPoint point){
|
||||
if(map == null)
|
||||
public static boolean isPointOffMap(final MapView map, final GeoPoint point) {
|
||||
if (map == null)
|
||||
return false;
|
||||
if (point == null)
|
||||
return false;
|
||||
@@ -230,27 +276,30 @@ public class GeoUtils {
|
||||
final double distance = GeoUtils.distanceKm(center, point);
|
||||
final double distanceLat = GeoUtils.distanceKm(center, new GeoPoint(center.getLatitudeE6() + map.getLatitudeSpan() / 2, center.getLongitudeE6()));
|
||||
final double distanceLon = GeoUtils.distanceKm(center, new GeoPoint(center.getLatitudeE6(), center.getLongitudeE6() + map.getLongitudeSpan() / 2));
|
||||
if (distance > distanceLat || distance > distanceLon)
|
||||
if (distance > distanceLat || distance > distanceLon)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* computes a geopoint the is the central geopoint between p1 and p1
|
||||
* @param p1 first geopoint
|
||||
* @param p2 second geopoint
|
||||
*
|
||||
* @param p1
|
||||
* first geopoint
|
||||
* @param p2
|
||||
* second geopoint
|
||||
* @return a MidPoint object
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static MidPoint midPoint(final GeoPoint p1, final GeoPoint p2) {
|
||||
int minLatitude = (int)(+81 * 1E6);
|
||||
int maxLatitude = (int)(-81 * 1E6);
|
||||
int minLongitude = (int)(+181 * 1E6);
|
||||
int maxLongitude = (int)(-181 * 1E6);
|
||||
int minLatitude = (int) (+81 * 1E6);
|
||||
int maxLatitude = (int) (-81 * 1E6);
|
||||
int minLongitude = (int) (+181 * 1E6);
|
||||
int maxLongitude = (int) (-181 * 1E6);
|
||||
final List<Point> mPoints = new ArrayList<Point>();
|
||||
int latitude = p1.getLatitudeE6();
|
||||
int longitude = p1.getLongitudeE6();
|
||||
if (latitude != 0 && longitude !=0) {
|
||||
if (latitude != 0 && longitude != 0) {
|
||||
minLatitude = minLatitude > latitude ? latitude : minLatitude;
|
||||
maxLatitude = maxLatitude < latitude ? latitude : maxLatitude;
|
||||
minLongitude = minLongitude > longitude ? longitude : minLongitude;
|
||||
@@ -260,18 +309,19 @@ public class GeoUtils {
|
||||
|
||||
latitude = p2.getLatitudeE6();
|
||||
longitude = p2.getLongitudeE6();
|
||||
if (latitude != 0 && longitude !=0) {
|
||||
if (latitude != 0 && longitude != 0) {
|
||||
minLatitude = minLatitude > latitude ? latitude : minLatitude;
|
||||
maxLatitude = maxLatitude < latitude ? latitude : maxLatitude;
|
||||
minLongitude = minLongitude > longitude ? longitude : minLongitude;
|
||||
maxLongitude = maxLongitude < longitude ? longitude : maxLongitude;
|
||||
mPoints.add(new Point(latitude, longitude));
|
||||
}
|
||||
return new MidPoint(new GeoPoint((maxLatitude + minLatitude)/2, (maxLongitude + minLongitude)/2 ), minLatitude, minLongitude, maxLatitude, maxLongitude);
|
||||
return new MidPoint(new GeoPoint((maxLatitude + minLatitude) / 2, (maxLongitude + minLongitude) / 2), minLatitude, minLongitude, maxLatitude, maxLongitude);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts radians to bearing
|
||||
*
|
||||
* @param rad
|
||||
* @return bearing
|
||||
* @author Google Inc.
|
||||
|
||||
Reference in New Issue
Block a user