Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1014 / libraries / libJCRS / src / org / gvsig / crs / COperation.java @ 13593

History | View | Annotate | Download (5.07 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.crs;
42

    
43
import java.awt.geom.Point2D;
44
import java.awt.geom.Rectangle2D;
45

    
46
import org.cresques.cts.ICoordTrans;
47
import org.cresques.cts.IProjection;
48
import org.gvsig.crs.proj.CrsProj;
49
import org.gvsig.crs.proj.JNIBaseCrs;
50
import org.gvsig.crs.proj.OperationCrsException;
51

    
52
import com.iver.andami.messages.NotificationManager;
53

    
54
/**
55
 * Clase que implementa las operaciones de conversi?n y transformaci?n 
56
 * de coordenadas entre CRSs.
57
 * 
58
 * @author Miguel Garc?a Jim?nez (garciajimenez.miguel@gmail.com)
59
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
60
 *
61
 */
62

    
63
public class COperation implements ICOperation {
64
        private Crs sourceCrs;
65
        private Crs targetCrs;
66
        private CrsProj nadCrsProj = null;
67
        private boolean nadInTarget;
68
        
69
        public COperation(ICrs from, ICrs to) throws CrsException {
70
                sourceCrs = (Crs) from;
71
                targetCrs = (Crs) to;
72
        }
73

    
74
        public ICrs getSource() {
75
                return sourceCrs;
76
        }
77

    
78
        public ICrs getTarget() {
79
                return targetCrs;
80
        }
81

    
82
        public Point2D operate(Point2D pt) throws CrsException {
83
                double x[] = {pt.getX()};
84
                double y[] = {pt.getY()};
85
                double z[] = {0D};
86
                int errno = 0;
87
                try {
88
                        if (nadCrsProj != null){
89
                                if (nadInTarget)
90
                                        errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), nadCrsProj);
91
                                else
92
                                        errno = JNIBaseCrs.operate( x , y, z,nadCrsProj,targetCrs.getCrsProj());
93
                                if (errno != -38)
94
                                        return new Point2D.Double(x[0],y[0]);
95
                        }
96
                        
97
                        // Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
98
                        x[0] = pt.getX();
99
                        y[0] = pt.getY();
100
                        z[0] = 0D;
101
                        errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj());
102
                        return new Point2D.Double(x[0],y[0]);
103
                        
104
                } catch (OperationCrsException e) {
105
                        throw new CrsException(e);
106
                }
107
        //        System.out.println("x="+x[0]+"y="+y[0]);
108
        //        if(!targetCrs.isProjected())
109
        //                return new Point2D.Double(x[0]*((180)/Math.PI),y[0]*((180)/Math.PI));
110
        //        else
111
        }
112

    
113
        public double [] operate(double []ptOrig) throws CrsException {
114
                double x[] = {ptOrig[0]};
115
                double y[] = {ptOrig[1]};
116
                double z[] = {ptOrig[2]};
117
                int errno = 0;
118
                try {
119
                        if (nadCrsProj != null){
120
                                if (nadInTarget)
121
                                        errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), nadCrsProj);
122
                                else
123
                                        errno = JNIBaseCrs.operate( x , y, z,nadCrsProj,targetCrs.getCrsProj());
124
                                if (errno != -38){
125
                                        double ptDest[] = {x[0], y[0], z[0]};
126
                                        return ptDest;
127
                                }
128
                        }
129
                        
130
                        // Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
131
                         x[0] = ptOrig[0];
132
                         y[0] = ptOrig[1];
133
                         z[0] = ptOrig[2];
134
                         JNIBaseCrs.operate(x, y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj());
135
                         double ptDest[] = {x[0], y[0], z[0]};
136
                         return ptDest;
137
                } catch (OperationCrsException e) {
138
                        throw new CrsException(e);
139
                }
140
        }
141

    
142
        public IProjection getPOrig() {
143
                return null;
144
        }
145

    
146
        public IProjection getPDest() {
147
                return null;
148
        }
149

    
150
        public Point2D convert(Point2D ptOrig, Point2D ptDest) {
151
                try {
152
                        ptDest = operate(ptOrig);
153
                } catch (CrsException e) {
154
                        // TODO LWS implementar una gesti?n completa de los errores.
155
                        NotificationManager.addError(e);
156
                }
157
                return ptDest;
158
        }
159

    
160
        public Rectangle2D convert(Rectangle2D rect) {
161
                Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY());
162
                Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY());
163
                try {
164
                        pt1 = operate(pt1);
165
                        pt2 = operate(pt2);
166
                } catch (CrsException e) {
167
                        // TODO LWS implementar una gesti?n completa de los errores.
168
                        e.printStackTrace();
169
                }
170
                rect = new Rectangle2D.Double();
171
                rect.setFrameFromDiagonal(pt1, pt2);
172
                return rect;
173
        }
174

    
175
        public ICoordTrans getInverted() {
176
                try {
177
                        return new COperation(targetCrs, sourceCrs);
178
                } catch (CrsException e) {
179
                        e.printStackTrace();
180
                }
181
                return null;
182
        }
183
        
184
        public void setNadCrsProj(CrsProj nadCrs, boolean inTarget){
185
                this.nadInTarget = inTarget;
186
                this.nadCrsProj = nadCrs;
187
        }
188
}