    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/gazetteer/LoadBBOXListener.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.gazetteer;
    037
    038    import java.io.IOException;
    039    import java.util.Map;
    040
    041    import org.deegree.crs.coordinatesystems.ProjectedCRS;
    042    import org.deegree.datatypes.QualifiedName;
    043    import org.deegree.enterprise.control.ajax.ResponseHandler;
    044    import org.deegree.enterprise.control.ajax.WebEvent;
    045    import org.deegree.framework.log.ILogger;
    046    import org.deegree.framework.log.LoggerFactory;
    047    import org.deegree.framework.util.Pair;
    048    import org.deegree.framework.util.Parameter;
    049    import org.deegree.model.crs.CoordinateSystem;
    050    import org.deegree.model.crs.GeoTransformer;
    051    import org.deegree.model.spatialschema.Envelope;
    052    import org.deegree.model.spatialschema.Geometry;
    053    import org.deegree.model.spatialschema.Point;
    054    import org.deegree.portal.Constants;
    055    import org.deegree.portal.context.Module;
    056    import org.deegree.portal.context.ViewContext;
    057
    058    /**
    059     * TODO add class documentation here
    060     *
    061     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
    062     * @author last edited by: $Author: apoth $
    063     *
    064     * @version $Revision: 25263 $, $Date: 2010-07-12 16:49:55 +0200 (Mo, 12. Jul 2010) $
    065     */
    066    public class LoadBBOXListener extends AbstractGazetteerListener {
    067
    068        private static final ILogger LOG = LoggerFactory.getLogger( LoadBBOXListener.class );
    069
    070        /*
    071         * (non-Javadoc)
    072         *
    073         * @see
    074         * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
    075         * , org.deegree.enterprise.control.ajax.ResponseHandler)
    076         */
    077        @SuppressWarnings("unchecked")
    078        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
    079                                throws IOException {
    080            Map<String, Object> param = event.getParameter();
    081            int index = ( (Number) param.get( "level" ) ).intValue();
    082            int hierarchyIndex = Integer.parseInt( (String) param.get( "hierarchyIndex" ) );
    083            String geographicIdentifier = (String) param.get( "geographicIdentifier" );
    084
    085            ViewContext vc = (ViewContext) event.getSession().getAttribute( Constants.CURRENTMAPCONTEXT );
    086            Module[] modules = vc.getGeneral().getExtension().getFrontend().getModulesByName( "Gazetteer" );
    087            Parameter[] parameters = modules[0].getParameter().getParameters();
    088            // just one gazetteer module is allowed to be contained in an instance of iGeoPortal
    089            Hierarchy hierarchy;
    090            try {
    091                hierarchy = loadHierarchy( (String) parameters[hierarchyIndex].getValue() );
    092            } catch ( Exception e ) {
    093                LOG.logError( e );
    094                responseHandler.writeAndClose( "ERROR: can not load hierarchy list" );
    095                return;
    096            }
    097
    098            HierarchyNode node = hierarchy.getRoot();
    099            int k = 0;
    100            while ( node != null && k < index ) {
    101                node = node.getChildNode();
    102                k++;
    103            }
    104            QualifiedName ft = node.getFeatureType();
    105            LoadBBOXCommand cmd = new LoadBBOXCommand( hierarchy.getGazetteerAddress(), ft, node.getProperties(),
    106                                                       geographicIdentifier );
    107            Envelope env = null;
    108            Pair<Geometry, Geometry> geometries = null;
    109            try {
    110                // first contains highlight geometry; second geographic extent
    111                geometries = cmd.execute();
    112                if ( geometries.first instanceof Point ) {
    113                    // heuristic to ensure a useful bounding box. This may fails with compound CRS
    114                    if ( geometries.second.getCoordinateSystem().getCRS() instanceof ProjectedCRS ) {
    115                        env = geometries.second.getBuffer( 50 ).getEnvelope();
    116                    } else {
    117                        env = geometries.second.getBuffer( 0.001 ).getEnvelope();
    118                    }
    119                } else {
    120                    env = geometries.second.getEnvelope();
    121                }
    122            } catch ( Exception e ) {
    123                LOG.logError( e );
    124                responseHandler.writeAndClose( "ERROR: could not find/load envelope for geographicIdentifier: "
    125                                               + geographicIdentifier );
    126                return;
    127            }
    128
    129            CoordinateSystem crs = vc.getGeneral().getBoundingBox()[0].getCoordinateSystem();
    130            if ( !crs.equals( env.getCoordinateSystem() ) ) {
    131                GeoTransformer gt = new GeoTransformer( crs );
    132                try {
    133                    env = gt.transform( env, env.getCoordinateSystem() );
    134                    event.getSession().setAttribute( "TEMP_WMS_GEOMETRY", gt.transform( geometries.first ) );
    135                } catch ( Exception e ) {
    136                    LOG.logError( e );
    137                    responseHandler.writeAndClose( "ERROR: can not transform geographic extent into map CRS" );
    138                    return;
    139                }
    140            } else {
    141                event.getSession().setAttribute( "TEMP_WMS_GEOMETRY", geometries.first );
    142            }
    143            responseHandler.writeAndClose( env.getMin().getX() + "," + env.getMin().getY() + "," + env.getMax().getX()+ "," + env.getMax().getY() );
    144        }
    145
    146    }