001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.projection.proj; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import org.openstreetmap.josm.data.Bounds; 007import org.openstreetmap.josm.data.projection.ProjectionConfigurationException; 008 009/** 010 * The polar case of the stereographic projection. 011 * <p> 012 * In the proj.4 library, the code "stere" covers several variants of the 013 * Stereographic projection, depending on the latitude of natural origin 014 * (parameter lat_0). 015 * <p> 016 * 017 * In this file, only the polar case is implemented. This corresponds to 018 * EPSG:9810 (Polar Stereographic Variant A) and EPSG:9829 (Polar Stereographic 019 * Variant B). 020 * <p> 021 * 022 * It is required, that the latitude of natural origin has the value +/-90 degrees. 023 * <p> 024 * 025 * This class has been derived from the implementation of the Geotools project; 026 * git 8cbf52d, org.geotools.referencing.operation.projection.PolarStereographic 027 * at the time of migration. 028 * <p> 029 * 030 * <b>References:</b> 031 * <ul> 032 * <li>John P. Snyder (Map Projections - A Working Manual,<br> 033 * U.S. Geological Survey Professional Paper 1395, 1987)</li> 034 * <li>"Coordinate Conversions and Transformations including Formulas",<br> 035 * EPSG Guidence Note Number 7, Version 19.</li> 036 * <li>Gerald Evenden. <A HREF="http://members.bellatlantic.net/~vze2hc4d/proj4/sterea.pdf"> 037 * "Supplementary PROJ.4 Notes - Oblique Stereographic Alternative"</A></li> 038 * <li>Krakiwsky, E.J., D.B. Thomson, and R.R. Steeves. 1977. A Manual 039 * For Geodetic Coordinate Transformations in the Maritimes. 040 * Geodesy and Geomatics Engineering, UNB. Technical Report No. 48.</li> 041 * <li>Thomson, D.B., M.P. Mepham and R.R. Steeves. 1977. 042 * The Stereographic Double Projection. 043 * Geodesy and Geomatics Engineereng, UNB. Technical Report No. 46.</li> 044 * </ul> 045 * 046 * @author André Gosselin 047 * @author Martin Desruisseaux (PMO, IRD) 048 * @author Rueben Schulz 049 * 050 * @see <A HREF="http://mathworld.wolfram.com/StereographicProjection.html">Stereographic projection on MathWorld</A> 051 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/polar_stereographic.html">Polar_Stereographic</A> 052 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/oblique_stereographic.html">Oblique_Stereographic</A> 053 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/stereographic.html">Stereographic</A> 054 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/random_issues.html#stereographic">Some Random Stereographic Issues</A> 055 * 056 * @see DoubleStereographic 057 * @since 9419 058 */ 059public class PolarStereographic extends AbstractProj { 060 /** 061 * Maximum number of iterations for iterative computations. 062 */ 063 private static final int MAXIMUM_ITERATIONS = 15; 064 065 /** 066 * Difference allowed in iterative computations. 067 */ 068 private static final double ITERATION_TOLERANCE = 1E-10; 069 070 /** 071 * Maximum difference allowed when comparing real numbers. 072 */ 073 private static final double EPSILON = 1E-8; 074 075 /** 076 * A constant used in the transformations. 077 */ 078 private double k0; 079 080 /** 081 * Latitude of true scale, in radians 082 */ 083 private double latitudeTrueScale; 084 085 /** 086 * {@code true} if this projection is for the south pole, or {@code false} 087 * if it is for the north pole. 088 */ 089 boolean southPole; 090 091 @Override 092 public String getName() { 093 return tr("Polar Stereographic"); 094 } 095 096 @Override 097 public String getProj4Id() { 098 return "stere"; 099 } 100 101 @Override 102 public void initialize(ProjParameters params) throws ProjectionConfigurationException { 103 super.initialize(params); 104 if (params.lat0 == null) 105 throw new ProjectionConfigurationException(tr("Parameter ''{0}'' required.", "lat_0")); 106 if (params.lat0 != 90.0 && params.lat0 != -90.0) 107 throw new ProjectionConfigurationException( 108 tr("Polar Stereographic: Parameter ''{0}'' must be 90 or -90.", "lat_0")); 109 if (params.lat_ts == null) { 110 latitudeTrueScale = (params.lat0 < 0) ? -Math.PI/2 : Math.PI/2; 111 } else { 112 latitudeTrueScale = Math.toRadians(params.lat_ts); 113 } 114 southPole = latitudeTrueScale < 0; 115 116 // Computes coefficients. 117 double latitudeTrueScaleAbs = Math.abs(latitudeTrueScale); 118 if (Math.abs(latitudeTrueScaleAbs - Math.PI/2) >= EPSILON) { 119 final double t = Math.sin(latitudeTrueScaleAbs); 120 k0 = msfn(t, Math.cos(latitudeTrueScaleAbs)) / 121 tsfn(latitudeTrueScaleAbs, t); // Derives from (21-32 and 21-33) 122 } else { 123 // True scale at pole (part of (21-33)) 124 k0 = 2.0 / Math.sqrt(Math.pow(1+e, 1+e)* 125 Math.pow(1-e, 1-e)); 126 } 127 } 128 129 @Override 130 public double[] project(double y, double x) { 131 final double sinlat = Math.sin(y); 132 final double coslon = Math.cos(x); 133 final double sinlon = Math.sin(x); 134 if (southPole) { 135 final double rho = k0 * tsfn(-y, -sinlat); 136 x = rho * sinlon; 137 y = rho * coslon; 138 } else { 139 final double rho = k0 * tsfn(y, sinlat); 140 x = rho * sinlon; 141 y = -rho * coslon; 142 } 143 return new double[] {x, y}; 144 } 145 146 @Override 147 public double[] invproject(double x, double y) { 148 final double rho = Math.hypot(x, y); 149 if (southPole) { 150 y = -y; 151 } 152 /* 153 * Compute latitude using iterative technique. 154 */ 155 final double t = rho/k0; 156 final double halfe = e/2.0; 157 double phi0 = 0; 158 for (int i = MAXIMUM_ITERATIONS;;) { 159 final double esinphi = e * Math.sin(phi0); 160 final double phi = (Math.PI/2) - 2.0*Math.atan(t*Math.pow((1-esinphi)/(1+esinphi), halfe)); 161 if (Math.abs(phi-phi0) < ITERATION_TOLERANCE) { 162 x = (Math.abs(rho) < EPSILON) ? 0.0 : Math.atan2(x, -y); 163 y = southPole ? -phi : phi; 164 break; 165 } 166 phi0 = phi; 167 if (--i < 0) { 168 throw new RuntimeException("no convergence"); 169 } 170 } 171 return new double[] {y, x}; 172 } 173 174 @Override 175 public Bounds getAlgorithmBounds() { 176 final double CUT = 60; 177 if (southPole) { 178 return new Bounds(-90, -180, CUT, 180, false); 179 } else { 180 return new Bounds(-CUT, -180, 90, 180, false); 181 } 182 } 183}