001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wms/control/RecenterToLayerListener.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.standard.wms.control;
038
039 import java.net.URL;
040
041 import javax.servlet.ServletRequest;
042
043 import org.deegree.enterprise.control.AbstractListener;
044 import org.deegree.enterprise.control.FormEvent;
045 import org.deegree.enterprise.control.RPCMethodCall;
046 import org.deegree.enterprise.control.RPCParameter;
047 import org.deegree.enterprise.control.RPCStruct;
048 import org.deegree.enterprise.control.RPCWebEvent;
049 import org.deegree.framework.util.MapUtils;
050 import org.deegree.model.crs.CRSFactory;
051 import org.deegree.model.crs.GeoTransformer;
052 import org.deegree.model.spatialschema.Envelope;
053 import org.deegree.ogcwebservices.wms.capabilities.ScaleHint;
054 import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
055 import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocument;
056 import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocumentFactory;
057 import org.deegree.portal.PortalException;
058
059 /**
060 * This class sets the bounding box of the portal to the bounding box of one selected layer as it is specified in the
061 * WMSCapabilities document of that layer.
062 *
063 * If the ScaleHint of the layer reduces the visible part of the layer's LatLonBoundingBox, then a new bounding box for
064 * this scale hint is calculated. Ohterwise the LatLonBoundingBox is used.
065 *
066 * The chosen bounding box is transformed to the portals crs and the portals aspect ratio of width and height.
067 *
068 * @author <a href="mailto:ncho@lat-lon.de">Serge N'Cho</a>
069 * @author <a href="mailto:mays@lat-lon.de">Judit Mays</a>
070 * @author last edited by: $Author: mschneider $
071 *
072 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
073 */
074 public class RecenterToLayerListener extends AbstractListener {
075
076 private static final double PIXEL_SIZE_IN_METERS = 0.00028;
077
078 /*
079 * (non-Javadoc)
080 *
081 * @see org.deegree.enterprise.control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
082 */
083 @Override
084 public void actionPerformed( FormEvent event ) {
085
086 RPCWebEvent rpc = (RPCWebEvent) event;
087 RPCMethodCall mc = rpc.getRPCMethodCall();
088 RPCParameter param = mc.getParameters()[0];
089 RPCStruct struct = (RPCStruct) param.getValue();
090
091 String capabilitiesRequest = struct.getMember( "capabilitiesRequest" ).getValue().toString();
092 String layerName = struct.getMember( "layerName" ).getValue().toString();
093
094 String crs = struct.getMember( "crs" ).getValue().toString();
095
096 int mapWidth = Integer.parseInt( struct.getMember( "mapWidth" ).getValue().toString() );
097 int mapHeight = Integer.parseInt( struct.getMember( "mapHeight" ).getValue().toString() );
098
099 try {
100 URL url = new URL( capabilitiesRequest );
101 WMSCapabilitiesDocument capsDoc = WMSCapabilitiesDocumentFactory.getWMSCapabilitiesDocument( url );
102
103 WMSCapabilities wmsCaps = (WMSCapabilities) capsDoc.parseCapabilities();
104 // Envelope in epsg4326
105 Envelope latLonBBox = wmsCaps.getLayer( layerName ).getLatLonBoundingBox();
106
107 if ( latLonBBox == null ) {
108 // this will never happen, because:
109 // capsDoc.parseCapabilities will check, that there is a latLonBBox for the
110 // document.
111 // otherwise an exception will be thrown there.
112 // getLayer( someName ).getLatLonBoundingBox() returns the bbox for the given layer,
113 // or, if not set, returns the bbox of the parentLayer.
114 throw new PortalException( "LatLonBoundingBox not found in Capabilities" );
115 }
116
117 Envelope outBBox = null;
118 final String epsg4326 = "EPSG:4326";
119 ScaleHint scaleHint = wmsCaps.getLayer( layerName ).getScaleHint();
120
121 // should use Double.POSITIVE_INFINITY instead of Double.MAX_VALUE
122 if ( scaleHint.getMax() == Double.MAX_VALUE && scaleHint.getMin() == 0 ) {
123 // scaleHint is not defined in capabilities
124
125 double latLonScale = MapUtils.calcScale( mapWidth, mapHeight, latLonBBox,
126 CRSFactory.create( epsg4326 ), PIXEL_SIZE_IN_METERS );
127 Envelope scaleHintBBox = MapUtils.scaleEnvelope( latLonBBox, latLonScale, scaleHint.getMax() );
128
129 // both bboxes are in crs EPSG:4326
130 if ( scaleHintBBox.getWidth() < latLonBBox.getWidth()
131 && scaleHintBBox.getHeight() < latLonBBox.getHeight() ) {
132
133 outBBox = scaleHintBBox;
134 } else {
135 outBBox = latLonBBox;
136 }
137 } else {
138 outBBox = latLonBBox;
139 }
140
141 // transform outBBox to the crs of the portal
142 outBBox = new GeoTransformer( crs ).transform( outBBox, epsg4326 );
143
144 // ensure aspect ratio of the portal
145 outBBox = MapUtils.ensureAspectRatio( outBBox, mapWidth, mapHeight );
146
147 double layerBBoxArray[] = { outBBox.getMin().getX(), outBBox.getMin().getY(), outBBox.getMax().getX(),
148 outBBox.getMax().getY() };
149
150 ServletRequest req = this.getRequest();
151 req.setAttribute( "BBOX", layerBBoxArray );
152
153 } catch ( Exception e ) {
154 e.printStackTrace();
155 }
156 }
157
158 }