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