Statistics
| Revision:

root / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / cts / gt2 / CoordTrans.java @ 2249

History | View | Annotate | Download (4.74 KB)

1
/*
2
 * Created on 21-jun-2005
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package org.cresques.cts.gt2;
48

    
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51

    
52
import org.cresques.cts.ICoordTrans;
53
import org.cresques.cts.IProjection;
54
//Geotools dependencies
55
import org.geotools.pt.*;
56
import org.geotools.ct.*;
57

    
58
//OpenGIS dependencies
59
import org.opengis.referencing.operation.TransformException;
60

    
61
//import org.geotools.pt.MismatchedDimensionException;
62

    
63
/**
64
 * Transforma coordenadas entre dos sistemas
65
 * @see org.creques.cts.CoordSys
66
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
67
 */
68
public class CoordTrans implements ICoordTrans {
69
        private CoordinateTransformationFactory trFactory = CoordinateTransformationFactory.getDefault();
70
        private CoordinateTransformation tr = null;
71
        private MathTransform mt = null, mt2 = null, mt3 = null, mtDatum = null;
72
        private CoordSys from = null, to = null;
73

    
74
        public CoordTrans(CoordSys from, CoordSys to) {
75
                this.from = from;
76
                this.to = to;
77
                
78
                // Si los dos CoordSys son proyectados, entonces hay
79
                // que hacer dos transformaciones (pasar por geogr?ficas)
80
                // Si hay cambio de datum son 3 las transformaciones
81
                try {
82
                        if (from.getDatum() != to.getDatum()) {
83
                                tr = trFactory.createFromCoordinateSystems(from.toGeo().getCS(), to.toGeo().getCS());
84
                                mtDatum = tr.getMathTransform();
85
                        }
86
                        if (from.projCS != null && to.projCS != null) {
87
                                CoordSys geogcs = from.toGeo();
88
                                tr = trFactory.createFromCoordinateSystems(from.getCS(), geogcs.getCS());
89
                                mt = tr.getMathTransform();
90
                                if (mtDatum != null)
91
                                        mt2 = mtDatum;
92
                                geogcs = to.toGeo();
93
                                tr = trFactory.createFromCoordinateSystems(geogcs.getCS(), to.getCS());
94
                                if (mt2 == null)
95
                                        mt2 = tr.getMathTransform();
96
                                else
97
                                        mt3 = tr.getMathTransform();
98
                        } else {
99
                                if (from.projCS == null)
100
                                        mt = mtDatum;
101
                                tr = trFactory.createFromCoordinateSystems(from.getCS(), to.getCS());
102
                                if (mt == null) {
103
                                        mt = tr.getMathTransform();
104
                                        if (mtDatum != null)
105
                                                mt2 = mtDatum;
106
                                } else
107
                                        mt2 = tr.getMathTransform();
108
                        }
109
                } catch (CannotCreateTransformException e) {
110
                        // TODO Bloque catch generado autom?ticamente
111
                        e.printStackTrace();
112
                }
113
        }
114
        
115
        public IProjection getPOrig() { return from; }
116
        public IProjection getPDest() { return to; }
117
        
118
        public ICoordTrans getInverted() {
119
                ICoordTrans cti = null;
120
                return cti;
121
        }
122
        
123
        public Point2D convert(Point2D ptOrig, Point2D ptDest) {
124
                CoordinatePoint pt1 = new CoordinatePoint(ptOrig);
125
                CoordinatePoint pt2 = new CoordinatePoint(0D,0D);
126
                ptDest = null;
127
                try {
128
                        mt.transform(pt1, pt2);
129
                        ptDest = pt2.toPoint2D();
130
                        if (mt2 != null) {
131
                                mt2.transform(pt2, pt1);
132
                                ptDest = pt1.toPoint2D();
133
                                if (mt3 != null) {
134
                                        mt3.transform(pt1, pt2);
135
                                        ptDest = pt2.toPoint2D();
136
                                }
137
                        }
138
                /*} catch (MismatchedDimensionException e) {
139
                        // TODO Bloque catch generado autom?ticamente
140
                        e.printStackTrace();
141
                */} catch (TransformException e) {
142
                        // TODO Bloque catch generado autom?ticamente
143
                        e.printStackTrace();
144
                }
145
                return ptDest;
146
        }
147

    
148
        
149
        public String toString() {
150
                return tr.toString();
151
        }
152

    
153
        /* (non-Javadoc)
154
         * @see org.cresques.cts.ICoordTrans#convert(java.awt.geom.Rectangle2D)
155
         */
156
        public Rectangle2D convert(Rectangle2D rect) {
157
                Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); 
158
                Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY());
159
                pt1 = convert(pt1, null);
160
                pt2 = convert(pt2, null);
161
                rect = new Rectangle2D.Double();
162
                rect.setFrameFromDiagonal(pt1, pt2);
163
                return rect;
164
        }
165
}