001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/graphics/transformation/WorldToScreenTransform.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    
045    package org.deegree.graphics.transformation;
046    
047    import org.deegree.model.spatialschema.Envelope;
048    import org.deegree.model.spatialschema.GeometryFactory;
049    import org.deegree.model.spatialschema.Position;
050    
051    /**
052     * the class <code>WorldToScreenTransform</code> implements <code>GeoTransformInterface</code>
053     * and defines a transformation to a linear coordinat system with its orgin on top/left. this can be
054     * used for realising a screen mapping of geometries.
055     * 
056     * @author Andreas Poth poth@lat-lon.de
057     * @version 28.12.2000
058     */
059    
060    public class WorldToScreenTransform implements GeoTransform {
061    
062        private double qx = 0;
063        private double qy = 0;
064        private Envelope sourceRect = null;
065        private Envelope destRect = null;
066    
067        /**
068         * constructor
069         * 
070         * initialices the transfromation rectangles with unique values (origin 0/0; widht 1; height 1)
071         */
072        public WorldToScreenTransform() {
073            setSourceRect( 0, 0, 1, 1 );
074            setDestRect( 0, 0, 1, 1 );
075        }
076    
077        /**
078         * constructor
079         * 
080         * initialices the transformation rectangle using the submitted source- and destination
081         * rectangle.
082         * 
083         * @param sourceRect
084         *            is the boundary of the source geometry.
085         * @param destRect
086         *            is the boundary of the destination rectangle (for example a region on the screen)
087         */
088        public WorldToScreenTransform( Envelope sourceRect, Envelope destRect ) {
089            setSourceRect( sourceRect );
090            setDestRect( destRect );
091        }
092    
093        /**
094         * constrctor
095         * 
096         * @param sourceXMin
097         *            minimum x-coordinate (source system) of the map.
098         * @param sourceYMin
099         *            minimum y-coordinate (source system) of the map.
100         * @param sourceXMax
101         *            maximum x-coordinate (source system) of the map.
102         * @param sourceYMax
103         *            maximum y-coordinate (source system) of the map.
104         * @param destXMin
105         *            minimum x-coordinate (destination system) of the map.
106         * @param destYMin
107         *            minimum y-coordinate (destination system) of the map.
108         * @param destXMax
109         *            maximum x-coordinate (destination system) of the map.
110         * @param destYMax
111         *            maximum y-coordinate (destination system) of the map.
112         */
113        public WorldToScreenTransform( double sourceXMin, double sourceYMin, double sourceXMax,
114                                      double sourceYMax, double destXMin, double destYMin,
115                                      double destXMax, double destYMax ) {
116            setSourceRect( sourceXMin, sourceYMin, sourceXMax, sourceYMax );
117            setDestRect( destXMin, destYMin, destXMax, destYMax );
118        }
119    
120        /**
121         * sets the source rectangle
122         * 
123         * @param rect
124         *            is the boundary of the source geometry.
125         * 
126         */
127        public void setSourceRect( Envelope rect ) {
128    
129            sourceRect = rect;
130    
131            if ( ( sourceRect != null )
132                && ( destRect != null ) ) {
133                calculateQX();
134                calculateQY();
135            }
136        }
137    
138        /**
139         * sets the source rectangle
140         * 
141         * @param xMin
142         *            minimum x-coordinate (source system) of the map.
143         * @param yMin
144         *            minimum y-coordinate (source system) of the map.
145         * @param xMax
146         *            maximum x-coordinate (source system) of the map.
147         * @param yMax
148         *            maximum y-coordinate (source system) of the map.
149         */
150        public void setSourceRect( double xMin, double yMin, double xMax, double yMax ) {
151    
152            double dum = 0;
153    
154            if ( xMin > xMax ) {
155                dum = xMax;
156                xMax = xMin;
157                xMin = dum;
158            }
159    
160            if ( yMin > yMax ) {
161                dum = yMax;
162                yMax = yMin;
163                yMin = dum;
164            }
165    
166            sourceRect = GeometryFactory.createEnvelope( xMin, yMin, xMax, yMax, null );
167    
168            if ( destRect != null ) {
169                calculateQX();
170                calculateQY();
171            }
172    
173        }
174    
175        /**
176         * 
177         */
178        public Envelope getSourceRect() {
179            return sourceRect;
180        }
181    
182        /**
183         * sets the destination rectangle.
184         * 
185         * @param rect
186         *            is the boundary of the destination rectangle (for example a region on the screen)
187         * 
188         */
189        public void setDestRect( Envelope rect ) {
190            destRect = rect;
191            if ( ( sourceRect != null )
192                && ( destRect != null ) ) {
193                calculateQX();
194                calculateQY();
195            }
196        }
197    
198        /**
199         * sets the destination rectangle
200         * 
201         * @param xMin
202         *            minimum x-coordinate (destination system) of the map.
203         * @param yMin
204         *            minimum y-coordinate (destination system) of the map.
205         * @param xMax
206         *            maximum x-coordinate (destination system) of the map.
207         * @param yMax
208         *            maximum y-coordinate (destination system) of the map.
209         */
210        public void setDestRect( double xMin, double yMin, double xMax, double yMax ) {
211    
212            double dum = 0;
213    
214            if ( xMin > xMax ) {
215                dum = xMax;
216                xMax = xMin;
217                xMin = dum;
218            }
219    
220            if ( yMin > yMax ) {
221                dum = yMax;
222                yMax = yMin;
223                yMin = dum;
224            }
225    
226            destRect = GeometryFactory.createEnvelope( xMin, yMin, xMax, yMax, null );
227    
228            if ( sourceRect != null ) {
229                calculateQX();
230                calculateQY();
231            }
232    
233        }
234    
235        /**
236         * 
237         */
238        public Envelope getDestRect() {
239            return destRect;
240        }
241    
242        /**
243         * executes a coordinat transformation for the submitted x-coordinate of the source coordinat
244         * system.
245         * 
246         * @param xsource
247         *            x-coordinate of a point in the source coordinate system.
248         * @return the x-coordinate of the submitted value in the destination coordinate system.
249         */
250        public double getDestX( double xsource ) {
251            return destRect.getMin().getX()
252                + ( xsource - sourceRect.getMin().getX() ) * qx;
253        }
254    
255        /**
256         * executes a coordinat transformation for the submitted y-coordinate of the source coordinat
257         * system.
258         * 
259         * @param ysource
260         *            y-coordinate of a point in the source coordinate system.
261         * @return the y-coordinate of the submitted value in the destination coordinate system.
262         */
263        public double getDestY( double ysource ) {
264            return destRect.getMin().getY()
265                + destRect.getHeight() - ( ysource - sourceRect.getMin().getY() ) * qy;
266        }
267    
268        /**
269         * executes a coordinat transformation for the submitted point of the source coordinat system.
270         * 
271         * @param point
272         *            in the source coordinate system.
273         * @return the location of the submitted point in the destination coordinate system.
274         */
275        public Position getDestPoint( Position point ) {
276            double x = getDestX( point.getX() );
277            double y = getDestY( point.getY() );
278            return GeometryFactory.createPosition( x, y );
279        }
280    
281        /**
282         * executes a coordinat transformation for the submitted x-coordinate of the destination
283         * coordinate system.
284         * 
285         * @param xdest
286         *            x-coordinate of a point in the destination coordinate system.
287         * @return the x-coordinate of the submitted value in the source coordinate system.
288         */
289        public double getSourceX( double xdest ) {
290            return ( xdest - destRect.getMin().getX() )
291                / qx + sourceRect.getMin().getX();
292        }
293    
294        /**
295         * executes a coordinat transformation for the submitted y-coordinate of the destination
296         * coordinate system.
297         * 
298         * @param ydest
299         *            y-coordinate of a point in the destination coordinate system.
300         * @return the y-coordinate of the submitted value in the source coordinate system.
301         */
302        public double getSourceY( double ydest ) {
303            double d = ( destRect.getHeight() - ( ydest - destRect.getMin().getY() ) )
304                / qy + sourceRect.getMin().getY();
305            return d;
306    
307        }
308    
309        /**
310         * executes a coordinat transformation for the submitted point of the destination coordinate
311         * system.
312         * 
313         * @param point
314         *            in the destination coordinate system.
315         * @return the location of the submitted point in the source coordinate system.
316         */
317        public Position getSourcePoint( Position point ) {
318            double x = getSourceX( point.getX() );
319            double y = getSourceY( point.getY() );
320            return GeometryFactory.createPosition( x, y );
321        }
322    
323        /**
324         * calculates the relation between the width of the destination and the source coordinate
325         * system.
326         */
327        protected void calculateQX() {
328            qx = destRect.getWidth() / sourceRect.getWidth();
329        }
330    
331        /**
332         * calculates the relation between the height of the destination and the source coordinate
333         * system.
334         */
335        protected void calculateQY() {
336            qy = destRect.getHeight() / sourceRect.getHeight();
337        }
338    
339    }