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