001    //$HeadURL: $
002    /*----------------    FILE HEADER  ------------------------------------------
003     This file is part of deegree.
004     Copyright (C) 2001-2008 by:
005     Department of Geography, University of Bonn
006     http://www.giub.uni-bonn.de/deegree/
007     lat/lon GmbH
008     http://www.lat-lon.de
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014     This library is distributed in the hope that it will be useful,
015     but WITHOUT ANY WARRANTY; without even the implied warranty of
016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     Lesser General Public License for more details.
018     You should have received a copy of the GNU Lesser General Public
019     License along with this library; if not, write to the Free Software
020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     Contact:
022    
023     Andreas Poth
024     lat/lon GmbH
025     Aennchenstr. 19
026     53177 Bonn
027     Germany
028     E-Mail: poth@lat-lon.de
029    
030     Prof. Dr. Klaus Greve
031     Department of Geography
032     University of Bonn
033     Meckenheimer Allee 166
034     53115 Bonn
035     Germany
036     E-Mail: greve@giub.uni-bonn.de
037     ---------------------------------------------------------------------------*/
038    
039    package org.deegree.crs.transformations;
040    
041    import java.util.ArrayList;
042    import java.util.List;
043    
044    import javax.vecmath.Point2d;
045    import javax.vecmath.Point3d;
046    
047    import org.deegree.crs.coordinatesystems.ProjectedCRS;
048    import org.deegree.crs.exceptions.CRSException;
049    import org.deegree.crs.projections.Projection;
050    
051    /**
052     * The <code>ProjectionTransform</code> class wraps the access to a projection, by calling it's doProjection.
053     * 
054     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
055     * 
056     * @author last edited by: $Author:$
057     * 
058     * @version $Revision:$, $Date:$
059     * 
060     */
061    
062    public class ProjectionTransform extends CRSTransformation {
063    
064        private Projection projection;
065    
066        /**
067         * @param projectedCRS
068         *            The crs containing a projection.
069         * @param identifier
070         * @param version
071         * @param description
072         * @param areaOfUse
073         */
074        public ProjectionTransform( ProjectedCRS projectedCRS, String identifier, String version, String description,
075                                    String areaOfUse ) {
076            super( projectedCRS.getGeographicCRS(),
077                   projectedCRS,
078                   identifier,
079                   "Projection-Transform",
080                   version,
081                   description,
082                   areaOfUse );
083            this.projection = projectedCRS.getProjection();
084        }
085    
086        /**
087         * @param projectedCRS
088         *            The crs containing a projection.
089         * @param identifier
090         */
091        public ProjectionTransform( ProjectedCRS projectedCRS, String identifier ) {
092            this( projectedCRS, identifier, "Unknown", "Unknown", "Unknown" );
093        }
094    
095        /**
096         * A projectiontransform with an id set to FROM_geoID_TO_projId
097         * 
098         * @param projectedCRS
099         *            The crs containing a projection.
100         * 
101         */
102        public ProjectionTransform( ProjectedCRS projectedCRS ) {
103            this( projectedCRS, new StringBuilder( "FROM_" ).append( projectedCRS.getGeographicCRS().getIdentifier() )
104                                                            .append( "_TO_" )
105                                                            .append( projectedCRS.getIdentifier() )
106                                                            .toString() );
107        }
108    
109        @Override
110        public List<Point3d> doTransform( List<Point3d> srcPts ) {
111            List<Point3d> result = new ArrayList<Point3d>( srcPts.size() );
112            if ( isInverseTransform() ) {
113    //            System.out.println( "An inverse projection transform with incoming points: " + srcPts
114    //                                + " and following projection: "
115    //                                + projection.getIdAndName() );
116                for ( Point3d p : srcPts ) {
117                    try {
118                        Point2d tmp = projection.doInverseProjection( p.x, p.y );
119                        result.add( new Point3d( tmp.x, tmp.y, p.z ) );
120                    } catch ( CRSException e ) {
121                        e.printStackTrace();
122                    }
123                }
124            } else {
125    //            System.out.println( "A projection transform with incoming points: " + srcPts
126    //                                + " and following projection: "
127    //                                + projection.getIdAndName() );
128                for ( Point3d p : srcPts ) {
129                    try {
130                        Point2d tmp = projection.doProjection( p.x, p.y );
131                        result.add( new Point3d( tmp.x, tmp.y, p.z ) );
132                    } catch ( CRSException e ) {
133                        e.printStackTrace();
134                    }
135                }
136            }
137            return result;
138        }
139    
140        @Override
141        public boolean isIdentity() {
142            // a projection cannot be an identity doesn't make a lot of sense.
143            return false;
144        }
145        
146        @Override
147        public String toString(){
148            String tmp = super.toString();
149            tmp += "Projection: " + projection.getName();
150            return tmp;
151        }
152    
153    }