Class for managing locations within a world.

Here is a class I wrote to handle locations within my world. I use Latitude and Longitude and this class will let me work out the distance between 2 points as well. (Not sure the maths is 100% correct, but it could be useful to someone)

Andy.


public class GPSLocation {

  private static final int worldRadius = 6378; // Earth radius in km.

  private float latDegrees;
  private int   latMinutes;
  private int   latSeconds;
  private char  latDirection;

  private float lonDegrees;
  private int   lonMinutes;
  private int   lonSeconds;
  private char  lonDirection;

  private int   altitude; // Measured in meters above sea level.

  public GPSLocation() {
    latDegrees = 0;
    latMinutes = 0;
    latSeconds = 0;
    latDirection = 'N';

    lonDegrees = 0;
    lonMinutes = 0;
    lonSeconds = 0;
    lonDirection = 'E';

    altitude = 0;
  }

  // Get methods.
  public float getLatDegrees() {
    return latDegrees;
  }

  public int getLatMinutes() {
    return latMinutes;
  }

  public int getLatSeconds() {
    return latSeconds;
  }

  public char getLatDirection() {
    return latDirection;
  }

  public float getLonDegrees() {
    return lonDegrees;
  }

  public int getLonMinutes() {
    return lonMinutes;
  }

  public int getLonSeconds() {
    return lonSeconds;
  }

  public char getLonDirection() {
    return lonDirection;
  }

  public int getAltitude() {
    return altitude;
  }

  public double getLatRadians() {
    double res1 = latDegrees + (latMinutes + latSeconds/60) /60;
    if (latDirection == 'S') {
      res1 *= -1;
    }
    return Math.toRadians(res1);
  }

  public double getLonRadians() {
    double res1 = lonDegrees + (lonMinutes + lonSeconds/60) /60;
    if (lonDirection == 'W') {
      res1 *= -1;
    }
    return Math.toRadians(res1);
  }

  // Set methods.
  public void setLatDegrees(float pLatDegrees) {
    latDegrees = pLatDegrees;
  }

  public void setLatMinutes(int pLatMinutes) {
    latMinutes =  pLatMinutes;
  }

  public void setLatSeconds(int pLatSeconds) {
    latSeconds = pLatSeconds;
  }

  public void setLatDirection(char pLatDirection) throws Exception {
    char dir = Character.toUpperCase(pLatDirection);
    if ((dir == 'N') || (dir == 'S')) {
      latDirection = dir;
    } else {
      throw new Exception("Invalid direction for Latitude, (N)orth or (S)outh only.");
    }
  }

  public void setLonDegrees(float pLonDegrees) {
    lonDegrees = pLonDegrees;
  }

  public void setLonMinutes(int pLonMinutes) {
    lonMinutes =  pLonMinutes;
  }

  public void setLonSeconds(int pLonSeconds) {
    lonSeconds = pLonSeconds;
  }

  public void setLonDirection(char pLonDirection) throws Exception {
    char dir = Character.toUpperCase(pLonDirection);
    if ((dir == 'E') || (dir == 'W')) {
      lonDirection = dir;
    } else {
      throw new Exception("Invalid direction for Longitude, (E)ast or (W)est only.");
    }
  }

  public void setAltitude(int pAltitude) {
    altitude = pAltitude;
  }

  // Utility methods.
  public double getDistance(GPSLocation otherLocation) {
    double a = this.getLatRadians();
    double b = this.getLonRadians();
    double c = otherLocation.getLatRadians();
    double d = otherLocation.getLonRadians();

    // This calculation should take into account the altitude of the 2 locations.

    if ((a == c) && (b == d)) {
      return 0;
    }

    double res1 = Math.sin(a) * Math.sin(c) + Math.cos(a) * Math.cos(c) * Math.cos(b-d);

    if (res1 > 1) {
      return worldRadius  * Math.acos(1);
    } else {
      return worldRadius  * Math.acos(res1);
    }
  }

} // Class

I posted this code a while ago, but no comments have been made about it, not that I expected loads of them, but I’d be interested to hear peoples thoughts on this class.

Each of the objects in my world has an attribute location, which is of type GPSLocation, its a nice handy utility especially for games that required real-world type locations, rather than just x,y,z.

Has anyone had a chance to verify the maths I’m using? I can’t remeber the site where I got it from, but I have seen other bits of code that gives different answers.

Andy.

At first glance … I would change

private char latDirection to

private short latDirection.

Same with lonDirection. Go on to declare your ‘compass’ in this manner …

public static final short N = 0;
…etc

Refactor your sets and gets …

This will save you time of comparing strings. Your new test will be similiar to

if(direction == N)

If you add more compass directions … then you can modify the if/else tests to a switch statement. It will also allow you to scale fairly easy. Like the following

public static final short N = 0;
public static final short NORTH = 0;

if(dir == N) // will handle N and NORTH.

now you would save yourself the trouble of

char dir = Character.toUpperCase(pLatDirection);

only to test

if((dir == “N”) || (dir == “NORTH”))

and as you add more directions … you would do

switch(dir){
case N: // falls through to NORTH
case NORTH:
doSomething;
break;
case NE: // falls through to NORTHEAST
case NORTHEAST:
doSomething();
break;
etc…
}

where adding these as strings would result in

char dir = Character.toUpperCase(pLatDirection);

if((dir == “N”) || (dir == “NORTH”)) {
doSomething();
} else if((dir == “NE”) || (dir == “NORTHEAST”)) {
doSomething();
} etc …

I hope I haven’t confused you by overexplaining. In java … short and char are both 2 bytes. The char will not allow you to account for NORTHEAST clearly where a String would, but the compares are somewhat expensive. That’s why I am suggesting the use of a short instead of an int. If I still have you in the dark … shoot me an email and I will send you some code that demonstrates the use of shorts over a char or String.

Ray