svn-gvsig-desktop / tags / v1_1_Build_1004 / libraries / libCq CMS for java.old / src / org / cresques / cts / gt2 / CoordTrans.java @ 12319
History | View | Annotate | Download (5.56 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 |
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 |
|
58 |
private ICoordTrans invertedCT = null; |
59 |
|
60 |
public CoordTrans(CoordSys from, CoordSys to) {
|
61 |
this.from = from;
|
62 |
this.to = to;
|
63 |
|
64 |
// Si los dos CoordSys son proyectados, entonces hay
|
65 |
// que hacer dos transformaciones (pasar por geogr?ficas)
|
66 |
// Si hay cambio de datum son 3 las transformaciones
|
67 |
try {
|
68 |
if (from.getDatum() != to.getDatum()) {
|
69 |
tr = trFactory.createFromCoordinateSystems(from.toGeo().getCS(), |
70 |
to.toGeo().getCS()); |
71 |
mtDatum = tr.getMathTransform(); |
72 |
} |
73 |
|
74 |
if ((from.projCS != null) && (to.projCS != null)) { |
75 |
CoordSys geogcs = from.toGeo(); |
76 |
tr = trFactory.createFromCoordinateSystems(from.getCS(), |
77 |
geogcs.getCS()); |
78 |
mt = tr.getMathTransform(); |
79 |
|
80 |
if (mtDatum != null) { |
81 |
mt2 = mtDatum; |
82 |
} |
83 |
|
84 |
geogcs = to.toGeo(); |
85 |
tr = trFactory.createFromCoordinateSystems(geogcs.getCS(), |
86 |
to.getCS()); |
87 |
|
88 |
if (mt2 == null) { |
89 |
mt2 = tr.getMathTransform(); |
90 |
} else {
|
91 |
mt3 = tr.getMathTransform(); |
92 |
} |
93 |
} else {
|
94 |
if (from.projCS == null) { |
95 |
mt = mtDatum; |
96 |
} |
97 |
|
98 |
tr = trFactory.createFromCoordinateSystems(from.getCS(), |
99 |
to.getCS()); |
100 |
|
101 |
if (mt == null) { |
102 |
mt = tr.getMathTransform(); |
103 |
|
104 |
if (mtDatum != null) { |
105 |
mt2 = mtDatum; |
106 |
} |
107 |
} else {
|
108 |
mt2 = tr.getMathTransform(); |
109 |
} |
110 |
} |
111 |
} catch (CannotCreateTransformException e) {
|
112 |
// TODO Bloque catch generado autom?ticamente
|
113 |
e.printStackTrace(); |
114 |
} |
115 |
} |
116 |
|
117 |
public IProjection getPOrig() {
|
118 |
return from;
|
119 |
} |
120 |
|
121 |
public IProjection getPDest() {
|
122 |
return to;
|
123 |
} |
124 |
|
125 |
public ICoordTrans getInverted() {
|
126 |
if (invertedCT == null) |
127 |
invertedCT = new CoordTrans(to, from);
|
128 |
return invertedCT;
|
129 |
} |
130 |
|
131 |
public Point2D convert(Point2D ptOrig, Point2D ptDest) { |
132 |
CoordinatePoint pt1 = new CoordinatePoint(ptOrig);
|
133 |
CoordinatePoint pt2 = new CoordinatePoint(0D, 0D); |
134 |
ptDest = null;
|
135 |
|
136 |
try {
|
137 |
mt.transform(pt1, pt2); |
138 |
ptDest = pt2.toPoint2D(); |
139 |
|
140 |
if (mt2 != null) { |
141 |
mt2.transform(pt2, pt1); |
142 |
ptDest = pt1.toPoint2D(); |
143 |
|
144 |
if (mt3 != null) { |
145 |
mt3.transform(pt1, pt2); |
146 |
ptDest = pt2.toPoint2D(); |
147 |
} |
148 |
} |
149 |
|
150 |
/*} catch (MismatchedDimensionException e) {
|
151 |
// TODO Bloque catch generado autom?ticamente
|
152 |
e.printStackTrace();
|
153 |
*/
|
154 |
} catch (TransformException e) {
|
155 |
// TODO Bloque catch generado autom?ticamente
|
156 |
e.printStackTrace(); |
157 |
} |
158 |
|
159 |
return ptDest;
|
160 |
} |
161 |
|
162 |
public String toString() { |
163 |
return tr.toString();
|
164 |
} |
165 |
|
166 |
/* (non-Javadoc)
|
167 |
* @see org.cresques.cts.ICoordTrans#convert(java.awt.geom.Rectangle2D)
|
168 |
*/
|
169 |
public Rectangle2D convert(Rectangle2D rect) { |
170 |
Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); |
171 |
Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); |
172 |
pt1 = convert(pt1, null);
|
173 |
pt2 = convert(pt2, null);
|
174 |
rect = new Rectangle2D.Double(); |
175 |
rect.setFrameFromDiagonal(pt1, pt2); |
176 |
|
177 |
return rect;
|
178 |
} |
179 |
} |