svn-gvsig-desktop / branches / libProjection_v2_0_prep / libraries / libProjection / src / org / cresques / impl / cts / gt2 / CoordTrans.java @ 27135
History | View | Annotate | Download (5.7 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.impl.cts.gt2; |
25 |
|
26 |
import java.awt.geom.Point2D; |
27 |
import java.awt.geom.Rectangle2D; |
28 |
|
29 |
import org.cresques.cts.ICoordTrans; |
30 |
import org.cresques.cts.IProjection; |
31 |
import org.geotools.ct.CannotCreateTransformException; |
32 |
import org.geotools.ct.CoordinateTransformation; |
33 |
import org.geotools.ct.CoordinateTransformationFactory; |
34 |
import org.geotools.ct.MathTransform; |
35 |
import org.geotools.pt.CoordinatePoint; |
36 |
import org.opengis.referencing.operation.TransformException; |
37 |
|
38 |
|
39 |
//import org.geotools.pt.MismatchedDimensionException;
|
40 |
|
41 |
/**
|
42 |
* Transforma coordenadas entre dos sistemas
|
43 |
* @see org.creques.cts.CoordSys
|
44 |
* @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
45 |
*/
|
46 |
class CoordTrans implements ICoordTrans { |
47 |
private CoordinateTransformationFactory trFactory = CoordinateTransformationFactory.getDefault();
|
48 |
private CoordinateTransformation tr = null; |
49 |
private MathTransform mt = null; |
50 |
private MathTransform mt2 = null; |
51 |
private MathTransform mt3 = null; |
52 |
private MathTransform mtDatum = null; |
53 |
private CoordSys from = null; |
54 |
private CoordSys to = null; |
55 |
|
56 |
private ICoordTrans invertedCT = null; |
57 |
|
58 |
public CoordTrans(CoordSys from, CoordSys to) {
|
59 |
this.from = from;
|
60 |
this.to = to;
|
61 |
|
62 |
// Si los dos CoordSys son proyectados, entonces hay
|
63 |
// que hacer dos transformaciones (pasar por geogr?ficas)
|
64 |
// Si hay cambio de datum son 3 las transformaciones
|
65 |
try {
|
66 |
if (from.getDatum() != to.getDatum()) {
|
67 |
tr = trFactory.createFromCoordinateSystems(from.toGeo().getCS(), |
68 |
to.toGeo().getCS()); |
69 |
mtDatum = tr.getMathTransform(); |
70 |
} |
71 |
|
72 |
if ((from.projCS != null) && (to.projCS != null)) { |
73 |
CoordSys geogcs = from.toGeo(); |
74 |
tr = trFactory.createFromCoordinateSystems(from.getCS(), |
75 |
geogcs.getCS()); |
76 |
mt = tr.getMathTransform(); |
77 |
|
78 |
if (mtDatum != null) { |
79 |
mt2 = mtDatum; |
80 |
} |
81 |
|
82 |
geogcs = to.toGeo(); |
83 |
tr = trFactory.createFromCoordinateSystems(geogcs.getCS(), |
84 |
to.getCS()); |
85 |
|
86 |
if (mt2 == null) { |
87 |
mt2 = tr.getMathTransform(); |
88 |
} else {
|
89 |
mt3 = tr.getMathTransform(); |
90 |
} |
91 |
} else {
|
92 |
if (from.projCS == null) { |
93 |
mt = mtDatum; |
94 |
} |
95 |
|
96 |
tr = trFactory.createFromCoordinateSystems(from.getCS(), |
97 |
to.getCS()); |
98 |
|
99 |
if (mt == null) { |
100 |
mt = tr.getMathTransform(); |
101 |
|
102 |
if (mtDatum != null) { |
103 |
mt2 = mtDatum; |
104 |
} |
105 |
} else {
|
106 |
mt2 = tr.getMathTransform(); |
107 |
} |
108 |
} |
109 |
} catch (CannotCreateTransformException e) {
|
110 |
// TODO Bloque catch generado autom?ticamente
|
111 |
e.printStackTrace(); |
112 |
} |
113 |
} |
114 |
|
115 |
public IProjection getPOrig() {
|
116 |
return from;
|
117 |
} |
118 |
|
119 |
public IProjection getPDest() {
|
120 |
return to;
|
121 |
} |
122 |
|
123 |
public ICoordTrans getInverted() {
|
124 |
if (invertedCT == null) |
125 |
invertedCT = new CoordTrans(to, from);
|
126 |
return invertedCT;
|
127 |
} |
128 |
|
129 |
public Point2D convert(Point2D ptOrig, Point2D ptDest) { |
130 |
CoordinatePoint pt1 = new CoordinatePoint(ptOrig);
|
131 |
CoordinatePoint pt2 = new CoordinatePoint(0D, 0D); |
132 |
ptDest = null;
|
133 |
|
134 |
try {
|
135 |
mt.transform(pt1, pt2); |
136 |
ptDest = pt2.toPoint2D(); |
137 |
|
138 |
if (mt2 != null) { |
139 |
mt2.transform(pt2, pt1); |
140 |
ptDest = pt1.toPoint2D(); |
141 |
|
142 |
if (mt3 != null) { |
143 |
mt3.transform(pt1, pt2); |
144 |
ptDest = pt2.toPoint2D(); |
145 |
} |
146 |
} |
147 |
|
148 |
/*} catch (MismatchedDimensionException e) {
|
149 |
// TODO Bloque catch generado autom?ticamente
|
150 |
e.printStackTrace();
|
151 |
*/
|
152 |
} catch (TransformException e) {
|
153 |
// TODO Bloque catch generado autom?ticamente
|
154 |
e.printStackTrace(); |
155 |
} |
156 |
|
157 |
return ptDest;
|
158 |
} |
159 |
|
160 |
public String toString() { |
161 |
return tr.toString();
|
162 |
} |
163 |
|
164 |
/* (non-Javadoc)
|
165 |
* @see org.cresques.cts.ICoordTrans#convert(java.awt.geom.Rectangle2D)
|
166 |
*/
|
167 |
public Rectangle2D convert(Rectangle2D rect) { |
168 |
Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); |
169 |
Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); |
170 |
pt1 = convert(pt1, null);
|
171 |
pt2 = convert(pt2, null);
|
172 |
rect = new Rectangle2D.Double(); |
173 |
rect.setFrameFromDiagonal(pt1, pt2); |
174 |
|
175 |
return rect;
|
176 |
} |
177 |
} |