root / branches / libProjection_v2_0_prep / libraries / libJCRS / src / org / gvsig / crs / COperation.java @ 27137
History | View | Annotate | Download (8.9 KB)
1 | 27137 | cmartinez | /* 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 tranformacion de
|
||
56 | * coordenadas entre dos 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 | public class COperation implements ICoordTrans { |
||
65 | /**
|
||
66 | * CRS Fuente
|
||
67 | */
|
||
68 | private ICrs sourceCrs;
|
||
69 | /**
|
||
70 | * CRS Destino
|
||
71 | */
|
||
72 | private ICrs targetCrs;
|
||
73 | /**
|
||
74 | * CRS con los par?metros de transformaci?n (puede representar el
|
||
75 | * CRS fuente o destino, dependiendo de <code>paramsInTarget</code>).
|
||
76 | */
|
||
77 | private CrsProj paramsCrsProj = null; |
||
78 | /**
|
||
79 | * Indica si los par?metros de transformaci?n van asociados al CRS
|
||
80 | * fuente (<code>false</code>) o al destino (<code>true</code>)
|
||
81 | */
|
||
82 | private boolean paramsInTarget; |
||
83 | |||
84 | /**
|
||
85 | * par?metros de transformaci?n para el CRS fuente en el formato proj4
|
||
86 | */
|
||
87 | private String sourceParams = null; |
||
88 | |||
89 | /**
|
||
90 | * par?metros de transformaci?n para el CRS destino en el formato proj4
|
||
91 | */
|
||
92 | private String targetParams = null; |
||
93 | |||
94 | private CrsProj source = null; |
||
95 | |||
96 | private CrsProj target = null; |
||
97 | |||
98 | |||
99 | /**
|
||
100 | * Constructor.
|
||
101 | *
|
||
102 | * @param from CRS fuente.
|
||
103 | * @param to CRS destino.
|
||
104 | * @throws CrsException
|
||
105 | */
|
||
106 | public COperation(ICrs from, ICrs to) throws CrsException { |
||
107 | sourceCrs = from; |
||
108 | targetCrs = to; |
||
109 | source = sourceCrs.getCrsProj(); |
||
110 | target = targetCrs.getCrsProj(); |
||
111 | } |
||
112 | |||
113 | /**
|
||
114 | * Construcctor.
|
||
115 | *
|
||
116 | * @param sourceCrs CRS fuente.
|
||
117 | * @param targetCrs CRS destino.
|
||
118 | * @param sourceParams Par?metros de transformaci?n para la fuente (en formato proj4).
|
||
119 | * @param targetParams Par?metros de transformaci?n para el destino (en formato proj4).
|
||
120 | * @throws CrsException
|
||
121 | */
|
||
122 | public COperation(ICrs sourceCrs, ICrs targetCrs, String sourceParams, String targetParams) throws CrsException { |
||
123 | this.sourceCrs = sourceCrs;
|
||
124 | this.targetCrs = targetCrs;
|
||
125 | this.sourceParams = sourceParams;
|
||
126 | this.targetParams = targetParams;
|
||
127 | |||
128 | if(sourceParams != null) |
||
129 | source = new CrsProj(sourceCrs.getProj4String()+sourceParams);
|
||
130 | else
|
||
131 | source = sourceCrs.getCrsProj(); |
||
132 | if(targetParams != null) |
||
133 | target = new CrsProj(targetCrs.getProj4String()+targetParams);
|
||
134 | else
|
||
135 | target = targetCrs.getCrsProj(); |
||
136 | } |
||
137 | |||
138 | /**
|
||
139 | * Realiza la operaci?n de transformaci?n sobre un punto.
|
||
140 | * Si exiten par?metros de transformaci?n (<code>paramsCrsProj</code>) se utilizan
|
||
141 | * en lugar del CRS fuente o destino, seg?n indique <code>paramsInTarget</code>.
|
||
142 | * @throws OperationCrsException
|
||
143 | *
|
||
144 | */
|
||
145 | private Point2D operate(Point2D pt) throws CrsException, OperationCrsException { |
||
146 | double x[] = {pt.getX()}; |
||
147 | double y[] = {pt.getY()}; |
||
148 | double z[] = {0D}; |
||
149 | int errno = 0; |
||
150 | |||
151 | errno = JNIBaseCrs.operate( x , y, z,source,target); |
||
152 | if (errno != -38) // "failed to load NAD27-83 correction file" (pj_strerrno.c) |
||
153 | return new Point2D.Double(x[0],y[0]); |
||
154 | else{
|
||
155 | x[0] = pt.getX();
|
||
156 | y[0] = pt.getY();
|
||
157 | z[0] = 0D; |
||
158 | JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj()); |
||
159 | return new Point2D.Double(x[0],y[0]); |
||
160 | } |
||
161 | |||
162 | /*
|
||
163 | if (paramsCrsProj != null){
|
||
164 | if (paramsInTarget)
|
||
165 | errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), paramsCrsProj);
|
||
166 | else
|
||
167 | errno = JNIBaseCrs.operate( x , y, z,paramsCrsProj,targetCrs.getCrsProj());
|
||
168 | if (errno != -38) // "failed to load NAD27-83 correction file" (pj_strerrno.c)
|
||
169 | return new Point2D.Double(x[0],y[0]);
|
||
170 | }
|
||
171 | |||
172 | // Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
|
||
173 | x[0] = pt.getX();
|
||
174 | y[0] = pt.getY();
|
||
175 | z[0] = 0D;
|
||
176 | JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj());
|
||
177 | return new Point2D.Double(x[0],y[0]);*/
|
||
178 | // System.out.println("x="+x[0]+"y="+y[0]);
|
||
179 | // if(!targetCrs.isProjected())
|
||
180 | // return new Point2D.Double(x[0]*((180)/Math.PI),y[0]*((180)/Math.PI));
|
||
181 | // else
|
||
182 | } |
||
183 | |||
184 | /**
|
||
185 | * Realiza la operaci?n de transformaci?n sobre un punto.
|
||
186 | * Si exiten par?metros de transformaci?n (<code>paramsCrsProj</code>) se utilizan
|
||
187 | * en lugar del CRS fuente o destino, seg?n indique <code>paramsInTarget</code>.
|
||
188 | * @throws CrsException
|
||
189 | *
|
||
190 | */
|
||
191 | //TODO Eliminar este m?todo. Me vale con el operate(Point2D).
|
||
192 | private double [] operate(double []ptOrig) throws OperationCrsException, CrsException { |
||
193 | double x[] = {ptOrig[0]}; |
||
194 | double y[] = {ptOrig[1]}; |
||
195 | double z[] = {ptOrig[2]}; |
||
196 | int errno = 0; |
||
197 | |||
198 | CrsProj source; |
||
199 | CrsProj target; |
||
200 | if(sourceParams != null) |
||
201 | source = new CrsProj(sourceCrs.getProj4String()+sourceParams);
|
||
202 | else
|
||
203 | source = sourceCrs.getCrsProj(); |
||
204 | if(targetParams != null) |
||
205 | target = new CrsProj(targetCrs.getProj4String()+targetParams);
|
||
206 | else
|
||
207 | target = targetCrs.getCrsProj(); |
||
208 | |||
209 | errno = JNIBaseCrs.operate( x , y, z,source,target); |
||
210 | if (errno != -38){ // "failed to load NAD27-83 correction file" (pj_strerrno.c) |
||
211 | double ptDest[] = {x[0], y[0], z[0]}; |
||
212 | return ptDest;
|
||
213 | } |
||
214 | |||
215 | /*if (paramsCrsProj != null){
|
||
216 | if (paramsInTarget)
|
||
217 | errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), paramsCrsProj);
|
||
218 | else
|
||
219 | errno = JNIBaseCrs.operate( x , y, z,paramsCrsProj,targetCrs.getCrsProj());
|
||
220 | if (errno != -38){ // "failed to load NAD27-83 correction file" (pj_strerrno.c)
|
||
221 | double ptDest[] = {x[0], y[0], z[0]};
|
||
222 | return ptDest;
|
||
223 | }
|
||
224 | }*/
|
||
225 | |||
226 | // Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
|
||
227 | x[0] = ptOrig[0]; |
||
228 | y[0] = ptOrig[1]; |
||
229 | z[0] = ptOrig[2]; |
||
230 | JNIBaseCrs.operate(x, y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj()); |
||
231 | double ptDest[] = {x[0], y[0], z[0]}; |
||
232 | return ptDest;
|
||
233 | } |
||
234 | |||
235 | public IProjection getPOrig() {
|
||
236 | return (IProjection)sourceCrs;
|
||
237 | } |
||
238 | |||
239 | public IProjection getPDest() {
|
||
240 | return (IProjection)targetCrs;
|
||
241 | } |
||
242 | |||
243 | public Point2D convert(Point2D ptOrig, Point2D ptDest){ |
||
244 | try {
|
||
245 | ptDest = operate(ptOrig); |
||
246 | } catch (CrsException e) {
|
||
247 | // TODO LWS implementar una gesti?n completa de los errores.
|
||
248 | //NotificationManager.addError(e);
|
||
249 | e.printStackTrace(); |
||
250 | throw new IllegalStateException(e.getMessage()); |
||
251 | }catch (OperationCrsException e) {
|
||
252 | // TODO LWS implementar una gesti?n completa de los errores.
|
||
253 | //NotificationManager.addError(e);
|
||
254 | e.printStackTrace(); |
||
255 | throw new IllegalStateException(e.getMessage()); |
||
256 | } |
||
257 | return ptDest;
|
||
258 | } |
||
259 | |||
260 | public Rectangle2D convert(Rectangle2D rect) { |
||
261 | Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); |
||
262 | Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); |
||
263 | try {
|
||
264 | pt1 = operate(pt1); |
||
265 | pt2 = operate(pt2); |
||
266 | } catch (CrsException e) {
|
||
267 | // TODO LWS implementar una gesti?n completa de los errores.
|
||
268 | //NotificationManager.addError(e);
|
||
269 | e.printStackTrace(); |
||
270 | throw new IllegalStateException(e.getMessage()); |
||
271 | }catch (OperationCrsException e) {
|
||
272 | // TODO LWS implementar una gesti?n completa de los errores.
|
||
273 | //NotificationManager.addError(e);
|
||
274 | e.printStackTrace(); |
||
275 | throw new IllegalStateException(e.getMessage()); |
||
276 | } |
||
277 | rect = new Rectangle2D.Double(); |
||
278 | rect.setFrameFromDiagonal(pt1, pt2); |
||
279 | return rect;
|
||
280 | } |
||
281 | |||
282 | |||
283 | public ICoordTrans getInverted() {
|
||
284 | COperation operation; |
||
285 | try {
|
||
286 | operation = new COperation(targetCrs, sourceCrs,targetParams,sourceParams);
|
||
287 | return operation;
|
||
288 | } catch (CrsException e) {
|
||
289 | // TODO Auto-generated catch block
|
||
290 | e.printStackTrace(); |
||
291 | return null; |
||
292 | } |
||
293 | } |
||
294 | /*
|
||
295 | public void setParamsCrsProj(CrsProj paramsCrs, boolean inTarget){
|
||
296 | this.paramsInTarget = inTarget;
|
||
297 | this.paramsCrsProj = paramsCrs;
|
||
298 | }
|
||
299 | */
|
||
300 | } |