001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/crs/CRSFactory.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    package org.deegree.model.crs;
037    
038    import javax.vecmath.Point2d;
039    
040    import org.deegree.crs.Identifiable;
041    import org.deegree.crs.components.Axis;
042    import org.deegree.crs.components.Ellipsoid;
043    import org.deegree.crs.components.GeodeticDatum;
044    import org.deegree.crs.components.Unit;
045    import org.deegree.crs.configuration.CRSConfiguration;
046    import org.deegree.crs.configuration.CRSProvider;
047    import org.deegree.crs.coordinatesystems.GeographicCRS;
048    import org.deegree.crs.coordinatesystems.ProjectedCRS;
049    import org.deegree.crs.exceptions.CRSConfigurationException;
050    import org.deegree.crs.projections.cylindric.TransverseMercator;
051    import org.deegree.crs.transformations.Transformation;
052    import org.deegree.crs.transformations.helmert.Helmert;
053    import org.deegree.framework.log.ILogger;
054    import org.deegree.framework.log.LoggerFactory;
055    
056    /**
057     *
058     * The <code>CRSFactory</code> class wraps the access to the CRSProvider in the org.deegree.crs package by supplying a
059     * static create method, thus encapsulating the access to the CoordinateSystems.
060     *
061     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
062     *
063     * @author last edited by: $Author: mschneider $
064     *
065     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
066     *
067     */
068    public class CRSFactory {
069    
070        private static ILogger LOG = LoggerFactory.getLogger( CRSFactory.class );
071    
072        private synchronized static CRSProvider getProvider( String providerName ) {
073            CRSConfiguration crsConfig = CRSConfiguration.getCRSConfiguration( providerName );
074            return crsConfig.getProvider();
075        }
076    
077        /**
078         * Creates a CRS from the given name using the given provider, if no CRS was found an UnkownCRSException will be
079         * thrown.
080         *
081         * @param providerName
082         *            to be used for the creation of the crs.
083         * @param name
084         *            of the crs, e.g. EPSG:31466
085         * @return a CoordinateSystem corresponding to the given name
086         * @throws UnknownCRSException
087         *             if the crs-name is not known
088         */
089        public synchronized static CoordinateSystem create( String providerName, String name )
090                                throws UnknownCRSException {
091            CRSProvider crsProvider = getProvider( providerName );
092            org.deegree.crs.coordinatesystems.CoordinateSystem realCRS = null;
093            try {
094                realCRS = crsProvider.getCRSByID( name );
095            } catch ( CRSConfigurationException e ) {
096                LOG.logError( e.getMessage(), e );
097            }
098            if ( realCRS == null ) {
099                throw new UnknownCRSException( name );
100            }
101            LOG.logDebug( "Successfully created the crs with id: " + name );
102            return new CoordinateSystem( realCRS, name );
103        }
104    
105        /**
106         * Get a {@link Transformation} with given id, or <code>null</code> if it does not exist.
107         *
108         * @param providerName
109         *            to use.
110         * @param id
111         *            of the Transformation.
112         * @return the identified transformation or <code>null<code> if no such transformation is found.
113         */
114        public synchronized static Transformation getTransformation( String providerName, String id ) {
115            CRSProvider crsProvider = getProvider( providerName );
116            Identifiable t = crsProvider.getIdentifiable( id );
117            if ( t instanceof Transformation ) {
118                return (Transformation) t;
119            }
120            LOG.logDebug( "The given id: " + id + " is not of type transformation return null." );
121            return null;
122        }
123    
124        /**
125         * Retrieve a {@link Transformation} (chain) which transforms coordinates from the given source into the given
126         * target crs. If no such {@link Transformation} could be found or the implementation does not support inverse
127         * lookup of transformations <code>null<code> will be returned.
128         * @param providerName
129         *            to use.
130         * @param sourceCRS start of the transformation (chain)
131         * @param targetCRS end point of the transformation (chain).
132         * @return the given {@link Transformation} or <code>null<code> if no such transformation was found.
133         */
134        public synchronized static Transformation getTransformation( String providerName, CoordinateSystem sourceCRS,
135                                                                     CoordinateSystem targetCRS ) {
136            CRSProvider crsProvider = getProvider( providerName );
137            return crsProvider.getTransformation( sourceCRS.getCRS(), targetCRS.getCRS() );
138        }
139    
140        /**
141         * Creates a CRS from the given name, if no CRS was found an UnkownCRSException will be thrown.
142         *
143         * @param name
144         *            of the crs, e.g. EPSG:4326
145         * @return a CoordinateSystem corresponding to the given name, using the configured provider.
146         * @throws UnknownCRSException
147         *             if the crs-name is not known
148         */
149        public synchronized static CoordinateSystem create( String name )
150                                throws UnknownCRSException {
151            return create( null, name );
152        }
153    
154        /**
155         * Wrapper for the private constructor of the org.deegree.model.crs.CoordinateSystem class.
156         *
157         * @param realCRS
158         *            to wrap
159         *
160         * @return a CoordinateSystem corresponding to the given crs.
161         */
162        public static CoordinateSystem create( org.deegree.crs.coordinatesystems.CoordinateSystem realCRS ) {
163            return new CoordinateSystem( realCRS, realCRS.getIdentifier() );
164        }
165    
166        /**
167         * Wrapper for the private constructor to create a dummy projected crs with no projection parameters set, the
168         * standard wgs84 datum and the given optional name as the identifier. X-Y axis are in metres.
169         *
170         * @param name
171         *            optional identifier, if missing, the word 'dummy' will be used.
172         *
173         * @return a dummy CoordinateSystem having filled out all the essential values.
174         */
175        public static CoordinateSystem createDummyCRS( String name ) {
176            if ( name == null || "".equals( name.trim() ) ) {
177                name = "dummy";
178            }
179            /**
180             * Standard axis of a geographic crs
181             */
182            final Axis[] axis_degree = new Axis[] { new Axis( Unit.DEGREE, "lon", Axis.AO_EAST ),
183                                                   new Axis( Unit.DEGREE, "lat", Axis.AO_NORTH ) };
184            final Axis[] axis_projection = new Axis[] { new Axis( "x", Axis.AO_EAST ), new Axis( "y", Axis.AO_NORTH ) };
185    
186            final Helmert wgs_info = new Helmert( GeographicCRS.WGS84, GeographicCRS.WGS84, name + "_wgs" );
187            final GeodeticDatum datum = new GeodeticDatum( Ellipsoid.WGS84, wgs_info, new String[] { name + "_datum" } );
188            final GeographicCRS geographicCRS = new GeographicCRS( datum, axis_degree, new String[] { name
189                                                                                                      + "geographic_crs" } );
190            final TransverseMercator projection = new TransverseMercator( true, geographicCRS, 0, 0, new Point2d( 0, 0 ),
191                                                                          Unit.METRE, 1 );
192    
193            return new CoordinateSystem(
194                                         new ProjectedCRS( projection, axis_projection, new String[] { name
195                                                                                                       + "projected_crs" } ),
196                                         name );
197    
198        }
199    }