Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / cts / gt2 / CoordTrans.java @ 101

History | View | Annotate | Download (3.08 KB)

1
/*
2
 * Creado el 07-ago-2004
3
 */
4
package org.cresques.cts.gt2;
5

    
6
import java.awt.geom.Point2D;
7

    
8
import org.cresques.cts.ICoordTrans;
9
import org.cresques.cts.IProjection;
10
import org.geotools.ct.CannotCreateTransformException;
11
import org.geotools.ct.CoordinateTransformation;
12
import org.geotools.ct.CoordinateTransformationFactory;
13
import org.geotools.ct.MathTransform;
14
import org.geotools.ct.TransformException;
15
import org.geotools.pt.CoordinatePoint;
16
import org.geotools.pt.MismatchedDimensionException;
17

    
18
/**
19
 * Transforma coordenadas entre dos sistemas
20
 * @see org.creques.cts.CoordSys
21
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
22
 */
23
public class CoordTrans implements ICoordTrans {
24
        private CoordinateTransformationFactory trFactory = CoordinateTransformationFactory.getDefault();
25
        private CoordinateTransformation tr = null;
26
        private MathTransform mt = null, mt2 = null, mt3 = null, mtDatum = null;
27
        private CoordSys from = null, to = null;
28

    
29
        public CoordTrans(CoordSys from, CoordSys to) {
30
                this.from = from;
31
                this.to = to;
32
                
33
                // Si los dos CoordSys son proyectados, entonces hay
34
                // que hacer dos transformaciones (pasar por geogr?ficas)
35
                // Si hay cambio de datum son 3 las transformaciones
36
                try {
37
                        if (from.getDatum() != to.getDatum()) {
38
                                tr = trFactory.createFromCoordinateSystems(from.toGeo().getCS(), to.toGeo().getCS());
39
                                mtDatum = tr.getMathTransform();
40
                        }
41
                        if (from.projCS != null && to.projCS != null) {
42
                                CoordSys geogcs = from.toGeo();
43
                                tr = trFactory.createFromCoordinateSystems(from.getCS(), geogcs.getCS());
44
                                mt = tr.getMathTransform();
45
                                if (mtDatum != null)
46
                                        mt2 = mtDatum;
47
                                geogcs = to.toGeo();
48
                                tr = trFactory.createFromCoordinateSystems(geogcs.getCS(), to.getCS());
49
                                if (mt2 == null)
50
                                        mt2 = tr.getMathTransform();
51
                                else
52
                                        mt3 = tr.getMathTransform();
53
                        } else {
54
                                if (from.projCS == null)
55
                                        mt = mtDatum;
56
                                tr = trFactory.createFromCoordinateSystems(from.getCS(), to.getCS());
57
                                if (mt == null) {
58
                                        mt = tr.getMathTransform();
59
                                        if (mtDatum != null)
60
                                                mt2 = mtDatum;
61
                                } else
62
                                        mt2 = tr.getMathTransform();
63
                        }
64
                } catch (CannotCreateTransformException e) {
65
                        // TODO Bloque catch generado autom?ticamente
66
                        e.printStackTrace();
67
                }
68
        }
69
        
70
        public IProjection getPOrig() { return from; }
71
        public IProjection getPDest() { return to; }
72
        
73
        public ICoordTrans getInverted() {
74
                ICoordTrans cti = null;
75
                return cti;
76
        }
77
        
78
        public Point2D convert(Point2D ptOrig, Point2D ptDest) {
79
                CoordinatePoint pt1 = new CoordinatePoint(ptOrig);
80
                CoordinatePoint pt2 = new CoordinatePoint(0D,0D);
81
                ptDest = null;
82
                try {
83
                        mt.transform(pt1, pt2);
84
                        ptDest = pt2.toPoint2D();
85
                        if (mt2 != null) {
86
                                mt2.transform(pt2, pt1);
87
                                ptDest = pt1.toPoint2D();
88
                                if (mt3 != null) {
89
                                        mt3.transform(pt1, pt2);
90
                                        ptDest = pt2.toPoint2D();
91
                                }
92
                        }
93
                } catch (MismatchedDimensionException e) {
94
                        // TODO Bloque catch generado autom?ticamente
95
                        e.printStackTrace();
96
                } catch (TransformException e) {
97
                        // TODO Bloque catch generado autom?ticamente
98
                        e.printStackTrace();
99
                }
100
                return ptDest;
101
        }
102

    
103
        
104
        public String toString() {
105
                return tr.toString();
106
        }
107
}