001    //$HeadURL: $
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.ogcwebservices.wcts.data;
038    
039    import java.util.ArrayList;
040    import java.util.List;
041    
042    import org.deegree.crs.transformations.Transformation;
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    import org.deegree.i18n.Messages;
046    import org.deegree.model.crs.CRSFactory;
047    import org.deegree.model.crs.CRSTransformationException;
048    import org.deegree.model.crs.CoordinateSystem;
049    import org.deegree.model.crs.GeoTransformer;
050    import org.deegree.model.spatialschema.Geometry;
051    import org.deegree.model.spatialschema.GeometryException;
052    import org.deegree.model.spatialschema.WKTAdapter;
053    import org.deegree.ogcbase.ExceptionCode;
054    import org.deegree.ogcwebservices.OGCWebServiceException;
055    
056    /**
057     * <code>GeometryData</code> encapsulates a list of geometries which can be transformed using the
058     * {@link #doTransform(Transformation, boolean)} or {@link #doTransform(CoordinateSystem, CoordinateSystem, boolean)}
059     * methods.
060     *
061     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
062     *
063     * @author last edited by: $Author:$
064     *
065     * @version $Revision:$, $Date:$
066     *
067     */
068    public class GeometryData extends TransformableData<Geometry> {
069        private List<Geometry> sourceGeometries;
070    
071        private final List<Geometry> transformedGeometries;
072    
073        private static ILogger LOG = LoggerFactory.getLogger( GeometryData.class );
074    
075        /**
076         * Creates a data instance which handles geometries.
077         *
078         * @param transformableData
079         *            to transform
080         * @throws IllegalArgumentException
081         *             if either one of the crs's are <code>null</code>.
082         */
083        public GeometryData( List<Geometry> transformableData ) throws IllegalArgumentException {
084    
085            if ( transformableData == null ) {
086                transformableData = new ArrayList<Geometry>();
087            }
088            this.sourceGeometries = transformableData;
089            transformedGeometries = new ArrayList<Geometry>( this.sourceGeometries.size() );
090        }
091    
092        /*
093         * (non-Javadoc)
094         *
095         * @see org.deegree.ogcwebservices.wcts.operation.TransformableData#doTransform(boolean)
096         */
097        @Override
098        public void doTransform( CoordinateSystem sourceCRS, CoordinateSystem targetCRS, boolean enableLogging )
099                                throws OGCWebServiceException {
100            LOG.logDebug( "Trying to transform the geometries with default transformation." );
101            GeoTransformer transformer = getGeotransformer( targetCRS );
102            doTransform( sourceCRS, transformer, enableLogging );
103        }
104    
105        @Override
106        public void doTransform( Transformation transformation, boolean enableLogging )
107                                throws OGCWebServiceException {
108            LOG.logDebug( "Trying to transform the geometries with a transformation." );
109            GeoTransformer transformer = getGeotransformer( transformation );
110            doTransform( CRSFactory.create( transformation.getSourceCRS() ), transformer, enableLogging );
111        }
112    
113        private void doTransform( CoordinateSystem sourceCRS, GeoTransformer transformer, @SuppressWarnings("unused")
114        boolean enableLogging )
115                                throws OGCWebServiceException {
116            for ( Geometry geom : sourceGeometries ) {
117                try {
118                    if ( !sourceCRS.equals( geom.getCoordinateSystem() ) ) {
119                        throw new OGCWebServiceException(
120                                                          Messages.getMessage( "WCTS_MISMATCHING_CRS_DEFINITIONS",
121                                                                               sourceCRS.getIdentifier(),
122                                                                               geom.getCoordinateSystem().getIdentifier() ),
123                                                          ExceptionCode.INVALIDPARAMETERVALUE );
124                    }
125                    LOG.logDebug( "Transforming geometry: " + geom );
126                    transformedGeometries.add( transformer.transform( geom ) );
127                } catch ( IllegalArgumentException e ) {
128                    LOG.logError( e.getMessage(), e );
129                } catch ( CRSTransformationException e ) {
130                    LOG.logError( e.getMessage(), e );
131                }
132            }
133        }
134    
135        /*
136         * (non-Javadoc)
137         *
138         * @see org.deegree.ogcwebservices.wcts.operation.TransformableData#getResult()
139         */
140        @Override
141        public List<Geometry> getTransformedData() {
142            return transformedGeometries;
143        }
144    
145        /**
146         * Try to create a Geometry from a given String, put the result into a GeometryData object.
147         *
148         * @param sourceCRS
149         *            in which the data is referenced
150         * @param wkt
151         *            to create the geometry from.
152         * @return a geometry data or <code>null</code> if the wkt could not be parsed.
153         * @throws OGCWebServiceException
154         *             if run into an exception (thrown by the {@link WKTAdapter} any other then a GeometryException.
155         */
156        public static GeometryData parseGeometryData( CoordinateSystem sourceCRS, String wkt )
157                                throws OGCWebServiceException {
158            if ( wkt == null ) {
159                return null;
160            }
161            GeometryData result = null;
162            try {
163                Geometry parsedGeom = WKTAdapter.wrap( wkt, sourceCRS );
164                if ( parsedGeom != null ) {
165                    LOG.logDebug( "The geomety is of type: ", parsedGeom );
166                    List<Geometry> pg = new ArrayList<Geometry>();
167                    pg.add( parsedGeom );
168                    result = new GeometryData( pg );
169                }
170            } catch ( GeometryException e ) {
171                LOG.logDebug( "No parsable geometry found:  ", e.getLocalizedMessage() );
172            } catch ( Exception e ) {
173                LOG.logError( e );
174                throw new OGCWebServiceException( e.getLocalizedMessage(), ExceptionCode.INVALIDPARAMETERVALUE );
175            }
176            return result;
177        }
178    
179    }