001: // $HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/portal/standard/wms/control/HighlightingInfoListener.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, 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: 53115 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.io.InputStreamReader;
047: import java.net.URL;
048:
049: import javax.servlet.http.HttpServletRequest;
050: import javax.servlet.http.HttpSession;
051:
052: import org.deegree.enterprise.control.FormEvent;
053: import org.deegree.enterprise.control.RPCMember;
054: import org.deegree.enterprise.control.RPCMethodCall;
055: import org.deegree.enterprise.control.RPCParameter;
056: import org.deegree.enterprise.control.RPCStruct;
057: import org.deegree.enterprise.control.RPCWebEvent;
058: import org.deegree.framework.util.CharsetUtils;
059: import org.deegree.framework.util.NetWorker;
060: import org.deegree.framework.util.StringTools;
061: import org.deegree.model.feature.FeatureCollection;
062: import org.deegree.model.feature.GMLFeatureCollectionDocument;
063: import org.deegree.model.spatialschema.GeometryFactory;
064: import org.deegree.model.spatialschema.Position;
065: import org.deegree.portal.PortalException;
066: import org.deegree.portal.context.LayerExtension;
067: import org.deegree.portal.context.ViewContext;
068:
069: /**
070: * This class is for accessing informations about the highlighted polygons A new WFS GetFeature
071: * request will be created and performed
072: *
073: * @version $Revision: 9346 $
074: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
075: * @author last edited by: $Author: apoth $
076: *
077: * @version 1.0. $Revision: 9346 $, $Date: 2007-12-27 08:39:07 -0800 (Thu, 27 Dec 2007) $
078: *
079: * @since 1.1
080: */
081: public class HighlightingInfoListener extends AbstractMapListener {
082:
083: /**
084: *
085: *
086: * @param event
087: */
088: public void actionPerformed(FormEvent event) {
089:
090: // default actions
091: super .actionPerformed(event);
092: RPCWebEvent rpc = (RPCWebEvent) event;
093: RPCMethodCall mc = rpc.getRPCMethodCall();
094: RPCParameter[] para = mc.getParameters();
095: RPCStruct struct = (RPCStruct) para[0].getValue();
096:
097: FeatureCollection fc = null;
098: try {
099: // get boundingbox of the request
100: RPCMember member = struct.getMember("boundingBox");
101: String tmp = (String) member.getValue();
102: double[] box = StringTools.toArrayDouble(tmp, ",");
103: // get coordinates for filtering from the request
104: Position[] coords = getCoordinates(struct);
105: // get layers/featuretypes to query
106: String[] queryLayers = getQueryLayers(struct);
107: // create WFS GetFeature request
108: String request = createRequest(queryLayers, coords, box);
109: // get responsible WFS URL
110: URL url = getResponsibleWFS(queryLayers[0]);
111: // get FeatureCollection from WFS
112: fc = performGetFeature(request, url);
113: } catch (Exception ex) {
114: gotoErrorPage("Invalid WCSCapabilityOperations: "
115: + ex.toString());
116: ex.printStackTrace();
117:
118: return;
119: }
120:
121: this .getRequest().setAttribute("HIGHLIGHTINFO", fc);
122:
123: }
124:
125: /**
126: * gets the layer to be highlighted.
127: */
128: private String[] getQueryLayers(RPCStruct struct) {
129: RPCMember mem = struct.getMember("queryLayers");
130: String tmp = (String) mem.getValue();
131: String[] queryLayers = StringTools.toArray(tmp, ",", false);
132: for (int i = 0; i < queryLayers.length; i++) {
133: int index = queryLayers[i].indexOf("|");
134: queryLayers[i] = queryLayers[i].substring(0, index);
135: }
136: return queryLayers;
137: }
138:
139: /**
140: * returns the URL of the WFS that is responsible for accessing the data of the passed
141: * layer/featuretype
142: *
143: * @param queryLayer
144: * layer to determine the responsible WFS for data access
145: */
146: private URL getResponsibleWFS(String queryLayer)
147: throws PortalException {
148:
149: HttpSession session = ((HttpServletRequest) this .getRequest())
150: .getSession(true);
151: ViewContext vc = (ViewContext) session
152: .getAttribute("DefaultMapContext");
153: if (vc.getLayerList().getLayer(queryLayer, null) == null) {
154: throw new PortalException("'" + queryLayer
155: + "' is not known by the client!");
156: }
157: LayerExtension le = vc.getLayerList()
158: .getLayer(queryLayer, null).getExtension();
159: URL wfsurl = null;
160: if (le.getDataService() == null) {
161: throw new PortalException(
162: "no WFS registered in MapContext for requested layer. "
163: + "Please contact your responsible administrator.");
164: }
165: if (!le.getDataService().getServer().getService().equals(
166: "ogc:WFS")) {
167: throw new PortalException(
168: "The responsible services isn't a ogc:WFS; no "
169: + "detail informations are available! ");
170: }
171: wfsurl = le.getDataService().getServer().getOnlineResource();
172:
173: return wfsurl;
174: }
175:
176: /**
177: * calculates the coordinates of the click event.
178: */
179: private Position[] getCoordinates(RPCStruct struct) {
180:
181: String xs = (String) struct.getMember("x").getValue();
182: String ys = (String) struct.getMember("y").getValue();
183: double[] x = StringTools.toArrayDouble(xs, ",");
184: double[] y = StringTools.toArrayDouble(ys, ",");
185:
186: Position[] pos = new Position[x.length];
187:
188: for (int i = 0; i < pos.length; i++) {
189: pos[i] = GeometryFactory.createPosition(x[i], y[i]);
190: }
191:
192: return pos;
193: }
194:
195: /**
196: * creates a WFS GetFeature request from the passed layers (feature types) and the coordinates.
197: * The least are used to create the filter conditions.
198: *
199: * @param queryLayers
200: * names of the layers/featuretypes that will be targeted by the request
201: * @param coords
202: * coordinates to be used as filter conditions (intersect)
203: * @param box
204: * relevant bounding box
205: */
206: private String createRequest(String[] queryLayers,
207: Position[] coords, double[] box) {
208:
209: StringBuffer sb = new StringBuffer(5000);
210: sb.append("<?xml version='1.0' encoding='UTF-8'?>");
211: sb.append("<GetFeature xmlns='http://www.opengis.net/wfs' ");
212: sb.append("xmlns:ogc='http://www.opengis.net/ogc' ");
213: sb.append("xmlns:gml='http://www.opengis.net/gml' ");
214: sb.append("service='WFS' version='1.0.0' outputFormat='GML2'>");
215: sb.append("<Query typeName='" + queryLayers[0] + "'>");
216:
217: sb.append("<ogc:Filter><ogc:And>");
218: if (coords.length > 1)
219: sb.append("<ogc:Or>");
220:
221: for (int k = 0; k < coords.length; k++) {
222: sb
223: .append("<ogc:Intersects><ogc:PropertyName>GEOM</ogc:PropertyName>");
224: sb.append("<gml:Point><gml:coordinates>").append(
225: coords[k].getX()).append(',');
226: sb.append(coords[k].getY()).append("</gml:coordinates>");
227: sb.append("</gml:Point></ogc:Intersects>");
228: }
229:
230: if (coords.length > 1)
231: sb.append("</ogc:Or>");
232:
233: sb
234: .append("<ogc:BBOX><ogc:PropertyName>GEOM</ogc:PropertyName>");
235: sb.append("<gml:Box><gml:coordinates>").append(box[0]).append(
236: ',').append(box[1]);
237: sb.append(' ').append(box[2]).append(',').append(box[3]);
238: sb.append("</gml:coordinates>").append("</gml:Box></ogc:BBOX>");
239: sb.append("</ogc:And></ogc:Filter></Query></GetFeature>");
240:
241: return sb.toString();
242: }
243:
244: /**
245: * performs a GetFeature request against the responsible WFS
246: */
247: private FeatureCollection performGetFeature(String request,
248: URL wfsURL) throws PortalException {
249:
250: FeatureCollection fc = null;
251: try {
252: NetWorker nw = new NetWorker(CharsetUtils
253: .getSystemCharset(), wfsURL, request);
254: InputStreamReader isr = new InputStreamReader(nw
255: .getInputStream(), CharsetUtils.getSystemCharset());
256: GMLFeatureCollectionDocument doc = new GMLFeatureCollectionDocument();
257: doc.load(isr, wfsURL.toString());
258: fc = doc.parse();
259: } catch (Exception e) {
260: throw new PortalException(
261: "couldn't perform GetFeature request", e);
262: }
263:
264: return fc;
265: }
266:
267: }
|