001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/context/DefaultMapModelAccess.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.context;
037    
038    import java.awt.Rectangle;
039    import java.net.URL;
040    
041    import org.deegree.datatypes.QualifiedName;
042    import org.deegree.graphics.sld.StyledLayerDescriptor;
043    import org.deegree.graphics.transformation.GeoTransform;
044    import org.deegree.graphics.transformation.WorldToScreenTransform;
045    import org.deegree.model.crs.CoordinateSystem;
046    import org.deegree.model.spatialschema.Envelope;
047    import org.deegree.model.spatialschema.GeometryFactory;
048    import org.deegree.model.spatialschema.Point;
049    import org.deegree.portal.PortalException;
050    
051    /**
052     *
053     *
054     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
055     * @author last edited by: $Author: mschneider $
056     *
057     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
058     */
059    public class DefaultMapModelAccess implements MapModelAccess {
060    
061        private ViewContext vc = null;
062    
063        /**
064         * @param vc
065         */
066        public DefaultMapModelAccess( ViewContext vc ) {
067            this.vc = vc;
068        }
069    
070        public ViewContext addCRSToLayer( String name, URL owsAddress, String type, String crsName )
071                                throws PortalException {
072            // TODO Auto-generated method stub
073            return null;
074        }
075    
076        public ViewContext addFormatToLayer( String name, URL owsAddress, String type, String mimeType )
077                                throws PortalException {
078            // TODO Auto-generated method stub
079            return null;
080        }
081    
082        public ViewContext addLayer( QualifiedName name, URL owsAddress, String type )
083                                throws PortalException {
084            // TODO Auto-generated method stub
085            return null;
086        }
087    
088        public ViewContext addLayer( int index, QualifiedName name, URL owsAddress, String type )
089                                throws PortalException {
090            // TODO Auto-generated method stub
091            return null;
092        }
093    
094        public ViewContext addLayer( int index, Layer layer )
095                                throws PortalException {
096            // TODO Auto-generated method stub
097            return null;
098        }
099    
100        public ViewContext addLayer( Layer layer )
101                                throws PortalException {
102            // TODO Auto-generated method stub
103            return null;
104        }
105    
106        public ViewContext addLayers( QualifiedName[] names, URL[] owsAddresses, String type )
107                                throws PortalException {
108            // TODO Auto-generated method stub
109            return null;
110        }
111    
112        public ViewContext addLayers( Layer[] layers )
113                                throws PortalException {
114            // TODO Auto-generated method stub
115            return null;
116        }
117    
118        public ViewContext addStyleToLayer( QualifiedName name, URL owsAddress, String type, String styleName )
119                                throws PortalException {
120            // TODO Auto-generated method stub
121            return null;
122        }
123    
124        public ViewContext assignSLDToLayer( QualifiedName name, URL owsAddress, String type, StyledLayerDescriptor sld )
125                                throws PortalException {
126            // TODO Auto-generated method stub
127            return null;
128        }
129    
130        public ViewContext destroyGroup( String groupName ) {
131            // TODO Auto-generated method stub
132            return null;
133        }
134    
135        public ViewContext groupLayers( int first, int last, String groupName, String parentGroupName )
136                                throws PortalException {
137            // TODO Auto-generated method stub
138            return null;
139        }
140    
141        public ViewContext groupLayers( String[] layers, URL[] owsAddresses, String[] types, String groupName,
142                                        String parentGroupName )
143                                throws PortalException {
144            // TODO Auto-generated method stub
145            return null;
146        }
147    
148        public ViewContext pan( double directionDegree, double factor )
149                                throws PortalException {
150            // TODO Auto-generated method stub
151            return null;
152        }
153    
154        /**
155         * moves the boundingbox of the map to a well known direction. Directions that are known:
156         *
157         * @see #PAN_NORTH
158         * @see #PAN_NORTHEAST
159         * @see #PAN_NORTHWEST
160         * @see #PAN_SOUTH
161         * @see #PAN_SOUTHEAST
162         * @see #PAN_SOUTHWEST
163         * @see #PAN_WEST
164         * @see #PAN_EAST
165         * @param direction
166         *            direction the map view shall be moved to
167         * @param factor
168         *            factor measured in % the map view shall be moved
169         * @return modified ViewContext
170         * @throws PortalException
171         *             will be thrown if passed direction is not valid or factor is <= 0
172         * @throws ContextException
173         */
174        public ViewContext pan( int direction, double factor )
175                                throws PortalException, ContextException {
176            if ( direction < 0 || direction > 7 ) {
177                throw new PortalException( "invalid pan direction: " + direction );
178            }
179            if ( factor <= 0 ) {
180                throw new PortalException( "pan factor must be > 0 " );
181            }
182            factor = factor / 100.0;
183            Point[] bbox = vc.getGeneral().getBoundingBox();
184            double dx = ( bbox[1].getX() - bbox[0].getX() ) * factor;
185            double dy = ( bbox[1].getY() - bbox[0].getY() ) * factor;
186            double minx = bbox[0].getX();
187            double miny = bbox[0].getY();
188            double maxx = bbox[1].getX();
189            double maxy = bbox[1].getY();
190    
191            if ( direction == MapModelAccess.PAN_WEST || direction == MapModelAccess.PAN_NORTHWEST
192                 || direction == MapModelAccess.PAN_SOUTHWEST ) {
193                minx = minx - dx;
194                maxx = maxx - dx;
195            } else if ( direction == MapModelAccess.PAN_EAST || direction == MapModelAccess.PAN_NORTHEAST
196                        || direction == MapModelAccess.PAN_SOUTHEAST ) {
197                minx = minx + dx;
198                maxx = maxx + dx;
199            }
200            if ( direction == MapModelAccess.PAN_SOUTH || direction == MapModelAccess.PAN_SOUTHEAST
201                 || direction == MapModelAccess.PAN_SOUTHWEST ) {
202                miny = miny - dy;
203                maxy = maxy - dy;
204            } else if ( direction == MapModelAccess.PAN_NORTH || direction == MapModelAccess.PAN_NORTHEAST
205                        || direction == MapModelAccess.PAN_NORTHWEST ) {
206                miny = miny + dy;
207                maxy = maxy + dy;
208            }
209            Envelope env = GeometryFactory.createEnvelope( minx, miny, maxx, maxy, null );
210            return setMapBoundingBox( env );
211        }
212    
213        /**
214         * centers he map described by a ViewContext to the passed point. The behavior of this method is the same as
215         *
216         * @see #zoom(java.awt.Point, double) with a passed factor == 0;
217         *
218         * @param point
219         *            point (pixel coordinates) of the current map that marks the new center of the map
220         * @return modified ViewContext
221         * @throws ContextException
222         */
223        public ViewContext recenterMap( java.awt.Point point )
224                                throws ContextException {
225            return zoom( point, 0 );
226        }
227    
228        /**
229         * centers he map described by a ViewContext to the passed point. The behavior of this method is the same as
230         *
231         * @see #zoom(Point, double) with a passed factor == 0;
232         *
233         * @param point
234         *            point (map coordinates) of the current map that marks the new center of the map
235         * @return modified ViewContext
236         * @throws ContextException
237         */
238        public ViewContext recenterMap( Point point )
239                                throws ContextException {
240            return zoom( point, 0 );
241        }
242    
243        public ViewContext removeCRSFromLayer( String name, URL owsAddress, String type, String crsName ) {
244            // TODO Auto-generated method stub
245            return null;
246        }
247    
248        public ViewContext removeFormatFromLayer( String name, URL owsAddress, String type, String mimeType )
249                                throws PortalException {
250            // TODO Auto-generated method stub
251            return null;
252        }
253    
254        public ViewContext removeLayer( QualifiedName name, URL owsAddress, String type ) {
255            // TODO Auto-generated method stub
256            return null;
257        }
258    
259        public ViewContext removeStyleFromLayer( QualifiedName name, URL owsAddress, String type, String styleName ) {
260            // TODO Auto-generated method stub
261            return null;
262        }
263    
264        public ViewContext setCurrentMapFormat( String mimeType, URL owsAddress, String type )
265                                throws PortalException {
266            // TODO Auto-generated method stub
267            return null;
268        }
269    
270        public ViewContext setLayerActiveStyle( QualifiedName name, URL owsAddress, String type, String styleName )
271                                throws PortalException {
272            // TODO Auto-generated method stub
273            return null;
274        }
275    
276        public ViewContext setLayers( QualifiedName[] names, URL[] owsAddresses, String type )
277                                throws PortalException {
278            // TODO Auto-generated method stub
279            return null;
280        }
281    
282        public ViewContext setLayers( Layer[] layers )
283                                throws PortalException {
284            // TODO Auto-generated method stub
285            return null;
286        }
287    
288        public ViewContext setLayersVisible( String name, URL owsAddress, String type, boolean visible )
289                                throws PortalException {
290            // TODO Auto-generated method stub
291            return null;
292        }
293    
294        /**
295         * sets the bounding box a map/VieContext
296         *
297         * @param boundingBox
298         *            new boundingbox
299         * @return modified ViewContext
300         * @throws ContextException
301         */
302        public ViewContext setMapBoundingBox( Envelope boundingBox )
303                                throws ContextException {
304    
305            CoordinateSystem crs = vc.getGeneral().getBoundingBox()[0].getCoordinateSystem();
306            Point[] points = new Point[2];
307            points[0] = GeometryFactory.createPoint( boundingBox.getMin(), crs );
308            points[1] = GeometryFactory.createPoint( boundingBox.getMax(), crs );
309            vc.getGeneral().setBoundingBox( points );
310    
311            return vc;
312        }
313    
314        public ViewContext setMapCRS( String crs )
315                                throws PortalException {
316            // TODO Auto-generated method stub
317            return null;
318        }
319    
320        /**
321         * sets the width and height of a map described by a ViewContext
322         *
323         * @param width
324         *            map width measured in pixel
325         * @param height
326         *            map height measured in pixel
327         * @return modified ViewContext
328         * @throws PortalException
329         *             will be thrown if width or height < 1 (even a width or height of 1 pixel is not really useful but
330         *             formal it is valid);
331         */
332        public ViewContext setMapSize( int width, int height )
333                                throws PortalException {
334            if ( width < 1 ) {
335                throw new PortalException( "width must be > 0" );
336            }
337            if ( height < 1 ) {
338                throw new PortalException( "height must be > 0" );
339            }
340            Rectangle window = new Rectangle( 0, 0, width, height );
341            vc.getGeneral().setWindow( window );
342            return vc;
343        }
344    
345        public ViewContext swapGroups( QualifiedName name, boolean up )
346                                throws PortalException {
347            // TODO Auto-generated method stub
348            return null;
349        }
350    
351        /**
352         * swaps two layers in their order. The layer identified by the passed name and OWS can be moved up or down for one
353         * position. If a layer can not be moved up or down because it is already the top or bottom most layer the unchanged
354         * <code>ViewContext</code> will be returned
355         * <p>
356         * Notice: if to layers to be swaped are served by differend OWS at least one new image-layer must be created by the
357         * client.
358         * </p>
359         *
360         * @param name
361         *            name of the layer to be moved up or down in the list
362         * @param owsAddress
363         *            address of the OWS that serves the layer
364         * @param type
365         *            OWS type (WCS, WMS or WFS)
366         * @param up
367         *            true if layer should be moved up otherwise it will be moved down.
368         * @return modified ViewContext
369         * @throws PortalException
370         *             will be thrown if the layer does not exists in a ViewContext.
371         */
372        public ViewContext swapLayers( QualifiedName name, URL owsAddress, String type, boolean up )
373                                throws PortalException {
374    
375            Layer layer = vc.getLayerList().getLayer( name.getPrefixedName(), owsAddress.toExternalForm() );
376            vc.getLayerList().move( layer, up );
377            return vc;
378        }
379    
380        /**
381         * zoom in or out of the map described by a ViewContext and recenters it to the passed point.
382         * <ul>
383         * <li>factor > 0: zoomout</li>
384         * <li>factor < 0: zoomin</li>
385         * <li>factor == 0: recenter</li>
386         * </ul>
387         *
388         * @param factor
389         *            zoom factor in % of the boundingbox size
390         * @param point
391         *            point (pixel coordinates) of the current map that marks the new center of the map
392         * @return modified ViewContext
393         * @throws ContextException
394         */
395        public ViewContext zoom( java.awt.Point point, double factor )
396                                throws ContextException {
397            Point[] points = vc.getGeneral().getBoundingBox();
398            Rectangle window = vc.getGeneral().getWindow();
399            GeoTransform gt = new WorldToScreenTransform( points[0].getX(), points[0].getY(), points[1].getX(),
400                                                          points[1].getY(), 0, 0, window.width, window.height );
401            double x = gt.getSourceX( point.x );
402            double y = gt.getSourceX( point.y );
403            Point pt = GeometryFactory.createPoint( x, y, null );
404            return zoom( pt, factor );
405        }
406    
407        /**
408         * zoom in or out of the map described by a ViewContext and recenters it to the passed point.
409         * <ul>
410         * <li>factor > 0: zoomout</li>
411         * <li>factor < 0: zoomin</li>
412         * <li>factor == 0: recenter</li>
413         * </ul>
414         *
415         * @param factor
416         *            zoom factor in % of the boundingbox size
417         * @param point
418         *            point (map coordinates) of the current map that marks the new center of the map
419         * @return modified ViewContext
420         * @throws ContextException
421         */
422        public ViewContext zoom( Point point, double factor )
423                                throws ContextException {
424            Point[] points = vc.getGeneral().getBoundingBox();
425            double w = points[1].getX() - points[0].getX();
426            double h = points[1].getY() - points[0].getY();
427            factor = 1d + ( factor / 100d );
428            w = w * factor;
429            h = h * factor;
430            double minx = point.getX() - w / 2d;
431            double miny = point.getY() - h / 2d;
432            double maxx = point.getX() + w / 2d;
433            double maxy = point.getY() + h / 2d;
434            Envelope env = GeometryFactory.createEnvelope( minx, miny, maxx, maxy, null );
435            return setMapBoundingBox( env );
436        }
437    
438    }