svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.projection / org.gvsig.projection.cresques / org.gvsig.projection.cresques.impl / src / main / java / org / cresques / impl / cts / gt2 / CoordTrans.java @ 40559
History | View | Annotate | Download (6.46 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.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 |
|
169 |
Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); |
170 |
Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); |
171 |
Point2D pt3 = new Point2D.Double(rect.getMinX(), rect.getMaxY()); |
172 |
Point2D pt4 = new Point2D.Double(rect.getMaxX(), rect.getMinY()); |
173 |
|
174 |
convert(pt1, pt1); |
175 |
convert(pt2, pt2); |
176 |
convert(pt3, pt3); |
177 |
convert(pt4, pt4); |
178 |
|
179 |
double min_x = Math.min( |
180 |
Math.min(pt1.getX(), pt2.getX()),
|
181 |
Math.min(pt3.getX(), pt4.getX()));
|
182 |
double min_y = Math.min( |
183 |
Math.min(pt1.getY(), pt2.getY()),
|
184 |
Math.min(pt3.getY(), pt4.getY()));
|
185 |
double max_x = Math.max( |
186 |
Math.max(pt1.getX(), pt2.getX()),
|
187 |
Math.max(pt3.getX(), pt4.getX()));
|
188 |
double max_y = Math.max( |
189 |
Math.max(pt1.getY(), pt2.getY()),
|
190 |
Math.max(pt3.getY(), pt4.getY()));
|
191 |
|
192 |
return new Rectangle2D.Double( |
193 |
min_x, min_y, max_x - min_x, max_y - min_y); |
194 |
|
195 |
} |
196 |
} |