From 8a2f0a9f45b9b4a99ac1e5bb7b1c4570e4847503 Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Mon, 15 May 2017 13:23:04 +1200 Subject: [PATCH] Added unit tests for Mercator projection class. - changed its methods to static - add some documentation for its methods #story[928] --- .../java/seng302/models/map/Boundary.java | 32 +++++++------- .../models/map/MercatorProjection.java | 17 +++----- .../models/map/MercatorProjectionTest.java | 42 +++++++++++++++++++ 3 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 src/test/java/seng302/models/map/MercatorProjectionTest.java diff --git a/src/main/java/seng302/models/map/Boundary.java b/src/main/java/seng302/models/map/Boundary.java index f2d1302c..d39a60f0 100644 --- a/src/main/java/seng302/models/map/Boundary.java +++ b/src/main/java/seng302/models/map/Boundary.java @@ -9,36 +9,36 @@ package seng302.models.map; */ 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) { - this.north = north; - this.east = east; - this.south = south; - this.west = west; + public Boundary(double northLat, double eastLng, double southLat, double westLng) { + this.northLat = northLat; + this.eastLng = eastLng; + this.southLat = southLat; + this.westLng = westLng; } public double getCentreLat() { - return (north + south) / 2; + return (northLat + southLat) / 2; } public double getCentreLng() { - return (east + west) / 2; + return (eastLng + westLng) / 2; } - public double getNorth() { - return north; + public double getNorthLat() { + return northLat; } - public double getEast() { - return east; + public double getEastLng() { + return eastLng; } - public double getSouth() { - return south; + public double getSouthLat() { + return southLat; } - public double getWest() { - return west; + public double getWestLng() { + return westLng; } } diff --git a/src/main/java/seng302/models/map/MercatorProjection.java b/src/main/java/seng302/models/map/MercatorProjection.java index 4a442123..915712ba 100644 --- a/src/main/java/seng302/models/map/MercatorProjection.java +++ b/src/main/java/seng302/models/map/MercatorProjection.java @@ -2,21 +2,16 @@ package seng302.models.map; public class MercatorProjection { - private double MERCATOR_RANGE = 256; - private double pixelsPerLngDegree, pixelsPerLngRadian; - - - public MercatorProjection() { - pixelsPerLngDegree = MERCATOR_RANGE / 360.0; - pixelsPerLngRadian = MERCATOR_RANGE / (2 * Math.PI); - } + private static final double MERCATOR_RANGE = 256; + private static final double pixelsPerLngDegree = MERCATOR_RANGE / 360.0; + private static final double pixelsPerLngRadian = MERCATOR_RANGE / (2 * Math.PI); /** * A help function keeps the value in bound between -0.9999 and 0.9999. * @param value in bound value * @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); } @@ -25,7 +20,7 @@ public class MercatorProjection { * @param geo MapGeo (lat, lng) location to be projected * @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 origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0); point.setX(origin.getX() + geo.getLng() * pixelsPerLngDegree); @@ -42,7 +37,7 @@ public class MercatorProjection { * @param point MapPoint (x, y) to be converted back * @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); double lng = (point.getX() - origin.getX()) / pixelsPerLngDegree; double latRadians = (point.getY() - origin.getY()) / (-pixelsPerLngRadian); diff --git a/src/test/java/seng302/models/map/MercatorProjectionTest.java b/src/test/java/seng302/models/map/MercatorProjectionTest.java new file mode 100644 index 00000000..a4350c18 --- /dev/null +++ b/src/test/java/seng302/models/map/MercatorProjectionTest.java @@ -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); + } + +} \ No newline at end of file