Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / cts / gt2 / CoordTrans.java @ 3096

History | View | Annotate | Download (5.57 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.cts.gt2;
25

    
26
import org.cresques.cts.ICoordTrans;
27
import org.cresques.cts.IProjection;
28

    
29
import org.geotools.ct.*;
30

    
31
//Geotools dependencies
32
import org.geotools.pt.*;
33

    
34
//OpenGIS dependencies
35
import org.opengis.referencing.operation.TransformException;
36

    
37
import java.awt.geom.Point2D;
38
import java.awt.geom.Rectangle2D;
39

    
40

    
41
//import org.geotools.pt.MismatchedDimensionException;
42

    
43
/**
44
 * Transforma coordenadas entre dos sistemas
45
 * @see org.creques.cts.CoordSys
46
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
47
 */
48
public class CoordTrans implements ICoordTrans {
49
    private CoordinateTransformationFactory trFactory = CoordinateTransformationFactory.getDefault();
50
    private CoordinateTransformation tr = null;
51
    private MathTransform mt = null;
52
    private MathTransform mt2 = null;
53
    private MathTransform mt3 = null;
54
    private MathTransform mtDatum = null;
55
    private CoordSys from = null;
56
    private CoordSys to = null;
57
    private ICoordTrans invertedCT = null;
58

    
59
    public CoordTrans(CoordSys from, CoordSys to) {
60
        this.from = from;
61
        this.to = to;
62

    
63
        // Si los dos CoordSys son proyectados, entonces hay
64
        // que hacer dos transformaciones (pasar por geogr?ficas)
65
        // Si hay cambio de datum son 3 las transformaciones
66
        try {
67
            if (from.getDatum() != to.getDatum()) {
68
                tr = trFactory.createFromCoordinateSystems(from.toGeo().getCS(),
69
                                                           to.toGeo().getCS());
70
                mtDatum = tr.getMathTransform();
71
            }
72

    
73
            if ((from.projCS != null) && (to.projCS != null)) {
74
                CoordSys geogcs = from.toGeo();
75
                tr = trFactory.createFromCoordinateSystems(from.getCS(),
76
                                                           geogcs.getCS());
77
                mt = tr.getMathTransform();
78

    
79
                if (mtDatum != null) {
80
                    mt2 = mtDatum;
81
                }
82

    
83
                geogcs = to.toGeo();
84
                tr = trFactory.createFromCoordinateSystems(geogcs.getCS(),
85
                                                           to.getCS());
86

    
87
                if (mt2 == null) {
88
                    mt2 = tr.getMathTransform();
89
                } else {
90
                    mt3 = tr.getMathTransform();
91
                }
92
            } else {
93
                if (from.projCS == null) {
94
                    mt = mtDatum;
95
                }
96

    
97
                tr = trFactory.createFromCoordinateSystems(from.getCS(),
98
                                                           to.getCS());
99

    
100
                if (mt == null) {
101
                    mt = tr.getMathTransform();
102

    
103
                    if (mtDatum != null) {
104
                        mt2 = mtDatum;
105
                    }
106
                } else {
107
                    mt2 = tr.getMathTransform();
108
                }
109
            }
110
        } catch (CannotCreateTransformException e) {
111
            // TODO Bloque catch generado autom?ticamente
112
            e.printStackTrace();
113
        }
114
    }
115

    
116
    public IProjection getPOrig() {
117
        return from;
118
    }
119

    
120
    public IProjection getPDest() {
121
        return to;
122
    }
123

    
124
    public ICoordTrans getInverted() {
125
        if (invertedCT == null)
126
            invertedCT = new CoordTrans(to, from);
127
        return invertedCT;
128
    }
129

    
130
    public Point2D convert(Point2D ptOrig, Point2D ptDest) {
131
        CoordinatePoint pt1 = new CoordinatePoint(ptOrig);
132
        CoordinatePoint pt2 = new CoordinatePoint(0D, 0D);
133
        ptDest = null;
134

    
135
        try {
136
            mt.transform(pt1, pt2);
137
            ptDest = pt2.toPoint2D();
138

    
139
            if (mt2 != null) {
140
                mt2.transform(pt2, pt1);
141
                ptDest = pt1.toPoint2D();
142

    
143
                if (mt3 != null) {
144
                    mt3.transform(pt1, pt2);
145
                    ptDest = pt2.toPoint2D();
146
                }
147
            }
148

    
149
            /*} catch (MismatchedDimensionException e) {
150
                    // TODO Bloque catch generado autom?ticamente
151
                    e.printStackTrace();
152
            */
153
        } catch (TransformException e) {
154
            // TODO Bloque catch generado autom?ticamente
155
            e.printStackTrace();
156
        }
157

    
158
        return ptDest;
159
    }
160

    
161
    public String toString() {
162
        return tr.toString();
163
    }
164

    
165
    /* (non-Javadoc)
166
     * @see org.cresques.cts.ICoordTrans#convert(java.awt.geom.Rectangle2D)
167
     */
168
    public Rectangle2D convert(Rectangle2D rect) {
169
        Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY());
170
        Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY());
171
        pt1 = convert(pt1, null);
172
        pt2 = convert(pt2, null);
173
        rect = new Rectangle2D.Double();
174
        rect.setFrameFromDiagonal(pt1, pt2);
175

    
176
        return rect;
177
    }
178
}