001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/crs/projections/cylindric/Mercator.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005     Department of Geography, University of Bonn
006     and
007     lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035     ----------------------------------------------------------------------------*/
036    
037    package org.deegree.crs.projections.cylindric;
038    
039    import static org.deegree.crs.projections.ProjectionUtils.calcPhiFromConformalLatitude;
040    import static org.deegree.crs.projections.ProjectionUtils.preCalcedThetaSeries;
041    
042    import javax.vecmath.Point2d;
043    
044    import org.deegree.crs.Identifiable;
045    import org.deegree.crs.components.Unit;
046    import org.deegree.crs.coordinatesystems.GeographicCRS;
047    import org.deegree.crs.exceptions.ProjectionException;
048    import org.deegree.crs.projections.ProjectionUtils;
049    import org.deegree.framework.log.ILogger;
050    import org.deegree.framework.log.LoggerFactory;
051    
052    /**
053     * The <code>Mercator</code> projection has following properties:
054     * <ul>
055     * <li>Cylindircal</li>
056     * <li>Conformal</li>
057     * <li>Meridians are equally spaced straight lines</li>
058     * <li>Parallels are unequally spaced straight lines closest near the equator, cutting meridians at right angles.</li>
059     * <li>Scale is true along the Equator, or along two parallels equidistant from the Equator</li>
060     * <li>Loxodromes (rhumb lines) are straight lines</li>
061     * <li>Not perspective</li>
062     * <li>Poles are at infinity; great distortion or area in polar regions</li>
063     * <li>Used for navigation</li>
064     * <li>Presented by Mercator in 1569</li>
065     * </ul>
066     * 
067     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
068     * 
069     * @author last edited by: $Author: rbezema $
070     * 
071     * @version $Revision: 19653 $, $Date: 2009-09-15 14:56:30 +0200 (Di, 15 Sep 2009) $
072     * 
073     */
074    public class Mercator extends CylindricalProjection {
075    
076        private static final long serialVersionUID = -8410660923891924617L;
077    
078        private static ILogger LOG = LoggerFactory.getLogger( Mercator.class );
079    
080        private double[] preCalcedPhiSeries;
081    
082        /**
083         * @param geographicCRS
084         * @param falseNorthing
085         * @param falseEasting
086         * @param naturalOrigin
087         * @param units
088         * @param scale
089         * @param id
090         *            an identifiable instance containing information about this projection
091         */
092        public Mercator( GeographicCRS geographicCRS, double falseNorthing, double falseEasting, Point2d naturalOrigin,
093                         Unit units, double scale, Identifiable id ) {
094            super( geographicCRS, falseNorthing, falseEasting, naturalOrigin, units, scale, true, false, id );
095            preCalcedPhiSeries = preCalcedThetaSeries( getSquaredEccentricity() );
096        }
097    
098        /**
099         * Sets the id to EPSG:9804
100         * 
101         * @param geographicCRS
102         * @param falseNorthing
103         * @param falseEasting
104         * @param naturalOrigin
105         * @param units
106         * @param scale
107         */
108        public Mercator( GeographicCRS geographicCRS, double falseNorthing, double falseEasting, Point2d naturalOrigin,
109                         Unit units, double scale ) {
110            this( geographicCRS, falseNorthing, falseEasting, naturalOrigin, units, scale, new Identifiable( "EPSG::9804" ) );
111        }
112    
113        @Override
114        public Point2d doInverseProjection( double x, double y )
115                                throws ProjectionException {
116            if ( LOG.isDebug() ) {
117                LOG.logDebug( "(Mercator incoming inv proj) x:" + x + ", y:" + y );
118            }
119            Point2d result = new Point2d( 0, 0 );
120            LOG.logDebug( "InverseProjection, incoming points x: " + x + " y: " + y );
121            x -= getFalseEasting();
122            y -= getFalseNorthing();
123    
124            result.x = ( x / getScaleFactor() ) + getProjectionLongitude();
125            result.y = ProjectionUtils.HALFPI - 2. * Math.atan( Math.exp( -y / getScaleFactor() ) );
126            if ( !isSpherical() ) {
127                result.y = calcPhiFromConformalLatitude( result.y, preCalcedPhiSeries );
128            }
129            if ( LOG.isDebug() ) {
130                LOG.logDebug( "(outgoing) lam:" + Math.toDegrees( result.x ) + ", phi:" + Math.toDegrees( result.y ) );
131            }
132            return result;
133        }
134    
135        @Override
136        public Point2d doProjection( double lambda, double phi )
137                                throws ProjectionException {
138            if ( LOG.isDebug() ) {
139                LOG.logDebug( "(Mercator incoming proj) lam: " + Math.toDegrees( lambda ) + ", phi:" + Math.toDegrees( phi ) );
140            }
141            Point2d result = new Point2d( 0, 0 );
142            lambda -= getProjectionLongitude();
143    
144            result.x = getScaleFactor() * lambda;
145            if ( isSpherical() ) {
146                result.y = getScaleFactor() * Math.log( Math.tan( ProjectionUtils.QUARTERPI + 0.5 * phi ) );
147            } else {
148                result.y = -getScaleFactor()
149                           * Math.log( ProjectionUtils.tanHalfCoLatitude( phi, Math.sin( phi ), getEccentricity() ) );
150            }
151            result.x += getFalseEasting();
152            result.y += getFalseNorthing();
153            if ( LOG.isDebug() ) {
154                LOG.logDebug( "(outgoing) x:" + result.x + ", y:" + result.y );
155            }
156            return result;
157        }
158    
159        @Override
160        public String getImplementationName() {
161            return "mercator";
162        }
163    
164    }