001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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,
114: double sourceXMax, double sourceYMax, double destXMin,
115: double destYMin, 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) && (destRect != null)) {
132: calculateQX();
133: calculateQY();
134: }
135: }
136:
137: /**
138: * sets the source rectangle
139: *
140: * @param xMin
141: * minimum x-coordinate (source system) of the map.
142: * @param yMin
143: * minimum y-coordinate (source system) of the map.
144: * @param xMax
145: * maximum x-coordinate (source system) of the map.
146: * @param yMax
147: * maximum y-coordinate (source system) of the map.
148: */
149: public void setSourceRect(double xMin, double yMin, double xMax,
150: 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,
167: yMax, null);
168:
169: if (destRect != null) {
170: calculateQX();
171: calculateQY();
172: }
173:
174: }
175:
176: /**
177: *
178: */
179: public Envelope getSourceRect() {
180: return sourceRect;
181: }
182:
183: /**
184: * sets the destination rectangle.
185: *
186: * @param rect
187: * is the boundary of the destination rectangle (for example a region on the screen)
188: *
189: */
190: public void setDestRect(Envelope rect) {
191: destRect = rect;
192: if ((sourceRect != null) && (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,
211: double yMax) {
212:
213: double dum = 0;
214:
215: if (xMin > xMax) {
216: dum = xMax;
217: xMax = xMin;
218: xMin = dum;
219: }
220:
221: if (yMin > yMax) {
222: dum = yMax;
223: yMax = yMin;
224: yMin = dum;
225: }
226:
227: destRect = GeometryFactory.createEnvelope(xMin, yMin, xMax,
228: yMax, null);
229:
230: if (sourceRect != null) {
231: calculateQX();
232: calculateQY();
233: }
234:
235: }
236:
237: /**
238: *
239: */
240: public Envelope getDestRect() {
241: return destRect;
242: }
243:
244: /**
245: * executes a coordinat transformation for the submitted x-coordinate of the source coordinat
246: * system.
247: *
248: * @param xsource
249: * x-coordinate of a point in the source coordinate system.
250: * @return the x-coordinate of the submitted value in the destination coordinate system.
251: */
252: public double getDestX(double xsource) {
253: return destRect.getMin().getX()
254: + (xsource - sourceRect.getMin().getX()) * qx;
255: }
256:
257: /**
258: * executes a coordinat transformation for the submitted y-coordinate of the source coordinat
259: * system.
260: *
261: * @param ysource
262: * y-coordinate of a point in the source coordinate system.
263: * @return the y-coordinate of the submitted value in the destination coordinate system.
264: */
265: public double getDestY(double ysource) {
266: return destRect.getMin().getY() + destRect.getHeight()
267: - (ysource - sourceRect.getMin().getY()) * qy;
268: }
269:
270: /**
271: * executes a coordinat transformation for the submitted point of the source coordinat system.
272: *
273: * @param point
274: * in the source coordinate system.
275: * @return the location of the submitted point in the destination coordinate system.
276: */
277: public Position getDestPoint(Position point) {
278: double x = getDestX(point.getX());
279: double y = getDestY(point.getY());
280: return GeometryFactory.createPosition(x, y);
281: }
282:
283: /**
284: * executes a coordinat transformation for the submitted x-coordinate of the destination
285: * coordinate system.
286: *
287: * @param xdest
288: * x-coordinate of a point in the destination coordinate system.
289: * @return the x-coordinate of the submitted value in the source coordinate system.
290: */
291: public double getSourceX(double xdest) {
292: return (xdest - destRect.getMin().getX()) / qx
293: + sourceRect.getMin().getX();
294: }
295:
296: /**
297: * executes a coordinat transformation for the submitted y-coordinate of the destination
298: * coordinate system.
299: *
300: * @param ydest
301: * y-coordinate of a point in the destination coordinate system.
302: * @return the y-coordinate of the submitted value in the source coordinate system.
303: */
304: public double getSourceY(double ydest) {
305: double d = (destRect.getHeight() - (ydest - destRect.getMin()
306: .getY()))
307: / qy + sourceRect.getMin().getY();
308: return d;
309:
310: }
311:
312: /**
313: * executes a coordinat transformation for the submitted point of the destination coordinate
314: * system.
315: *
316: * @param point
317: * in the destination coordinate system.
318: * @return the location of the submitted point in the source coordinate system.
319: */
320: public Position getSourcePoint(Position point) {
321: double x = getSourceX(point.getX());
322: double y = getSourceY(point.getY());
323: return GeometryFactory.createPosition(x, y);
324: }
325:
326: /**
327: * calculates the relation between the width of the destination and the source coordinate
328: * system.
329: */
330: protected void calculateQX() {
331: qx = destRect.getWidth() / sourceRect.getWidth();
332: }
333:
334: /**
335: * calculates the relation between the height of the destination and the source coordinate
336: * system.
337: */
338: protected void calculateQY() {
339: qy = destRect.getHeight() / sourceRect.getHeight();
340: }
341:
342: }
|