001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/portlet/modules/map/actions/portlets/CRSChooserPortletPerform.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 037 package org.deegree.portal.portlet.modules.map.actions.portlets; 038 039 import java.awt.Rectangle; 040 import java.util.HashMap; 041 import java.util.Map; 042 043 import javax.servlet.ServletContext; 044 import javax.servlet.http.HttpServletRequest; 045 import javax.servlet.http.HttpSession; 046 047 import org.apache.jetspeed.portal.Portlet; 048 import org.deegree.framework.log.ILogger; 049 import org.deegree.framework.log.LoggerFactory; 050 import org.deegree.model.crs.CRSFactory; 051 import org.deegree.model.crs.CoordinateSystem; 052 import org.deegree.model.crs.GeoTransformer; 053 import org.deegree.model.spatialschema.Envelope; 054 import org.deegree.model.spatialschema.GeometryFactory; 055 import org.deegree.model.spatialschema.Point; 056 import org.deegree.portal.PortalException; 057 import org.deegree.portal.context.ViewContext; 058 import org.deegree.portal.portlet.modules.actions.AbstractPortletPerform; 059 import org.deegree.portal.portlet.modules.actions.IGeoPortalPortletPerform; 060 061 /** 062 * This Perform class takes care of changing the WMC's bounding box based on a scale paramter. The parameter is passed 063 * in the request. The paramter name is defined by the static member NEW_SCALE_VALUE 064 * 065 * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a> 066 * @author last edited by: $Author: mschneider $ 067 * 068 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 069 */ 070 071 public class CRSChooserPortletPerform extends IGeoPortalPortletPerform { 072 073 private static final ILogger LOG = LoggerFactory.getLogger( CRSChooserPortletPerform.class ); 074 075 public static final String REQUESTED_CRS = "REQUESTED_CRS"; 076 077 public static final String AVAILABLE_CRS = "AVAILABLE_CRS"; 078 079 public static final String AVAILABLE_CRS_NAMES = "AVAILABLE_CRS_NAMES"; 080 081 private static Map<String, Envelope> homeBBox; 082 static { 083 if ( homeBBox == null ) { 084 homeBBox = new HashMap<String, Envelope>(); 085 } 086 } 087 088 /** 089 * private constructor 090 * 091 * @param request 092 * @param portlet 093 * @param sc 094 */ 095 CRSChooserPortletPerform( HttpServletRequest request, Portlet portlet, ServletContext sc ) { 096 super( request, portlet, sc ); 097 } 098 099 /** 100 * TODO reads the init parameters of the portlet and build the scale list 101 * 102 * @throws PortalException 103 * 104 */ 105 void readInitParameter() 106 throws PortalException { 107 108 HttpSession ses = request.getSession(); 109 if ( ses.getAttribute( AVAILABLE_CRS ) == null ) { 110 String list = getInitParam( AVAILABLE_CRS ); 111 if ( list == null ) { 112 list = "EPSG:31467|GK 3;EPSG:4326|WGS 84"; 113 } 114 String[] tmp = list.split( ";" ); 115 String[] crs = new String[tmp.length]; 116 Map<String, String> crsToNames = new HashMap<String, String>( crs.length ); 117 118 for ( int i = 0; i < tmp.length; i++ ) { 119 String[] tmpEntry = tmp[i].split( "\\|" ); 120 if ( tmpEntry.length != 2 ) { 121 throw new PortalException( "Parameter is not properly define. An entry must contain a CRS and a " 122 + "name, and they must be separeted by a '|'" ); 123 } 124 crs[i] = tmpEntry[0]; 125 crsToNames.put( crs[i], tmpEntry[1] ); 126 } 127 128 ses.setAttribute( AVAILABLE_CRS, crs ); 129 ses.setAttribute( AVAILABLE_CRS_NAMES, crsToNames ); 130 } 131 132 } 133 134 /** 135 * Changes the CRS of the current ViewContext. The CRS value is in the parameter under the 'REQUESTED_CRS' key. 136 * 137 * @throws PortalException 138 * 139 */ 140 void doCRSChange() 141 throws PortalException { 142 143 String newCRS = parameter.get( REQUESTED_CRS ); 144 145 if ( newCRS == null ) { 146 // throw new PortalException( "No crs available in the request. Missing " + 147 // REQUESTED_CRS + " parameter" ); 148 return; 149 } 150 151 ViewContext vc = getCurrentViewContext( getInitParam( INIT_MAPPORTLETID ) ); 152 if ( vc == null ) { 153 throw new PortalException( "no valid view context available through users session" ); 154 } 155 156 // read points/bbox from ViewCOntext 157 Point p0 = vc.getGeneral().getBoundingBox()[0]; 158 Point p1 = vc.getGeneral().getBoundingBox()[1]; 159 160 CoordinateSystem oldCs = p0.getCoordinateSystem(); 161 162 if ( oldCs.getPrefixedName().equals( newCRS ) ) { 163 return; 164 } 165 166 167 // transform home bbox to new CRS 168 Envelope homeEnv = (Envelope) request.getSession().getAttribute( AbstractPortletPerform.SESSION_HOME ); 169 if ( homeBBox.get( oldCs.getPrefixedName() ) == null ) { 170 homeBBox.put( oldCs.getPrefixedName(), homeEnv ); 171 } 172 try { 173 GeoTransformer trans = new GeoTransformer( newCRS ); 174 175 if ( homeBBox.get( newCRS ) == null ) { 176 homeEnv = trans.transform( homeEnv, oldCs ); 177 homeEnv = ensureAspectRatio( homeEnv, vc ); 178 homeBBox.put( newCRS, homeEnv ); 179 } else { 180 homeEnv = homeBBox.get( newCRS ); 181 } 182 } catch ( Exception e ) { 183 LOG.logError( e.getMessage(), e ); 184 throw new PortalException( e.getMessage(), e ); 185 } 186 request.getSession().setAttribute( AbstractPortletPerform.SESSION_HOME, homeEnv ); 187 188 try { 189 CoordinateSystem newCs = CRSFactory.create( newCRS ); 190 191 Envelope env = GeometryFactory.createEnvelope( p0.getX(), p0.getY(), p1.getX(), p1.getY(), oldCs ); 192 193 GeoTransformer gt = new GeoTransformer( newCs ); 194 env = gt.transform( env, oldCs ); 195 env = ensureAspectRatio( env, vc ); 196 197 // set new bbox 198 p0 = GeometryFactory.createPoint( env.getMin().getX(), env.getMin().getY(), newCs ); 199 p1 = GeometryFactory.createPoint( env.getMax().getX(), env.getMax().getY(), newCs ); 200 201 vc.getGeneral().setBoundingBox( new Point[] { p0, p1 } ); 202 203 setCurrentMapContext( vc, getInitParam( INIT_MAPPORTLETID ) ); 204 205 } catch ( Exception e ) { 206 LOG.logError( e.getMessage(), e ); 207 throw new PortalException( e.getMessage(), e ); 208 } 209 } 210 211 private Envelope ensureAspectRatio( Envelope env, ViewContext vc ) { 212 Rectangle rect = vc.getGeneral().getWindow(); 213 double ar = ( (double) rect.width ) / ( (double) rect.height ); 214 double envAr = env.getWidth() / env.getHeight(); 215 if ( ar < envAr ) { 216 double tmp = env.getWidth() / ar; 217 double miny = env.getCentroid().getY() - tmp / 2d; 218 double maxy = env.getCentroid().getY() + tmp / 2d; 219 env = GeometryFactory.createEnvelope( env.getMin().getX(), miny, env.getMax().getX(), maxy, 220 env.getCoordinateSystem() ); 221 } else if ( ar > envAr ) { 222 double tmp = env.getHeight() * ar; 223 double minx = env.getCentroid().getX() - tmp / 2d; 224 double maxx = env.getCentroid().getX() + tmp / 2d; 225 env = GeometryFactory.createEnvelope( minx, env.getMin().getY(), maxx, env.getMax().getY(), 226 env.getCoordinateSystem() ); 227 } 228 return env; 229 } 230 231 }