Merge pull request #5537 from grigoriosdimopoulos/Mercator

Convert Latitude / Longitude to a 2d Point
This commit is contained in:
Eric Martin 2018-11-06 20:53:29 -06:00 committed by GitHub
commit 78dd662f70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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