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