001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/standard/wms/control/LegendListener.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.portal.standard.wms.control; 037 038 import java.awt.Color; 039 import java.awt.Graphics; 040 import java.awt.Rectangle; 041 import java.awt.geom.Rectangle2D; 042 import java.awt.image.BufferedImage; 043 import java.io.FileOutputStream; 044 import java.io.IOException; 045 import java.net.MalformedURLException; 046 import java.net.URL; 047 import java.util.ArrayList; 048 import java.util.HashMap; 049 import java.util.StringTokenizer; 050 051 import javax.servlet.http.HttpServletRequest; 052 import javax.servlet.http.HttpSession; 053 054 import org.deegree.enterprise.control.FormEvent; 055 import org.deegree.enterprise.control.RPCMember; 056 import org.deegree.enterprise.control.RPCMethodCall; 057 import org.deegree.enterprise.control.RPCParameter; 058 import org.deegree.enterprise.control.RPCStruct; 059 import org.deegree.enterprise.control.RPCWebEvent; 060 import org.deegree.framework.log.ILogger; 061 import org.deegree.framework.log.LoggerFactory; 062 import org.deegree.framework.util.IDGenerator; 063 import org.deegree.framework.util.ImageUtils; 064 import org.deegree.ogcwebservices.InconsistentRequestException; 065 import org.deegree.ogcwebservices.wms.operation.GetLegendGraphic; 066 import org.deegree.portal.context.GeneralExtension; 067 import org.deegree.portal.context.IOSettings; 068 import org.deegree.portal.context.ViewContext; 069 070 /** 071 * will be called if the client forces a dynamic legend. 072 * 073 * 074 * @author <a href="mailto:lupp@lat-lon.de">Katharina Lupp</a> 075 * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 076 */ 077 public class LegendListener extends AbstractMapListener { 078 079 private static final ILogger LOG = LoggerFactory.getLogger( LegendListener.class ); 080 081 /** 082 * the method will be called if a zoomout action/event occurs. 083 */ 084 public void actionPerformed( FormEvent event ) { 085 086 super.actionPerformed( event ); 087 088 RPCWebEvent rpc = (RPCWebEvent) event; 089 RPCMethodCall mc = rpc.getRPCMethodCall(); 090 RPCParameter[] para = mc.getParameters(); 091 RPCStruct struct = (RPCStruct) para[0].getValue(); 092 HttpSession session = ( (HttpServletRequest) this.getRequest() ).getSession( true ); 093 ViewContext vc = (ViewContext) session.getAttribute( "DefaultMapContext" ); 094 095 HashMap[] model = createWMSRequestModel( struct ); 096 097 try { 098 GetLegendGraphic legendParam = getLegendRequestParameter(); 099 HashMap symbols = setLegend( legendParam, model ); 100 Rectangle rect = calcLegendSize( symbols ); 101 BufferedImage bi = new BufferedImage( rect.width + 30, rect.height + 50, BufferedImage.TYPE_INT_RGB ); 102 bi = drawSymbolsToBI( symbols, bi ); 103 saveImage( vc, bi ); 104 } catch ( Exception e ) { 105 LOG.logError( "Error occurred in PrintListener: ", e ); 106 } 107 108 } 109 110 private Rectangle calcLegendSize( HashMap map ) { 111 112 String[] layers = (String[]) map.get( "NAMES" ); 113 BufferedImage[] legs = (BufferedImage[]) map.get( "IMAGES" ); 114 115 int w = 0; 116 int h = 0; 117 for ( int i = 0; i < layers.length; i++ ) { 118 h += legs[i].getHeight() + 6; 119 Graphics g = legs[i].getGraphics(); 120 Rectangle2D rect = g.getFontMetrics().getStringBounds( layers[i], g ); 121 g.dispose(); 122 if ( rect.getWidth() > w ) { 123 w = (int) rect.getWidth(); 124 } 125 } 126 w += 50; 127 128 return new Rectangle( w, h ); 129 } 130 131 private BufferedImage drawSymbolsToBI( HashMap map, BufferedImage bi ) { 132 133 Graphics g = bi.getGraphics(); 134 g.setColor( Color.WHITE ); 135 g.fillRect( 1, 1, bi.getWidth() - 2, bi.getHeight() - 2 ); 136 137 String[] layers = (String[]) map.get( "NAMES" ); 138 BufferedImage[] legs = (BufferedImage[]) map.get( "IMAGES" ); 139 int h = 5; 140 for ( int i = layers.length - 1; i >= 0; i-- ) { 141 g.drawImage( legs[i], 20, h, null ); 142 g.setColor( Color.BLACK ); 143 if ( legs[i].getHeight() < 50 ) { 144 g.drawString( layers[i], 30 + legs[i].getWidth(), h + (int) ( legs[i].getHeight() / 1.2 ) ); 145 } 146 h += legs[i].getHeight() + 5; 147 } 148 g.dispose(); 149 return bi; 150 } 151 152 private HashMap[] createWMSRequestModel( RPCStruct struct ) { 153 154 RPCMember[] member = struct.getMembers(); 155 156 HashMap<String, String>[] getMR = new HashMap[member.length]; 157 for ( int i = 0; i < member.length; i++ ) { 158 String request = (String) member[i].getValue(); 159 getMR[i] = toMap( request ); 160 StringTokenizer st = new StringTokenizer( request, "?" ); 161 getMR[i].put( "URL", st.nextToken() ); 162 } 163 return getMR; 164 } 165 166 private void saveImage( ViewContext vc, BufferedImage bg ) { 167 168 GeneralExtension ge = vc.getGeneral().getExtension(); 169 IOSettings ios = ge.getIOSettings(); 170 String dir = ios.getPrintDirectory().getDirectoryName(); 171 String format = "jpeg"; 172 long l = IDGenerator.getInstance().generateUniqueID(); 173 String file = "legend" + l + '.' + format; 174 try { 175 FileOutputStream fos = new FileOutputStream( dir + "/" + file ); 176 177 ImageUtils.saveImage( bg, fos, format, 1 ); 178 179 fos.close(); 180 } catch ( Exception e ) { 181 LOG.logError( "Error occurred in saving legend image: ", e ); 182 } 183 int pos = dir.lastIndexOf( '/' ); 184 String access = "./" + dir.substring( pos + 1, dir.length() ) + "/" + file; 185 this.getRequest().setAttribute( "DYNLEGENDIMAGE", access ); 186 187 } 188 189 private GetLegendGraphic getLegendRequestParameter() 190 throws InconsistentRequestException { 191 192 HashMap<String, String> legend = toMap( "VERSION=1.1.1&REQUEST=GetLegendGraphic&FORMAT=image/jpeg&WIDTH=50&HEIGHT=50&" 193 + "EXCEPTIONS=application/vnd.ogc.se_inimage&LAYER=europe:major_rivers&STYLE=default&" 194 + "SLD=file:///styles.xml" ); 195 legend.put( "ID", "1" ); 196 return GetLegendGraphic.create( legend ); 197 198 } 199 200 /** 201 * creates legend 202 */ 203 private HashMap setLegend( GetLegendGraphic glr, HashMap[] model ) 204 throws MalformedURLException, IOException { 205 206 ArrayList<String> list1 = new ArrayList<String>(); 207 ArrayList<BufferedImage> list2 = new ArrayList<BufferedImage>(); 208 209 StringTokenizer st = null; 210 String format = glr.getFormat(); 211 if ( format.equals( "image/jpg" ) ) 212 format = "image/jpeg"; 213 String legendURL = ""; 214 int lgHeight = 0; 215 for ( int i = 0; i < model.length; i++ ) { 216 217 String style = (String) model[i].get( "STYLE" ); 218 if ( style != null ) { 219 st = new StringTokenizer( style, "," ); 220 style = st.nextToken(); 221 } else 222 style = "default"; 223 st = new StringTokenizer( (String) model[i].get( "LAYERS" ), "," ); 224 225 while ( st.hasMoreTokens() ) { 226 String layer = st.nextToken(); 227 legendURL = setLegendURL( layer, style, format, glr, model[0] ); 228 lgHeight = lgHeight + 30; 229 BufferedImage legendGraphic = ImageUtils.loadImage( new URL( legendURL ) ); 230 list1.add( layer ); 231 list2.add( legendGraphic ); 232 } 233 } 234 235 String[] layers = list1.toArray( new String[list1.size()] ); 236 BufferedImage[] legs = list2.toArray( new BufferedImage[list2.size()] ); 237 HashMap<String, Object> map = new HashMap<String, Object>(); 238 map.put( "NAMES", layers ); 239 map.put( "IMAGES", legs ); 240 return map; 241 } 242 243 private String setLegendURL( String layer, String style, String format, GetLegendGraphic glr, HashMap model ) { 244 245 StringBuffer sb = new StringBuffer( 500 ); 246 sb.append( model.get( "URL" ) ).append( '?' ); 247 sb.append( "&VERSION=" ).append( glr.getVersion() ); 248 sb.append( "&REQUEST=GetLegendGraphic" ); 249 sb.append( "&FORMAT=" ).append( format ); 250 sb.append( "&WIDTH=15" ); 251 sb.append( "&HEIGHT=15&EXCEPTIONS=application/vnd.ogc.se_inimage" ); 252 sb.append( "&LAYER=" ).append( layer ); 253 sb.append( "&STYLE=" ).append( style ); 254 255 return sb.toString(); 256 } 257 258 }