root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / COperation.java @ 264
History | View | Annotate | Download (11.2 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 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
/**
|
55 |
* Clase que implementa las operaciones de tranformacion de coordenadas entre
|
56 |
* 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 |
private static final Logger logger = LoggerFactory |
67 |
.getLogger(COperation.class); |
68 |
/**
|
69 |
* CRS Fuente
|
70 |
*/
|
71 |
private ICrs sourceCrs;
|
72 |
/**
|
73 |
* CRS Destino
|
74 |
*/
|
75 |
private ICrs targetCrs;
|
76 |
/**
|
77 |
* CRS con los par?metros de transformaci?n (puede representar el CRS fuente
|
78 |
* o destino, dependiendo de <code>paramsInTarget</code>).
|
79 |
*/
|
80 |
private CrsProj paramsCrsProj = null; |
81 |
/**
|
82 |
* Indica si los par?metros de transformaci?n van asociados al CRS fuente (
|
83 |
* <code>false</code>) o al destino (<code>true</code>)
|
84 |
*/
|
85 |
private boolean paramsInTarget; |
86 |
|
87 |
/**
|
88 |
* par?metros de transformaci?n para el CRS fuente en el formato proj4
|
89 |
*/
|
90 |
private String sourceParams = null; |
91 |
|
92 |
/**
|
93 |
* par?metros de transformaci?n para el CRS destino en el formato proj4
|
94 |
*/
|
95 |
private String targetParams = null; |
96 |
|
97 |
private CrsProj source = null; |
98 |
|
99 |
private CrsProj target = null; |
100 |
|
101 |
/**
|
102 |
* Constructor.
|
103 |
*
|
104 |
* @param from
|
105 |
* CRS fuente.
|
106 |
* @param to
|
107 |
* CRS destino.
|
108 |
* @throws CrsException
|
109 |
*/
|
110 |
public COperation(ICrs from, ICrs to) throws CrsException { |
111 |
sourceCrs = from; |
112 |
targetCrs = to; |
113 |
source = sourceCrs.getCrsProj(); |
114 |
target = targetCrs.getCrsProj(); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Construcctor.
|
119 |
*
|
120 |
* @param sourceCrs
|
121 |
* CRS fuente.
|
122 |
* @param targetCrs
|
123 |
* CRS destino.
|
124 |
* @param sourceParams
|
125 |
* Par?metros de transformaci?n para la fuente (en formato
|
126 |
* proj4).
|
127 |
* @param targetParams
|
128 |
* Par?metros de transformaci?n para el destino (en formato
|
129 |
* proj4).
|
130 |
* @throws CrsException
|
131 |
*/
|
132 |
public COperation(ICrs sourceCrs, ICrs targetCrs, String sourceParams, |
133 |
String targetParams) throws CrsException { |
134 |
this.sourceCrs = sourceCrs;
|
135 |
this.targetCrs = targetCrs;
|
136 |
this.sourceParams = sourceParams;
|
137 |
this.targetParams = targetParams;
|
138 |
|
139 |
if (sourceParams != null) |
140 |
source = new CrsProj(sourceCrs.getProj4String() + sourceParams);
|
141 |
else
|
142 |
source = sourceCrs.getCrsProj(); |
143 |
if (targetParams != null) |
144 |
target = new CrsProj(targetCrs.getProj4String() + targetParams);
|
145 |
else
|
146 |
target = targetCrs.getCrsProj(); |
147 |
} |
148 |
|
149 |
/**
|
150 |
* Realiza la operaci?n de transformaci?n sobre un punto. Si exiten
|
151 |
* par?metros de transformaci?n (<code>paramsCrsProj</code>) se utilizan en
|
152 |
* lugar del CRS fuente o destino, seg?n indique <code>paramsInTarget</code>
|
153 |
* .
|
154 |
*
|
155 |
* @throws OperationCrsException
|
156 |
*
|
157 |
*/
|
158 |
private Point2D operate(Point2D pt) throws CrsException, |
159 |
OperationCrsException { |
160 |
double x[] = { pt.getX() }; |
161 |
double y[] = { pt.getY() }; |
162 |
double z[] = { 0D }; |
163 |
int errno = 0; |
164 |
|
165 |
try {
|
166 |
errno = JNIBaseCrs.operate(x, y, z, source, target); |
167 |
} catch (OperationCrsException e) {
|
168 |
// Parche para arreglar posible error al transformar de latlong a proyectadas cuando la y es +/- 90
|
169 |
if (e.getErrno() == -20) { |
170 |
source.reloadCrs(); |
171 |
target.reloadCrs(); |
172 |
if (!sourceCrs.isProjected() && targetCrs.isProjected()) {
|
173 |
if (Math.abs((pt.getY() - 90.0)) < 0.000000001) { |
174 |
x[0] = pt.getX();
|
175 |
y[0] = 89.99999999; |
176 |
z[0] = 0D; |
177 |
errno = JNIBaseCrs.operate(x, y, z, source, target); |
178 |
} else if (Math.abs(pt.getY() + 90.0) < 0.000000001) { |
179 |
x[0] = pt.getX();
|
180 |
y[0] = -89.99999999; |
181 |
z[0] = 0D; |
182 |
errno = JNIBaseCrs.operate(x, y, z, source, target); |
183 |
} |
184 |
} |
185 |
} |
186 |
} |
187 |
if (errno != -38) // "failed to load NAD27-83 correction file" |
188 |
// (pj_strerrno.c)
|
189 |
return new Point2D.Double(x[0], y[0]); |
190 |
else {
|
191 |
x[0] = pt.getX();
|
192 |
y[0] = pt.getY();
|
193 |
z[0] = 0D; |
194 |
try {
|
195 |
errno = JNIBaseCrs.operate(x, y, z, sourceCrs.getCrsProj(), |
196 |
targetCrs.getCrsProj()); |
197 |
} catch (OperationCrsException e) {
|
198 |
if (e.getErrno() == -20) { |
199 |
if (!sourceCrs.isProjected() && targetCrs.isProjected()) {
|
200 |
if (Math.abs((pt.getY() - 90.0)) < 0.000000001) { |
201 |
x[0] = pt.getX();
|
202 |
y[0] = 89.99999999; |
203 |
z[0] = 0D; |
204 |
errno = JNIBaseCrs.operate(x, y, z, |
205 |
sourceCrs.getCrsProj(), |
206 |
targetCrs.getCrsProj()); |
207 |
} else if (Math.abs(pt.getY() + 90.0) < 0.000000001) { |
208 |
x[0] = pt.getX();
|
209 |
y[0] = -89.99999999; |
210 |
z[0] = 0D; |
211 |
errno = JNIBaseCrs.operate(x, y, z, |
212 |
sourceCrs.getCrsProj(), |
213 |
targetCrs.getCrsProj()); |
214 |
} |
215 |
} |
216 |
} |
217 |
} |
218 |
return new Point2D.Double(x[0], y[0]); |
219 |
} |
220 |
|
221 |
/*
|
222 |
* if (paramsCrsProj != null){ if (paramsInTarget) errno =
|
223 |
* JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), paramsCrsProj);
|
224 |
* else errno = JNIBaseCrs.operate( x , y,
|
225 |
* z,paramsCrsProj,targetCrs.getCrsProj()); if (errno != -38) //
|
226 |
* "failed to load NAD27-83 correction file" (pj_strerrno.c) return new
|
227 |
* Point2D.Double(x[0],y[0]); }
|
228 |
*
|
229 |
* // Si el punto estaba fuera del ?mbito del nadgrid operamos sin
|
230 |
* nadgrid (convertimos) x[0] = pt.getX(); y[0] = pt.getY(); z[0] = 0D;
|
231 |
* JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(),
|
232 |
* targetCrs.getCrsProj()); return new Point2D.Double(x[0],y[0]);
|
233 |
*/
|
234 |
// System.out.println("x="+x[0]+"y="+y[0]);
|
235 |
// if(!targetCrs.isProjected())
|
236 |
// return new Point2D.Double(x[0]*((180)/Math.PI),y[0]*((180)/Math.PI));
|
237 |
// else
|
238 |
} |
239 |
|
240 |
/**
|
241 |
* Realiza la operaci?n de transformaci?n sobre un punto. Si exiten
|
242 |
* par?metros de transformaci?n (<code>paramsCrsProj</code>) se utilizan en
|
243 |
* lugar del CRS fuente o destino, seg?n indique <code>paramsInTarget</code>
|
244 |
* .
|
245 |
*
|
246 |
* @throws CrsException
|
247 |
*
|
248 |
*/
|
249 |
// TODO Eliminar este m?todo. Me vale con el operate(Point2D).
|
250 |
private double[] operate(double[] ptOrig) throws OperationCrsException, |
251 |
CrsException { |
252 |
double x[] = { ptOrig[0] }; |
253 |
double y[] = { ptOrig[1] }; |
254 |
double z[] = { ptOrig[2] }; |
255 |
int errno = 0; |
256 |
|
257 |
CrsProj source; |
258 |
CrsProj target; |
259 |
if (sourceParams != null) |
260 |
source = new CrsProj(sourceCrs.getProj4String() + sourceParams);
|
261 |
else
|
262 |
source = sourceCrs.getCrsProj(); |
263 |
if (targetParams != null) |
264 |
target = new CrsProj(targetCrs.getProj4String() + targetParams);
|
265 |
else
|
266 |
target = targetCrs.getCrsProj(); |
267 |
|
268 |
errno = JNIBaseCrs.operate(x, y, z, source, target); |
269 |
if (errno != -38) { // "failed to load NAD27-83 correction file" |
270 |
// (pj_strerrno.c)
|
271 |
double ptDest[] = { x[0], y[0], z[0] }; |
272 |
return ptDest;
|
273 |
} |
274 |
|
275 |
/*
|
276 |
* if (paramsCrsProj != null){ if (paramsInTarget) errno =
|
277 |
* JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), paramsCrsProj);
|
278 |
* else errno = JNIBaseCrs.operate( x , y,
|
279 |
* z,paramsCrsProj,targetCrs.getCrsProj()); if (errno != -38){ //
|
280 |
* "failed to load NAD27-83 correction file" (pj_strerrno.c) double
|
281 |
* ptDest[] = {x[0], y[0], z[0]}; return ptDest; } }
|
282 |
*/
|
283 |
|
284 |
// Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid
|
285 |
// (convertimos)
|
286 |
x[0] = ptOrig[0]; |
287 |
y[0] = ptOrig[1]; |
288 |
z[0] = ptOrig[2]; |
289 |
JNIBaseCrs.operate(x, y, z, sourceCrs.getCrsProj(), |
290 |
targetCrs.getCrsProj()); |
291 |
double ptDest[] = { x[0], y[0], z[0] }; |
292 |
return ptDest;
|
293 |
} |
294 |
|
295 |
public IProjection getPOrig() {
|
296 |
return (IProjection) sourceCrs;
|
297 |
} |
298 |
|
299 |
public IProjection getPDest() {
|
300 |
return (IProjection) targetCrs;
|
301 |
} |
302 |
|
303 |
public Point2D convert(Point2D ptOrig, Point2D ptDest) { |
304 |
try {
|
305 |
ptDest = operate(ptOrig); |
306 |
} catch (CrsException e) {
|
307 |
logger.error("Can't operate over point orig (" + ptOrig.toString()
|
308 |
+ ").", e);
|
309 |
throw new IllegalStateException(e.getMessage()); |
310 |
} catch (OperationCrsException e) {
|
311 |
logger.error("Can't operate over point orig (" + ptOrig.toString()
|
312 |
+ ").", e);
|
313 |
throw new IllegalStateException(e.getMessage()); |
314 |
} |
315 |
return ptDest;
|
316 |
} |
317 |
|
318 |
public Rectangle2D convert(Rectangle2D rect) { |
319 |
Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); |
320 |
Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); |
321 |
Point2D pt3 = new Point2D.Double(rect.getMinX(), rect.getMaxY()); |
322 |
Point2D pt4 = new Point2D.Double(rect.getMaxX(), rect.getMinY()); |
323 |
try {
|
324 |
pt1 = operate(pt1); |
325 |
pt2 = operate(pt2); |
326 |
pt3 = operate(pt3); |
327 |
pt4 = operate(pt4); |
328 |
} catch (CrsException e) {
|
329 |
logger.error("Can't operate over rect (" + rect.toString() + ").", |
330 |
e); |
331 |
throw new IllegalStateException(e.getMessage()); |
332 |
} catch (OperationCrsException e) {
|
333 |
logger.error("Can't operate over rect (" + rect.toString() + ").", |
334 |
e); |
335 |
throw new IllegalStateException(e.getMessage()); |
336 |
} |
337 |
|
338 |
double min_x = Math.min(Math.min(pt1.getX(), pt2.getX()), |
339 |
Math.min(pt3.getX(), pt4.getX()));
|
340 |
double min_y = Math.min(Math.min(pt1.getY(), pt2.getY()), |
341 |
Math.min(pt3.getY(), pt4.getY()));
|
342 |
double max_x = Math.max(Math.max(pt1.getX(), pt2.getX()), |
343 |
Math.max(pt3.getX(), pt4.getX()));
|
344 |
double max_y = Math.max(Math.max(pt1.getY(), pt2.getY()), |
345 |
Math.max(pt3.getY(), pt4.getY()));
|
346 |
|
347 |
return new Rectangle2D.Double(min_x, min_y, max_x - min_x, max_y |
348 |
- min_y); |
349 |
} |
350 |
|
351 |
public ICoordTrans getInverted() {
|
352 |
COperation operation; |
353 |
try {
|
354 |
operation = new COperation(targetCrs, sourceCrs, targetParams,
|
355 |
sourceParams); |
356 |
return operation;
|
357 |
} catch (CrsException e) {
|
358 |
logger.error("Can't get operation.", e);
|
359 |
return null; |
360 |
} |
361 |
} |
362 |
/*
|
363 |
* public void setParamsCrsProj(CrsProj paramsCrs, boolean inTarget){
|
364 |
* this.paramsInTarget = inTarget; this.paramsCrsProj = paramsCrs; }
|
365 |
*/
|
366 |
} |