Mercator: Article example implementation of mercator projections.

This commit is contained in:
Grigorios Dimopoulos 2018-10-26 11:22:09 +03:00
parent 362bd203e9
commit 0187c74f79
5 changed files with 103 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}