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