Added unit tests for Mercator projection class.

- changed its methods to static
- add some documentation for its methods

#story[928]
This commit is contained in:
Haoming Yin
2017-05-15 13:23:04 +12:00
parent 5cc865f0af
commit 8a2f0a9f45
3 changed files with 64 additions and 27 deletions
+16 -16
View File
@@ -9,36 +9,36 @@ package seng302.models.map;
*/ */
public class Boundary { public class Boundary {
private double north, east, south, west; private double northLat, eastLng, southLat, westLng;
public Boundary(double north, double east, double south, double west) { public Boundary(double northLat, double eastLng, double southLat, double westLng) {
this.north = north; this.northLat = northLat;
this.east = east; this.eastLng = eastLng;
this.south = south; this.southLat = southLat;
this.west = west; this.westLng = westLng;
} }
public double getCentreLat() { public double getCentreLat() {
return (north + south) / 2; return (northLat + southLat) / 2;
} }
public double getCentreLng() { public double getCentreLng() {
return (east + west) / 2; return (eastLng + westLng) / 2;
} }
public double getNorth() { public double getNorthLat() {
return north; return northLat;
} }
public double getEast() { public double getEastLng() {
return east; return eastLng;
} }
public double getSouth() { public double getSouthLat() {
return south; return southLat;
} }
public double getWest() { public double getWestLng() {
return west; return westLng;
} }
} }
@@ -2,21 +2,16 @@ package seng302.models.map;
public class MercatorProjection { public class MercatorProjection {
private double MERCATOR_RANGE = 256; private static final double MERCATOR_RANGE = 256;
private double pixelsPerLngDegree, pixelsPerLngRadian; private static final double pixelsPerLngDegree = MERCATOR_RANGE / 360.0;
private static final double pixelsPerLngRadian = MERCATOR_RANGE / (2 * Math.PI);
public MercatorProjection() {
pixelsPerLngDegree = MERCATOR_RANGE / 360.0;
pixelsPerLngRadian = MERCATOR_RANGE / (2 * Math.PI);
}
/** /**
* A help function keeps the value in bound between -0.9999 and 0.9999. * A help function keeps the value in bound between -0.9999 and 0.9999.
* @param value in bound value * @param value in bound value
* @return the value in bound * @return the value in bound
*/ */
private double bound(double value) { private static double bound(double value) {
return Math.min(Math.max(value, -0.9999), 0.9999); return Math.min(Math.max(value, -0.9999), 0.9999);
} }
@@ -25,7 +20,7 @@ public class MercatorProjection {
* @param geo MapGeo (lat, lng) location to be projected * @param geo MapGeo (lat, lng) location to be projected
* @return the projection GeoPoint (x, y) on planar * @return the projection GeoPoint (x, y) on planar
*/ */
public MapPoint toMapPoint(MapGeo geo) { public static MapPoint toMapPoint(MapGeo geo) {
MapPoint point = new MapPoint(0, 0); MapPoint point = new MapPoint(0, 0);
MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0); MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
point.setX(origin.getX() + geo.getLng() * pixelsPerLngDegree); point.setX(origin.getX() + geo.getLng() * pixelsPerLngDegree);
@@ -42,7 +37,7 @@ public class MercatorProjection {
* @param point MapPoint (x, y) to be converted back * @param point MapPoint (x, y) to be converted back
* @return the original Geo location converted from the given projection point * @return the original Geo location converted from the given projection point
*/ */
public MapGeo toMapGeo(MapPoint point) { public static MapGeo toMapGeo(MapPoint point) {
MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0); MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
double lng = (point.getX() - origin.getX()) / pixelsPerLngDegree; double lng = (point.getX() - origin.getX()) / pixelsPerLngDegree;
double latRadians = (point.getY() - origin.getY()) / (-pixelsPerLngRadian); double latRadians = (point.getY() - origin.getY()) / (-pixelsPerLngRadian);
@@ -0,0 +1,42 @@
package seng302.models.map;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Unit test for Mercator Project class.
* Created by hyi25 on 15/05/17.
*/
public class MercatorProjectionTest {
@Test
public void toMapPoint() throws Exception {
MapGeo geo1 = new MapGeo(12.485394, 19.38947);
MapPoint actualPoint1 = MercatorProjection.toMapPoint(geo1);
MapPoint expectedPoint1 = new MapPoint(141.78806755555556, 119.0503853635612);
assertEquals(expectedPoint1.getX(), actualPoint1.getX(), 0.0001);
assertEquals(expectedPoint1.getY(), actualPoint1.getY(), 0.0001);
MapGeo geo2 = new MapGeo(77.456432, -23.456462);
MapPoint actualPoint2 = MercatorProjection.toMapPoint(geo2);
MapPoint expectedPoint2 = new MapPoint(111.31984924444444, 38.03143323746788);
assertEquals(expectedPoint2.getX(), actualPoint2.getX(), 0.0001);
assertEquals(expectedPoint2.getY(), actualPoint2.getY(), 0.0001);
}
@Test
public void toMapGeo() throws Exception {
MapPoint point1 = new MapPoint(123.1234, 25.4565);
MapGeo actualGeo1 = MercatorProjection.toMapGeo(point1);
MapGeo expectedGeo1 = new MapGeo(80.77043127275441, -6.857718749999995);
assertEquals(expectedGeo1.getLat(), actualGeo1.getLat(), 0.0001);
assertEquals(expectedGeo1.getLng(), actualGeo1.getLng(), 0.0001);
MapPoint point2 = new MapPoint(1.235, 255.4565);
MapGeo actualGeo2 = MercatorProjection.toMapGeo(point2);
MapGeo expectedGeo2 = new MapGeo(-84.98475532898011, -178.26328125);
assertEquals(expectedGeo2.getLat(), actualGeo2.getLat(), 0.0001);
assertEquals(expectedGeo2.getLng(), actualGeo2.getLng(), 0.0001);
}
}