001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wms/control/ScaleBarSwitcherListener.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 javax.servlet.ServletRequest;
039
040 import org.deegree.enterprise.control.FormEvent;
041 import org.deegree.enterprise.control.RPCMethodCall;
042 import org.deegree.enterprise.control.RPCParameter;
043 import org.deegree.enterprise.control.RPCStruct;
044 import org.deegree.enterprise.control.RPCWebEvent;
045 import org.deegree.framework.util.MapUtils;
046 import org.deegree.model.crs.CRSFactory;
047 import org.deegree.model.crs.CoordinateSystem;
048 import org.deegree.model.crs.GeoTransformer;
049 import org.deegree.model.crs.UnknownCRSException;
050 import org.deegree.model.spatialschema.Envelope;
051 import org.deegree.model.spatialschema.GeometryFactory;
052 import org.deegree.portal.Constants;
053
054 /**
055 * The ScaleSwitcherListner handles switching the scalebar values from igeoportal
056 *
057 * @author <a href="mailto:ncho@lat-lon.de">Serge N'Cho</a>
058 * @author last edited by: $$Author: mschneider $$
059 *
060 * @version $$Revision: 18195 $$, $$Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $$
061 */
062
063 public class ScaleBarSwitcherListener extends AbstractMapListener {
064
065 /**
066 * Comment for "unit"
067 */
068 public static final String UNIT = "unit";
069
070 /**
071 * Comment for "taskFromListener"
072 */
073 public static final String TASK_FROM_LISTENER = "taskFromListener";
074
075 /**
076 * Comment for "scaleBarValue"
077 */
078 public static final String SCALE_BAR_VALUE = "scaleBarValue";
079
080 /**
081 * Comment for "newScaleBarValue"
082 */
083 public static final String NEW_SCALE_BAR_VALUE = "newScaleBarValue";
084
085 /**
086 * Comment for "BBOX"
087 */
088 public static final String BBOX = "BBOX";
089
090 /**
091 * Comment for "newBBox"
092 */
093 public static final String NEW_BBOX = "newBBox";
094
095 /**
096 * Constant for "crs"
097 */
098 private static final String CRS = "crs";
099
100 /**
101 * Constant for "taskFromJSObject"
102 */
103 private static final String JS_TAK = "taskFromJSObject";
104
105 /**
106 * Constant for "requestedBarValue"
107 */
108 private static final String REQUESTED_BAR_VALUE = "requestedBarValue";
109
110 /**
111 * Constant for "getNewBBOX"
112 */
113 private static final String GET_NEW_BBOX = "getNewBBOX";
114
115 /**
116 * Constant for "getActualScaleBarValue"
117 */
118 private static final String GET_ACTUAL_BAR_VAULE = "getActualScaleBarValue";
119
120 /*
121 * (non-Javadoc)
122 *
123 * @see org.deegree.enterprise .control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
124 */
125 @Override
126 public void actionPerformed( FormEvent event ) {
127
128 RPCWebEvent rpc = (RPCWebEvent) event;
129 RPCMethodCall mc = rpc.getRPCMethodCall();
130 RPCParameter param = mc.getParameters()[0];
131 RPCStruct struct = (RPCStruct) param.getValue();
132 double minx = (Double) struct.getMember( Constants.RPC_BBOXMINX ).getValue();
133 double miny = (Double) struct.getMember( Constants.RPC_BBOXMINY ).getValue();
134 double maxx = (Double) struct.getMember( Constants.RPC_BBOXMAXX ).getValue();
135 double maxy = (Double) struct.getMember( Constants.RPC_BBOXMAXY ).getValue();
136 String crsString = struct.getMember( CRS ).getValue().toString();
137 CoordinateSystem crs;
138 try {
139 crs = CRSFactory.create( crsString );
140 } catch ( UnknownCRSException e ) {
141 gotoErrorPage( e.getMessage() );
142 return;
143 }
144 Envelope inBBOX = GeometryFactory.createEnvelope( minx, miny, maxx, maxy, crs );
145 String unit = struct.getMember( UNIT ).getValue().toString();
146 String taskFromJSObject = struct.getMember( JS_TAK ).getValue().toString();
147 double actualBarValue = calculateScaleBarValue( inBBOX );
148
149 if ( !"m".equalsIgnoreCase( unit ) ) {
150 actualBarValue = convertToUnit( actualBarValue, unit );
151 }
152
153 ServletRequest request = this.getRequest();
154
155 if ( GET_ACTUAL_BAR_VAULE.equalsIgnoreCase( taskFromJSObject ) ) {
156 request.setAttribute( TASK_FROM_LISTENER, NEW_SCALE_BAR_VALUE );
157 request.setAttribute( SCALE_BAR_VALUE, Double.valueOf( actualBarValue ) );
158 } else if ( GET_NEW_BBOX.equals( taskFromJSObject ) ) {
159
160 Double requestedBarValue = (Double) struct.getMember( REQUESTED_BAR_VALUE ).getValue();
161
162 Envelope newBBox = MapUtils.scaleEnvelope( inBBOX, actualBarValue, requestedBarValue );
163
164 double[] bbox = { newBBox.getMin().getX(), newBBox.getMin().getY(), newBBox.getMax().getX(),
165 newBBox.getMax().getY() };
166
167 request.setAttribute( TASK_FROM_LISTENER, NEW_BBOX );
168 request.setAttribute( BBOX, bbox );
169 request.setAttribute( SCALE_BAR_VALUE, requestedBarValue );
170
171 }
172
173 request.setAttribute( UNIT, unit );
174 }
175
176 /**
177 * The methode <code>calculateScaleBarValue</code> calculates the scaleBarValue of the Map.
178 *
179 * @param bbox
180 * the actual Envelope of the map
181 * @param mapWidth
182 * @param mapHeight
183 * @return - The distance from <code>bbox</code>.minx to <code>.maxx in meters
184 */
185 private double calculateScaleBarValue( Envelope bbox ) {
186
187 double scaleBarValue = 0;
188
189 try {
190 // Convert BBox to EPSG:4326 befor calculating the scalBarValue
191 if ( !"EPSG:4326".equalsIgnoreCase( bbox.getCoordinateSystem().getIdentifier() ) ) {
192
193 GeoTransformer transformer = new GeoTransformer( "EPSG:4326" );
194 bbox = transformer.transform( bbox, bbox.getCoordinateSystem() );
195 }
196
197 scaleBarValue = MapUtils.calcDistance( bbox.getMin().getX(), bbox.getMin().getY(), bbox.getMax().getX(),
198 bbox.getMin().getY() );
199 } catch ( Exception e ) {
200 e.printStackTrace();
201 }
202
203 return scaleBarValue;
204 }
205
206 /**
207 * The methode converts the length -actualValue- to unit
208 *
209 * @param actualValue -
210 * The length to be converted
211 * @param unit -
212 * The unit in which the length is to converted to
213 * @return - The converted length to unit
214 */
215 private double convertToUnit( double actualValue, String unit ) {
216
217 if ( "km".equalsIgnoreCase( unit ) ) {
218 return actualValue * 0.001;
219 }
220 // FIXME to which unit should this be extended to
221
222 return actualValue;
223 }
224
225 }