Skip to content

Instantly share code, notes, and snippets.

@fdansv
Last active December 23, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdansv/6579158 to your computer and use it in GitHub Desktop.
Save fdansv/6579158 to your computer and use it in GitHub Desktop.
Method that calculates the area of the polygon formed by the given list of Location objects
public double areaPoly(ArrayList<Location> places){
int[] x = new int[places.size()];
int[] y = new int[places.size()];
for (int i = 0; i<places.size(); i++){
x[i] = LocationToEN(places.get(i))[0];
y[i] = LocationToEN(places.get(i))[1];
}
double area = 0.0;
int j = x.length-1;
for(int i = 0; i < x.length; i++) {
area = area + (x[j]+x[i]) * (y[j]-y[i]);
j = i;
}
area = area/2;
if (area < 0)
area = area * -1;
return area;
}
public int[] LocationToEN (Location location){
float lat = (float) location.getLatitude();
float lon = (float) location.getLongitude();
System.out.println("Coordinates: " + lat + "," + lon);
int[] eastingsAndNorthings = new int[2];
uk.me.jstott.jcoord.LatLng coords = new uk.me.jstott.jcoord.LatLng(lat,lon);
UTMRef UTMEN = coords.toUTMRef();
eastingsAndNorthings[0]=(int)UTMEN.getEasting();
eastingsAndNorthings[1]=(int)UTMEN.getNorthing();
System.out.println("E/N: " + UTMEN.toString());
System.out.println("Calc: " + eastingsAndNorthings[0]);
return eastingsAndNorthings;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment