From 0187c74f792cf39feda799508e74ce1478c9b964 Mon Sep 17 00:00:00 2001 From: Grigorios Dimopoulos Date: Fri, 26 Oct 2018 11:22:09 +0300 Subject: [PATCH] Mercator: Article example implementation of mercator projections. --- .../baeldung/mercator/EllipticalMercator.java | 29 +++++++++++++++++++ .../java/com/baeldung/mercator/Mercator.java | 10 +++++++ .../baeldung/mercator/SphericalMercator.java | 14 +++++++++ .../mercator/EllipticalMercatorUnitTest.java | 25 ++++++++++++++++ .../mercator/SphericalMercatorUnitTest.java | 25 ++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/mercator/EllipticalMercator.java create mode 100644 core-java/src/main/java/com/baeldung/mercator/Mercator.java create mode 100644 core-java/src/main/java/com/baeldung/mercator/SphericalMercator.java create mode 100644 core-java/src/test/java/com/baeldung/mercator/EllipticalMercatorUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/mercator/SphericalMercatorUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/mercator/EllipticalMercator.java b/core-java/src/main/java/com/baeldung/mercator/EllipticalMercator.java new file mode 100644 index 0000000000..bb9ebb0e58 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/mercator/EllipticalMercator.java @@ -0,0 +1,29 @@ +package com.baeldung.mercator; + +class EllipticalMercator extends Mercator { + @Override + double yAxisProjection(double input) { + if (input > 89.5) { + input = 89.5; + } + if (input < -89.5) { + input = -89.5; + } + double temp = RADIUS_MINOR / RADIUS_MAJOR; + double es = 1.0 - (temp * temp); + double eccent = Math.sqrt(es); + double phi = Math.toRadians(input); + double sinphi = Math.sin(phi); + double con = eccent * sinphi; + double com = 0.5 * eccent; + con = Math.pow(((1.0 - con)/(1.0+con)), com); + double ts = Math.tan(0.5 * ((Math.PI*0.5) - phi))/con; + double y = 0 - RADIUS_MAJOR * Math.log(ts); + return y; + } + + @Override + double xAxisProjection(double input) { + return RADIUS_MAJOR * Math.toRadians(input); + } +} diff --git a/core-java/src/main/java/com/baeldung/mercator/Mercator.java b/core-java/src/main/java/com/baeldung/mercator/Mercator.java new file mode 100644 index 0000000000..3f50884f6b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/mercator/Mercator.java @@ -0,0 +1,10 @@ +package com.baeldung.mercator; + +abstract class Mercator { + final static double RADIUS_MAJOR = 6378137.0; + final static double RADIUS_MINOR = 6356752.3142; + + abstract double yAxisProjection(double input); + + abstract double xAxisProjection(double input); +} diff --git a/core-java/src/main/java/com/baeldung/mercator/SphericalMercator.java b/core-java/src/main/java/com/baeldung/mercator/SphericalMercator.java new file mode 100644 index 0000000000..6cc405b3b0 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/mercator/SphericalMercator.java @@ -0,0 +1,14 @@ +package com.baeldung.mercator; + +public class SphericalMercator extends Mercator { + + @Override + double yAxisProjection(double input) { + return Math.toRadians(input) * RADIUS_MAJOR; + } + + @Override + double xAxisProjection(double input) { + return Math.log(Math.tan(Math.PI / 4 + Math.toRadians(input) / 2)) * RADIUS_MAJOR; + } +} diff --git a/core-java/src/test/java/com/baeldung/mercator/EllipticalMercatorUnitTest.java b/core-java/src/test/java/com/baeldung/mercator/EllipticalMercatorUnitTest.java new file mode 100644 index 0000000000..0088cc451c --- /dev/null +++ b/core-java/src/test/java/com/baeldung/mercator/EllipticalMercatorUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.mercator; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import static junit.framework.TestCase.assertEquals; + +@RunWith(MockitoJUnitRunner.class) +public class EllipticalMercatorUnitTest { + + @Test + public void giventThatTheInputIs22_whenXAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() { + Mercator mercator = new EllipticalMercator(); + double result = mercator.xAxisProjection(22); + assertEquals(result, 2449028.7974520186); + } + + @Test + public void giventThatTheInputIs44_whenYAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() { + Mercator mercator = new EllipticalMercator(); + double result = mercator.yAxisProjection(44); + assertEquals(result, 5435749.887511954); + } +} diff --git a/core-java/src/test/java/com/baeldung/mercator/SphericalMercatorUnitTest.java b/core-java/src/test/java/com/baeldung/mercator/SphericalMercatorUnitTest.java new file mode 100644 index 0000000000..2e5ebe53cb --- /dev/null +++ b/core-java/src/test/java/com/baeldung/mercator/SphericalMercatorUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.mercator; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import static junit.framework.TestCase.assertEquals; + +@RunWith(MockitoJUnitRunner.class) +public class SphericalMercatorUnitTest { + + @Test + public void giventThatTheInputIs22_whenXAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() { + Mercator mercator = new SphericalMercator(); + double result = mercator.xAxisProjection(22); + assertEquals(result, 2511525.234845713); + } + + @Test + public void giventThatTheInputIs44_whenYAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() { + Mercator mercator = new SphericalMercator(); + double result = mercator.yAxisProjection(44); + assertEquals(result, 4898057.594904037); + } +}