Merge pull request #5537 from grigoriosdimopoulos/Mercator
Convert Latitude / Longitude to a 2d Point
This commit is contained in:
commit
78dd662f70
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.algorithms.mercator;
|
||||
|
||||
class EllipticalMercator extends Mercator {
|
||||
|
||||
@Override
|
||||
double yAxisProjection(double input) {
|
||||
|
||||
input = Math.min(Math.max(input, -89.5), 89.5);
|
||||
double earthDimensionalRateNormalized = 1.0 - Math.pow(RADIUS_MINOR / RADIUS_MAJOR, 2);
|
||||
|
||||
double inputOnEarthProj = Math.sqrt(earthDimensionalRateNormalized) * Math.sin( Math.toRadians(input));
|
||||
|
||||
inputOnEarthProj = Math.pow(((1.0 - inputOnEarthProj)/(1.0+inputOnEarthProj)), 0.5 * Math.sqrt(earthDimensionalRateNormalized));
|
||||
double inputOnEarthProjNormalized = Math.tan(0.5 * ((Math.PI*0.5) - Math.toRadians(input)))/inputOnEarthProj;
|
||||
return (-1) * RADIUS_MAJOR * Math.log(inputOnEarthProjNormalized);
|
||||
}
|
||||
|
||||
@Override
|
||||
double xAxisProjection(double input) {
|
||||
return RADIUS_MAJOR * Math.toRadians(input);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.algorithms.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);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.algorithms.mercator;
|
||||
|
||||
public class SphericalMercator extends Mercator {
|
||||
|
||||
@Override
|
||||
double xAxisProjection(double input) {
|
||||
return Math.toRadians(input) * RADIUS_MAJOR;
|
||||
}
|
||||
|
||||
@Override
|
||||
double yAxisProjection(double input) {
|
||||
return Math.log(Math.tan(Math.PI / 4 + Math.toRadians(input) / 2)) * RADIUS_MAJOR;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.algorithms.mercator;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class EllipticalMercatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void giventThatTheInputIs22_whenXAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() {
|
||||
Mercator mercator = new EllipticalMercator();
|
||||
double result = mercator.xAxisProjection(22);
|
||||
Assert.assertEquals(result, 2449028.7974520186, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giventThatTheInputIs44_whenYAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() {
|
||||
Mercator mercator = new EllipticalMercator();
|
||||
double result = mercator.yAxisProjection(44);
|
||||
Assert.assertEquals(result, 5435749.887511954, 0.0);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.algorithms.mercator;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SphericalMercatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void giventThatTheInputIs22_whenXAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() {
|
||||
Mercator mercator = new SphericalMercator();
|
||||
double result = mercator.xAxisProjection(22);
|
||||
Assert.assertEquals(result, 2449028.7974520186, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giventThatTheInputIs44_whenYAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() {
|
||||
Mercator mercator = new SphericalMercator();
|
||||
double result = mercator.yAxisProjection(44);
|
||||
Assert.assertEquals(result, 5465442.183322753, 0.0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue