001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/WMSInvoker.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstraße 19
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041     
042     ---------------------------------------------------------------------------*/
043    
044    package org.deegree.ogcwebservices.wpvs;
045    
046    import java.awt.Color;
047    import java.awt.Graphics2D;
048    import java.awt.Image;
049    import java.awt.image.BufferedImage;
050    import java.util.Map;
051    
052    import org.deegree.datatypes.values.Values;
053    import org.deegree.framework.log.ILogger;
054    import org.deegree.framework.log.LoggerFactory;
055    import org.deegree.framework.util.IDGenerator;
056    import org.deegree.framework.util.StringTools;
057    import org.deegree.ogcwebservices.OGCWebService;
058    import org.deegree.ogcwebservices.OGCWebServiceException;
059    import org.deegree.ogcwebservices.wms.operation.GetMap;
060    import org.deegree.ogcwebservices.wms.operation.GetMapResult;
061    import org.deegree.ogcwebservices.wpvs.configuration.AbstractDataSource;
062    import org.deegree.ogcwebservices.wpvs.configuration.LocalWMSDataSource;
063    import org.deegree.ogcwebservices.wpvs.utils.ImageUtils;
064    import org.deegree.ogcwebservices.wpvs.utils.ResolutionStripe;
065    
066    /**
067     * Invoker for a Web Map Service.
068     * 
069     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
070     * @author last edited by: $Author: apoth $
071     * 
072     * $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
073     */
074    public class WMSInvoker extends GetViewServiceInvoker {
075    
076        private static final ILogger LOG = LoggerFactory.getLogger( WMSInvoker.class );
077    
078        private int id;
079    
080        /**
081         * Creates a new instance of this class.
082         * 
083         * @param owner
084         *            the handler that owns this invoker
085         * @param id
086         *            which can be used to sort all the request/responses in the resolutionStripe.
087         */
088        public WMSInvoker( ResolutionStripe owner, int id ) {
089            super( owner );
090            this.id = id;
091        }
092    
093        @Override
094        public void invokeService( AbstractDataSource dataSource ) {
095            // int count = 0;
096            if ( !( dataSource != null && ( AbstractDataSource.LOCAL_WMS == dataSource.getServiceType() || AbstractDataSource.REMOTE_WMS == dataSource.getServiceType() ) ) ) {
097                LOG.logError( "The given AbstractDataSource is no LocalWMSDataSource instance. It is needed for a WMSInvoker" );
098                throw new RuntimeException( "DataSource should be a WMS-instance for a WMSInvoker" );
099            }
100            OGCWebService service = null;
101            try {
102                service = dataSource.getOGCWebService();
103            } catch ( OGCWebServiceException ogcwe ) {
104                LOG.logError( ogcwe.getMessage() );
105                throw new RuntimeException( ogcwe );
106            }
107    
108            Object response = null;
109            try {
110                GetMap getMapRequest = createGetMapRequest( (LocalWMSDataSource) dataSource );
111                if ( getMapRequest == null ) {
112                    return;
113                }
114                LOG.logDebug( "WMS request: " + getMapRequest );
115                /**
116                 * Invoke the wms service.
117                 */
118                response = service.doService( getMapRequest );
119            } catch ( OGCWebServiceException ogcwse ) {
120                if ( !Thread.currentThread().isInterrupted() ) {
121                    LOG.logError( StringTools.concat( 500, "Error when performing WMS GetMap: ", ogcwse.getMessage() ) );
122                }
123                return;
124            }
125            if ( response != null ) {
126                if ( response instanceof GetMapResult ) {
127                    BufferedImage responseImage = null;
128                    // the exception may be inside the response
129                    if ( ( (GetMapResult) response ).getException() != null ) {
130                        resolutionStripe.setTextureRetrievalException( dataSource.getName().getFormattedString() + id,
131                                                                       ( (GetMapResult) response ).getException() );
132                    } else {
133                        responseImage = (BufferedImage) ( (GetMapResult) response ).getMap();
134                        // LOG.logDebug( StringTools.concat( 100, "WMS RESULT: ", response ) );
135                        if ( responseImage != null ) {
136                            Color[] colors = ( (LocalWMSDataSource) dataSource ).getTransparentColors();
137                            if ( colors != null && colors.length > 0 ) {
138                                ImageUtils imgUtil = new ImageUtils( colors );
139                                Image img = imgUtil.filterImage( responseImage );
140                                Graphics2D g2d = (Graphics2D) responseImage.getGraphics();
141                                g2d.drawImage( img, 0, 0, null );
142                                g2d.dispose();
143                            }
144                            resolutionStripe.addTexture( ( dataSource.getName().getFormattedString() + id ), responseImage );
145                        }
146                    }
147                }
148            }
149        }
150    
151        /**
152         * Creates a new GetMap request for the given datasource.
153         * 
154         * @param ds
155         *            the WMs datasource
156         * @param box
157         *            the surface to be used as the bouding box
158         * @return a new GetMap request
159         */
160        private GetMap createGetMapRequest( LocalWMSDataSource ds ) {
161    
162            GetMap getMapRequest = ds.getPartialGetMapRequest();
163    
164            Values elevation = null;
165            Values time = null;
166            Map<String, Values> sampleDim = null;
167    
168            // int tileSize = owner.getMaxTileSize();
169            int tileWidth = resolutionStripe.getRequestWidthForBBox();
170            int tileHeight = resolutionStripe.getRequestHeightForBBox();
171            if ( tileWidth == -1 || tileHeight == -1 ) {
172                LOG.logError( StringTools.concat( 500,
173                                                  "Not creating WMS-GetMap because the height or the width of the image are -1: " ) );
174                return null;
175            }
176            // GeometryUtils.getImageSizeForSurface( (RankedSurface)box, owner.getMaxTileSize());
177    
178            IDGenerator idg = IDGenerator.getInstance();
179            getMapRequest = GetMap.create( getMapRequest.getVersion(), String.valueOf( idg.generateUniqueID() ),
180                                           getMapRequest.getLayers(), elevation, sampleDim, getMapRequest.getFormat(),
181                                           tileWidth, tileHeight, resolutionStripe.getCRSName().getFormattedString(),
182                                           resolutionStripe.getSurface().getEnvelope(), getMapRequest.getTransparency(),
183                                           getMapRequest.getBGColor(), getMapRequest.getExceptions(), time, null, null,
184                                           null );
185    
186            return getMapRequest;
187        }
188    }