001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/ogcwebservices/wpvs/WMSInvoker.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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: bezema $
071     * 
072     * $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 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 an 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                /**
112                 * Invoke the wms service.
113                 */
114                response = service.doService( getMapRequest );
115            } catch ( OGCWebServiceException ogcwse ) {
116                if ( !Thread.currentThread().isInterrupted() ) {
117                    LOG.logError( StringTools.concat( 500, "Error when performing WMS GetMap: ",
118                                                      ogcwse.getMessage() ) );
119                }
120                return;
121            }
122            if ( response != null ) {
123                if ( response instanceof GetMapResult ) {
124                    BufferedImage responseImage = null;
125                    // the exception may be inside the response
126                    if ( ( (GetMapResult) response ).getException() != null ) {
127                        resolutionStripe.setTextureRetrievalException(
128                                                                       dataSource.getName().getFormattedString()
129                                                                                               + id,
130                                                                       ( (GetMapResult) response ).getException() );
131                    } else {
132                        responseImage = (BufferedImage) ( (GetMapResult) response ).getMap();
133                        // LOG.logDebug( StringTools.concat( 100, "WMS RESULT: ", response ) );
134                        if ( responseImage != null ) {
135                            Color[] colors = ( (LocalWMSDataSource) dataSource ).getTransparentColors();
136                            if ( colors != null && colors.length > 0 ) {
137                                ImageUtils imgUtil = new ImageUtils( colors );
138                                Image img = imgUtil.filterImage( responseImage );
139                                Graphics2D g2d = (Graphics2D) responseImage.getGraphics();
140                                g2d.drawImage( img, 0, 0, null );
141                                g2d.dispose();
142                            }
143                            resolutionStripe.addTexture(
144                                                         ( dataSource.getName().getFormattedString() + id ),
145                                                         responseImage );
146                        }
147                    }
148                }
149            }
150        }
151    
152        /**
153         * Creates a new GetMap request for the given datasource.
154         * 
155         * @param ds
156         *            the WMs datasource
157         * @param box
158         *            the surface to be used as the bouding box
159         * @return a new GetMap request
160         */
161        private GetMap createGetMapRequest( LocalWMSDataSource ds ) {
162    
163            GetMap getMapRequest = ds.getPartialGetMapRequest();
164    
165            Values elevation = null;
166            Values time = null;
167            Map<String, Values> sampleDim = null;
168    
169            // int tileSize = owner.getMaxTileSize();
170            int tileWidth = resolutionStripe.getRequestWidthForBBox();
171            int tileHeight = resolutionStripe.getRequestHeightForBBox();
172            // GeometryUtils.getImageSizeForSurface( (RankedSurface)box, owner.getMaxTileSize());
173    
174            IDGenerator idg = IDGenerator.getInstance();
175            getMapRequest = GetMap.create( getMapRequest.getVersion(),
176                                           String.valueOf( idg.generateUniqueID() ),
177                                           getMapRequest.getLayers(), elevation, sampleDim,
178                                           getMapRequest.getFormat(), tileWidth, tileHeight,
179                                           resolutionStripe.getCRSName().getFormattedString(),
180                                           resolutionStripe.getSurface().getEnvelope(),
181                                           getMapRequest.getTransparency(), getMapRequest.getBGColor(),
182                                           getMapRequest.getExceptions(), time, null, null, null );
183    
184            return getMapRequest;
185        }
186    }