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