svn-gvsig-desktop / trunk / libraries / libJCRS / src / org / gvsig / crs / COperation.java @ 8878
History | View | Annotate | Download (5.26 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.CrsProjException; |
50 |
import org.gvsig.crs.proj.JNIBaseCrs; |
51 |
import org.gvsig.crs.proj.OperationCrsException; |
52 |
|
53 |
import com.iver.andami.messages.NotificationManager; |
54 |
|
55 |
/**
|
56 |
* Esta clase sirve para realizar la operaci?n que corresponda una vez
|
57 |
* tenemos dos CRS y tenemos que realizar alguna operaci?n entre ellos
|
58 |
*
|
59 |
* @author Miguel Garc?a Jim?nez (garciajimenez.miguel@gmail.com)
|
60 |
* @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
61 |
*
|
62 |
*/
|
63 |
|
64 |
public class COperation implements ICOperation { |
65 |
private Crs sourceCrs;
|
66 |
private Crs targetCrs;
|
67 |
private CrsProj nadCrsProj = null; |
68 |
private boolean nadInTarget; |
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 |
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 |
} catch (CrsProjException e) {
|
107 |
throw new CrsException(e); |
108 |
} |
109 |
// System.out.println("x="+x[0]+"y="+y[0]);
|
110 |
// if(!targetCrs.isProjected())
|
111 |
// return new Point2D.Double(x[0]*((180)/Math.PI),y[0]*((180)/Math.PI));
|
112 |
// else
|
113 |
} |
114 |
|
115 |
public double [] operate(double []ptOrig) throws CrsException { |
116 |
double x[] = {ptOrig[0]}; |
117 |
double y[] = {ptOrig[1]}; |
118 |
double z[] = {ptOrig[2]}; |
119 |
int errno = 0; |
120 |
try {
|
121 |
if (nadCrsProj != null){ |
122 |
if (nadInTarget)
|
123 |
errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), nadCrsProj); |
124 |
else
|
125 |
errno = JNIBaseCrs.operate( x , y, z,nadCrsProj,targetCrs.getCrsProj()); |
126 |
if (errno != -38){ |
127 |
double ptDest[] = {x[0], y[0], z[0]}; |
128 |
return ptDest;
|
129 |
} |
130 |
} |
131 |
|
132 |
// Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
|
133 |
x[0] = ptOrig[0]; |
134 |
y[0] = ptOrig[1]; |
135 |
z[0] = ptOrig[2]; |
136 |
JNIBaseCrs.operate(x, y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj()); |
137 |
double ptDest[] = {x[0], y[0], z[0]}; |
138 |
return ptDest;
|
139 |
} catch (OperationCrsException e) {
|
140 |
throw new CrsException(e); |
141 |
} catch (CrsProjException e) {
|
142 |
throw new CrsException(e); |
143 |
} |
144 |
} |
145 |
|
146 |
public IProjection getPOrig() {
|
147 |
return null; |
148 |
} |
149 |
|
150 |
public IProjection getPDest() {
|
151 |
return null; |
152 |
} |
153 |
|
154 |
public Point2D convert(Point2D ptOrig, Point2D ptDest) { |
155 |
try {
|
156 |
ptDest = operate(ptOrig); |
157 |
} catch (CrsException e) {
|
158 |
// TODO LWS implementar una gesti?n completa de los errores.
|
159 |
NotificationManager.addError(e); |
160 |
} |
161 |
return ptDest;
|
162 |
} |
163 |
|
164 |
public Rectangle2D convert(Rectangle2D rect) { |
165 |
Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); |
166 |
Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); |
167 |
try {
|
168 |
pt1 = operate(pt1); |
169 |
pt2 = operate(pt2); |
170 |
} catch (CrsException e) {
|
171 |
// TODO LWS implementar una gesti?n completa de los errores.
|
172 |
e.printStackTrace(); |
173 |
} |
174 |
rect = new Rectangle2D.Double(); |
175 |
rect.setFrameFromDiagonal(pt1, pt2); |
176 |
return rect;
|
177 |
} |
178 |
|
179 |
public ICoordTrans getInverted() {
|
180 |
try {
|
181 |
return new COperation(targetCrs, sourceCrs); |
182 |
} catch (CrsException e) {
|
183 |
e.printStackTrace(); |
184 |
} |
185 |
return null; |
186 |
} |
187 |
|
188 |
public void setNadCrsProj(CrsProj nadCrs, boolean inTarget){ |
189 |
this.nadInTarget = inTarget;
|
190 |
this.nadCrsProj = nadCrs;
|
191 |
} |
192 |
} |