Skip to content

Commit 78dd662

Browse files
authored
Merge pull request eugenp#5537 from grigoriosdimopoulos/Mercator
Convert Latitude / Longitude to a 2d Point
2 parents 5a5c5c3 + cc54876 commit 78dd662

5 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.algorithms.mercator;
2+
3+
class EllipticalMercator extends Mercator {
4+
5+
@Override
6+
double yAxisProjection(double input) {
7+
8+
input = Math.min(Math.max(input, -89.5), 89.5);
9+
double earthDimensionalRateNormalized = 1.0 - Math.pow(RADIUS_MINOR / RADIUS_MAJOR, 2);
10+
11+
double inputOnEarthProj = Math.sqrt(earthDimensionalRateNormalized) * Math.sin( Math.toRadians(input));
12+
13+
inputOnEarthProj = Math.pow(((1.0 - inputOnEarthProj)/(1.0+inputOnEarthProj)), 0.5 * Math.sqrt(earthDimensionalRateNormalized));
14+
double inputOnEarthProjNormalized = Math.tan(0.5 * ((Math.PI*0.5) - Math.toRadians(input)))/inputOnEarthProj;
15+
return (-1) * RADIUS_MAJOR * Math.log(inputOnEarthProjNormalized);
16+
}
17+
18+
@Override
19+
double xAxisProjection(double input) {
20+
return RADIUS_MAJOR * Math.toRadians(input);
21+
}
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.algorithms.mercator;
2+
3+
abstract class Mercator {
4+
final static double RADIUS_MAJOR = 6378137.0;
5+
final static double RADIUS_MINOR = 6356752.3142;
6+
7+
abstract double yAxisProjection(double input);
8+
9+
abstract double xAxisProjection(double input);
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.algorithms.mercator;
2+
3+
public class SphericalMercator extends Mercator {
4+
5+
@Override
6+
double xAxisProjection(double input) {
7+
return Math.toRadians(input) * RADIUS_MAJOR;
8+
}
9+
10+
@Override
11+
double yAxisProjection(double input) {
12+
return Math.log(Math.tan(Math.PI / 4 + Math.toRadians(input) / 2)) * RADIUS_MAJOR;
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.algorithms.mercator;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
7+
public class EllipticalMercatorUnitTest {
8+
9+
@Test
10+
public void giventThatTheInputIs22_whenXAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() {
11+
Mercator mercator = new EllipticalMercator();
12+
double result = mercator.xAxisProjection(22);
13+
Assert.assertEquals(result, 2449028.7974520186, 0.0);
14+
}
15+
16+
@Test
17+
public void giventThatTheInputIs44_whenYAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() {
18+
Mercator mercator = new EllipticalMercator();
19+
double result = mercator.yAxisProjection(44);
20+
Assert.assertEquals(result, 5435749.887511954, 0.0);
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.algorithms.mercator;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class SphericalMercatorUnitTest {
7+
8+
@Test
9+
public void giventThatTheInputIs22_whenXAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() {
10+
Mercator mercator = new SphericalMercator();
11+
double result = mercator.xAxisProjection(22);
12+
Assert.assertEquals(result, 2449028.7974520186, 0.0);
13+
}
14+
15+
@Test
16+
public void giventThatTheInputIs44_whenYAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() {
17+
Mercator mercator = new SphericalMercator();
18+
double result = mercator.yAxisProjection(44);
19+
Assert.assertEquals(result, 5465442.183322753, 0.0);
20+
}
21+
}

0 commit comments

Comments
 (0)