Statistics
| Revision:

root / trunk / libraries / libJCRS / src / org / gvsig / crs / COperation.java @ 8676

History | View | Annotate | Download (3.72 KB)

1
package org.gvsig.crs;
2

    
3
import java.awt.geom.Point2D;
4
import java.awt.geom.Rectangle2D;
5

    
6
import org.cresques.cts.ICoordTrans;
7
import org.cresques.cts.IProjection;
8
import org.gvsig.crs.proj.CrsProj;
9
import org.gvsig.crs.proj.CrsProjException;
10
import org.gvsig.crs.proj.JNIBaseCrs;
11
import org.gvsig.crs.proj.OperationCrsException;
12

    
13
import com.iver.andami.messages.NotificationManager;
14

    
15
public class COperation implements ICOperation {
16
        private Crs sourceCrs;
17
        private Crs targetCrs;
18
        private CrsProj nadCrsProj = null;
19
        private boolean nadInTarget;
20
        public COperation(ICrs from, ICrs to) throws CrsException {
21
                sourceCrs = (Crs) from;
22
                targetCrs = (Crs) to;
23
        }
24

    
25
        public ICrs getSource() {
26
                return sourceCrs;
27
        }
28

    
29
        public ICrs getTarget() {
30
                return targetCrs;
31
        }
32

    
33
        public Point2D operate(Point2D pt) throws CrsException {
34
                double x[] = {pt.getX()};
35
                double y[] = {pt.getY()};
36
                double z[] = {0D};
37
                int errno = 0;
38
                try {
39
                        if (nadCrsProj != null){
40
                                if (nadInTarget)
41
                                        errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), nadCrsProj);
42
                                else
43
                                        errno = JNIBaseCrs.operate( x , y, z,nadCrsProj,targetCrs.getCrsProj());
44
                                if (errno != -38)
45
                                        return new Point2D.Double(x[0],y[0]);
46
                        }
47
                        
48
                        // Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
49
                        x[0] = pt.getX();
50
                        y[0] = pt.getY();
51
                        z[0] = 0D;
52
                        JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj());
53
                        return new Point2D.Double(x[0],y[0]);
54
                        
55
                } catch (OperationCrsException e) {
56
                        throw new CrsException(e);
57
                } catch (CrsProjException e) {
58
                        throw new CrsException(e);
59
                }
60
        //        System.out.println("x="+x[0]+"y="+y[0]);
61
        //        if(!targetCrs.isProjected())
62
        //                return new Point2D.Double(x[0]*((180)/Math.PI),y[0]*((180)/Math.PI));
63
        //        else
64
        }
65

    
66
        public double [] operate(double []ptOrig) throws CrsException {
67
                double x[] = {ptOrig[0]};
68
                double y[] = {ptOrig[1]};
69
                double z[] = {ptOrig[2]};
70
                int errno = 0;
71
                try {
72
                        if (nadCrsProj != null){
73
                                if (nadInTarget)
74
                                        errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), nadCrsProj);
75
                                else
76
                                        errno = JNIBaseCrs.operate( x , y, z,nadCrsProj,targetCrs.getCrsProj());
77
                                if (errno != -38){
78
                                        double ptDest[] = {x[0], y[0], z[0]};
79
                                        return ptDest;
80
                                }
81
                        }
82
                        
83
                        // Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
84
                         x[0] = ptOrig[0];
85
                         y[0] = ptOrig[1];
86
                         z[0] = ptOrig[2];
87
                         JNIBaseCrs.operate(x, y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj());
88
                         double ptDest[] = {x[0], y[0], z[0]};
89
                         return ptDest;
90
                } catch (OperationCrsException e) {
91
                        throw new CrsException(e);
92
                } catch (CrsProjException e) {
93
                        throw new CrsException(e);
94
                }
95
        }
96

    
97
        public IProjection getPOrig() {
98
                return null;
99
        }
100

    
101
        public IProjection getPDest() {
102
                return null;
103
        }
104

    
105
        public Point2D convert(Point2D ptOrig, Point2D ptDest) {
106
                try {
107
                        ptDest = operate(ptOrig);
108
                } catch (CrsException e) {
109
                        // TODO LWS implementar una gesti?n completa de los errores.
110
                        NotificationManager.addError(e);
111
                }
112
                return ptDest;
113
        }
114

    
115
        public Rectangle2D convert(Rectangle2D rect) {
116
                Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY());
117
                Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY());
118
                try {
119
                        pt1 = operate(pt1);
120
                        pt2 = operate(pt2);
121
                } catch (CrsException e) {
122
                        // TODO LWS implementar una gesti?n completa de los errores.
123
                        e.printStackTrace();
124
                }
125
                rect = new Rectangle2D.Double();
126
                rect.setFrameFromDiagonal(pt1, pt2);
127
                return rect;
128
        }
129

    
130
        public ICoordTrans getInverted() {
131
                try {
132
                        return new COperation(targetCrs, sourceCrs);
133
                } catch (CrsException e) {
134
                        e.printStackTrace();
135
                }
136
                return null;
137
        }
138
        
139
        public void setNadCrsProj(CrsProj nadCrs, boolean inTarget){
140
                this.nadInTarget = inTarget;
141
                this.nadCrsProj = nadCrs;
142
        }
143
}