Java program to find the distance between two GPS locations.

Location.java
 
package com.cables.gps;

public class Location {
 private int latDegree;
 private int latMinutes;
 private int latSeconds;
 private char latDirection;
 private int longDegree;
 private int longMinutes;
 private int longSeconds;
 private char longDirection;
 
 private float latitude;
 private float longitude;
 
 private void init() throws Exception {
  latitude = latDegree + (latMinutes / 60.0f) + (latSeconds / 3600.0f);
  if (latDirection == 'N') {
   latitude *= 1f;
  } else if (latDirection == 'S'){
   latitude *= -1f;
  } else {
   throw new Exception("Invalid Latitude Direction: " + latDirection);
  }
  longitude = longDegree + (longMinutes / 60.0f) + (longSeconds / 3600.0f);
  if (longDirection == 'E') {
   longitude *= 1f;
  } else if (longDirection == 'W') {
   longitude *= -1f;
  } else {
   throw new Exception("Invalid Longitude Direction: " + longDirection);
  }
 }
 
 public String toString() {
  String returnString = "";
  try {
   init();
  } catch (Exception e) {   
   e.printStackTrace();
   return null;
  }
  returnString += "(" + latitude + ", " + longitude + ")";
  return returnString;
 }
 
 public void setLatitude(int deg, int min, int sec, char dir) {
  latDegree = deg;
  latMinutes = min;
  latSeconds = sec;
  latDirection = dir;
 }
 
 public void setLongitude(int deg, int min, int sec, char dir) {
  longDegree = deg;
  longMinutes = min;
  longSeconds = sec;
  longDirection = dir;
 }

 public int getLatDegree() {
  return latDegree;
 }

 public void setLatDegree(int latDegree) {
  this.latDegree = latDegree;
 }

 public int getLatMinutes() {
  return latMinutes;
 }

 public void setLatMinutes(int latMinutes) {
  this.latMinutes = latMinutes;
 }

 public int getLatSeconds() {
  return latSeconds;
 }

 public void setLatSeconds(int latSeconds) {
  this.latSeconds = latSeconds;
 }

 public char getLatDirection() {
  return latDirection;
 }

 public void setLatDirection(char latDirection) throws Exception {
  if (latDirection != 'N' && latDirection != 'S') {
   throw new Exception("Invalid Latitude Direction: " + latDirection);   
  }
  this.latDirection = latDirection;
 }

 public int getLongDegree() {
  return longDegree;
 }

 public void setLongDegree(int longDegree) {
  this.longDegree = longDegree;
 }

 public int getLongMinutes() {
  return longMinutes;
 }

 public void setLongMinutes(int longMinutes) {
  this.longMinutes = longMinutes;
 }

 public int getLongSeconds() {
  return longSeconds;
 }

 public void setLongSeconds(int longSeconds) {
  this.longSeconds = longSeconds;
 }

 public char getLongDirection() {
  return longDirection;
 }

 public void setLongDirection(char longDirection) throws Exception {
  if (longDirection != 'E' && longDirection != 'W') {
   throw new Exception("Invalid Longitude Direction: " + longDirection);   
  }
  this.longDirection = longDirection;
 }

 public float getLatitude() throws Exception {
  init();
  return latitude;
 }

 public void setLatitude(float latitude) {
  this.latitude = latitude;
 }

 public float getLongitude() throws Exception {  
  init();
  return longitude;
 }

 public void setLongitude(float longitude) {
  this.longitude = longitude;
 }  
 
 public void setLatString(String latStr) throws Exception {
  latStr = latStr.trim();
  String[] values = latStr.split(" ");
  latDegree = Integer.parseInt(values[0].trim());
  latMinutes = Integer.parseInt(values[1].trim());
  latSeconds = Integer.parseInt(values[2].trim());
  latDirection = values[3].trim().charAt(0);
  if (latDirection != 'N' && latDirection != 'S') {   
   throw new Exception("Invalid Latitude Direction: " + latDirection);   
  }
 }
 
 public void setLongString(String longStr) throws Exception {  
  longStr = longStr.trim();
  String[] values = longStr.split(" ");
  longDegree = Integer.parseInt(values[0].trim());
  longMinutes = Integer.parseInt(values[1].trim());
  longSeconds = Integer.parseInt(values[2].trim());
  longDirection = values[3].trim().charAt(0);
  if (longDirection != 'E' && longDirection != 'W') {
   throw new Exception("Invalid Longitude Direction: " + longDirection);   
  }
 }
}
LocationOps.java
 
package com.cables.gps;

public class LocationOps {

 private Location location1 = null;
 private Location location2 = null;
 private int radius = 6371;
 
 
 public LocationOps(Location location1, Location location2) {
  this.location1 = location1;
  this.location2 = location2;
 }
 
 public double calculateDistance() throws Exception {
  double distance = 0.0;
  double lat1 = degreesToRadians(location1.getLatitude());
  double lat2 = degreesToRadians(location2.getLatitude());
  double dLat = degreesToRadians(
    location2.getLatitude() - location1.getLatitude());
  double dLong = degreesToRadians(
    location2.getLongitude() - location1.getLongitude());
  double a = Math.sin(dLat/2.0) * Math.sin(dLat/2.0)
     + Math.cos(lat1) * Math.cos(lat2) *
     Math.sin(dLong/2.0) * Math.sin(dLong/2.0);
  double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  distance = radius * c;
  return distance;
 }
 
 public double degreesToRadians(double degrees) {
  double radians = 0.0;
  radians = degrees * Math.PI / 180.0;
  return radians;
 }
 
 public static void main(String[] args) throws Exception {
  Location loc1 = new Location();
  loc1.setLatDegree(50);
  loc1.setLatMinutes(03);
  loc1.setLatSeconds(59);
  loc1.setLatDirection('N');
  loc1.setLongDegree(005);
  loc1.setLongMinutes(42);
  loc1.setLongSeconds(53);
  loc1.setLongDirection('W');
  Location loc2 = new Location();
  loc2.setLatDegree(58);
  loc2.setLatMinutes(38);
  loc2.setLatSeconds(38);
  loc2.setLatDirection('N');
  loc2.setLongDegree(003);
  loc2.setLongMinutes(04);
  loc2.setLongSeconds(12);
  loc2.setLongDirection('W');
  System.out.println(new LocationOps(loc1, loc2).calculateDistance());
  loc1.setLatString("50 03 59 N");
  loc1.setLongString("005 42 53 W");
  loc2.setLatString("58 38 38 N");
  loc2.setLongString("003 04 12 W");
  System.out.println(new LocationOps(loc1, loc2).calculateDistance());
 }

}

Post a Comment

0 Comments