source: josm/trunk/src/org/openstreetmap/josm/data/projection/proj/AlbersEqualArea.java@ 11549

Last change on this file since 11549 was 10378, checked in by Don-vip, 8 years ago

Checkstyle 6.19: enable SingleSpaceSeparator and fix violations

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.proj;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import org.openstreetmap.josm.data.Bounds;
7import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
8
9/**
10 * Albers Equal Area Projection (EPSG code 9822). This is a conic projection with parallels being
11 * unequally spaced arcs of concentric circles, more closely spaced at north and south edges of the
12 * map. Merideans are equally spaced radii of the same circles and intersect parallels at right
13 * angles. As the name implies, this projection minimizes distortion in areas.
14 * <p>
15 * The {@code "standard_parallel_2"} parameter is optional and will be given the same value as
16 * {@code "standard_parallel_1"} if not set (creating a 1 standard parallel projection).
17 * <p>
18 * <b>NOTE:</b>
19 * formulae used below are from a port, to Java, of the {@code proj4}
20 * package of the USGS survey. USGS work is acknowledged here.
21 * <p>
22 * This class has been derived from the implementation of the Geotools project;
23 * git 8cbf52d, org.geotools.referencing.operation.projection.AlbersEqualArea
24 * at the time of migration.
25 * <p>
26 * <b>References:</b>
27 * <ul>
28 * <li> Proj-4.4.7 available at <A HREF="http://www.remotesensing.org/proj">www.remotesensing.org/proj</A><br>
29 * Relevent files are: PJ_aea.c, pj_fwd.c and pj_inv.c </li>
30 * <li> John P. Snyder (Map Projections - A Working Manual,
31 * U.S. Geological Survey Professional Paper 1395, 1987)</li>
32 * <li> "Coordinate Conversions and Transformations including Formulas",
33 * EPSG Guidence Note Number 7, Version 19.</li>
34 * </ul>
35 *
36 * @author Gerald I. Evenden (for original code in Proj4)
37 * @author Rueben Schulz
38 *
39 * @see <A HREF="http://mathworld.wolfram.com/AlbersEqual-AreaConicProjection.html">Albers Equal-Area Conic Projection on MathWorld</A>
40 * @see <A HREF="http://www.remotesensing.org/geotiff/proj_list/albers_equal_area_conic.html">"Albers_Conic_Equal_Area" on RemoteSensing.org</A>
41 * @see <A HREF="http://srmwww.gov.bc.ca/gis/bceprojection.html">British Columbia Albers Standard Projection</A>
42 *
43 * @since 9419
44 */
45public class AlbersEqualArea extends AbstractProj {
46
47 /**
48 * Maximum number of iterations for iterative computations.
49 */
50 private static final int MAXIMUM_ITERATIONS = 15;
51
52 /**
53 * Difference allowed in iterative computations.
54 */
55 private static final double ITERATION_TOLERANCE = 1E-10;
56
57 /**
58 * Maximum difference allowed when comparing real numbers.
59 */
60 private static final double EPSILON = 1E-6;
61
62 /**
63 * Constants used by the spherical and elliptical Albers projection.
64 */
65 private double n, c, rho0;
66
67 /**
68 * An error condition indicating iteration will not converge for the
69 * inverse ellipse. See Snyder (14-20)
70 */
71 private double ec;
72
73 @Override
74 public String getName() {
75 return tr("Albers Equal Area");
76 }
77
78 @Override
79 public String getProj4Id() {
80 return "aea";
81 }
82
83 @Override
84 public void initialize(ProjParameters params) throws ProjectionConfigurationException {
85 super.initialize(params);
86 if (params.lat0 == null)
87 throw new ProjectionConfigurationException(tr("Parameter ''{0}'' required.", "lat_0"));
88 if (params.lat1 == null)
89 throw new ProjectionConfigurationException(tr("Parameter ''{0}'' required.", "lat_1"));
90
91 double lat0 = Math.toRadians(params.lat0);
92 // Standards parallels in radians.
93 double phi1 = Math.toRadians(params.lat1);
94 double phi2 = params.lat2 == null ? phi1 : Math.toRadians(params.lat2);
95
96 // Compute Constants
97 if (Math.abs(phi1 + phi2) < EPSILON) {
98 throw new ProjectionConfigurationException(tr("standard parallels are opposite"));
99 }
100 double sinphi = Math.sin(phi1);
101 double cosphi = Math.cos(phi1);
102 double n = sinphi;
103 boolean secant = Math.abs(phi1 - phi2) >= EPSILON;
104 double m1 = msfn(sinphi, cosphi);
105 double q1 = qsfn(sinphi);
106 if (secant) { // secant cone
107 sinphi = Math.sin(phi2);
108 cosphi = Math.cos(phi2);
109 double m2 = msfn(sinphi, cosphi);
110 double q2 = qsfn(sinphi);
111 n = (m1 * m1 - m2 * m2) / (q2 - q1);
112 }
113 c = m1 * m1 + n * q1;
114 rho0 = Math.sqrt(c - n * qsfn(Math.sin(lat0))) /n;
115 ec = 1.0 - .5 * (1.0-e2) *
116 Math.log((1.0 - e) / (1.0 + e)) / e;
117 this.n = n;
118 }
119
120 @Override
121 public double[] project(double y, double x) {
122 x *= n;
123 double rho = c - n * qsfn(Math.sin(y));
124 if (rho < 0.0) {
125 if (rho > -EPSILON) {
126 rho = 0.0;
127 } else {
128 throw new AssertionError();
129 }
130 }
131 rho = Math.sqrt(rho) / n;
132 // CHECKSTYLE.OFF: SingleSpaceSeparator
133 y = rho0 - rho * Math.cos(x);
134 x = rho * Math.sin(x);
135 // CHECKSTYLE.ON: SingleSpaceSeparator
136 return new double[] {x, y};
137 }
138
139 @Override
140 public double[] invproject(double x, double y) {
141 y = rho0 - y;
142 double rho = Math.hypot(x, y);
143 if (rho > EPSILON) {
144 if (n < 0.0) {
145 rho = -rho;
146 x = -x;
147 y = -y;
148 }
149 x = Math.atan2(x, y) / n;
150 y = rho * n;
151 y = (c - y*y) / n;
152 if (Math.abs(y) <= ec) {
153 y = phi1(y);
154 } else {
155 y = (y < 0.0) ? -Math.PI/2.0 : Math.PI/2.0;
156 }
157 } else {
158 x = 0.0;
159 y = n > 0.0 ? Math.PI/2.0 : -Math.PI/2.0;
160 }
161 return new double[] {y, x};
162 }
163
164 /**
165 * Iteratively solves equation (3-16) from Snyder.
166 *
167 * @param qs arcsin(q/2), used in the first step of iteration
168 * @return the latitude
169 */
170 public double phi1(final double qs) {
171 final double toneEs = 1 - e2;
172 double phi = Math.asin(0.5 * qs);
173 if (e < EPSILON) {
174 return phi;
175 }
176 for (int i = 0; i < MAXIMUM_ITERATIONS; i++) {
177 final double sinpi = Math.sin(phi);
178 final double cospi = Math.cos(phi);
179 final double con = e * sinpi;
180 final double com = 1.0 - con*con;
181 final double dphi = 0.5 * com*com / cospi *
182 (qs/toneEs - sinpi / com + 0.5/e * Math.log((1. - con) / (1. + con)));
183 phi += dphi;
184 if (Math.abs(dphi) <= ITERATION_TOLERANCE) {
185 return phi;
186 }
187 }
188 throw new IllegalStateException("no convergence for qs="+qs);
189 }
190
191 /**
192 * Calculates q, Snyder equation (3-12)
193 *
194 * @param sinphi sin of the latitude q is calculated for
195 * @return q from Snyder equation (3-12)
196 */
197 private double qsfn(final double sinphi) {
198 final double oneEs = 1 - e2;
199 if (e >= EPSILON) {
200 final double con = e * sinphi;
201 return oneEs * (sinphi / (1. - con*con) -
202 (0.5/e) * Math.log((1.-con) / (1.+con)));
203 } else {
204 return sinphi + sinphi;
205 }
206 }
207
208 @Override
209 public Bounds getAlgorithmBounds() {
210 return new Bounds(-89, -180, 89, 180, false);
211 }
212}
Note: See TracBrowser for help on using the repository browser.