Revision 264 org.gvsig.projection.jcrs/trunk/org.gvsig.projection.jcrs/org.gvsig.projection.jcrs.lib/src/main/java/org/gvsig/crs/COperation.java

View differences:

COperation.java
51 51
import org.slf4j.Logger;
52 52
import org.slf4j.LoggerFactory;
53 53

  
54

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

  
64
//public class COperation implements ICOperation {
63
// public class COperation implements ICOperation {
65 64
public class COperation implements ICoordTrans {
66
	
67
	private static final Logger logger = LoggerFactory.getLogger(COperation.class); 
65

  
66
	private static final Logger logger = LoggerFactory
67
			.getLogger(COperation.class);
68 68
	/**
69 69
	 * CRS Fuente
70 70
	 */
......
74 74
	 */
75 75
	private ICrs targetCrs;
76 76
	/**
77
	 * CRS con los par?metros de transformaci?n (puede representar el
78
	 * CRS fuente o destino, dependiendo de <code>paramsInTarget</code>).
77
	 * CRS con los par?metros de transformaci?n (puede representar el CRS fuente
78
	 * o destino, dependiendo de <code>paramsInTarget</code>).
79 79
	 */
80 80
	private CrsProj paramsCrsProj = null;
81 81
	/**
82
	 * Indica si los par?metros de transformaci?n van asociados al CRS
83
	 * fuente (<code>false</code>) o al destino (<code>true</code>)
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 84
	 */
85 85
	private boolean paramsInTarget;
86
	
86

  
87 87
	/**
88 88
	 * par?metros de transformaci?n para el CRS fuente en el formato proj4
89 89
	 */
90 90
	private String sourceParams = null;
91
	
91

  
92 92
	/**
93 93
	 * par?metros de transformaci?n para el CRS destino en el formato proj4
94 94
	 */
95 95
	private String targetParams = null;
96
	
96

  
97 97
	private CrsProj source = null;
98
	
98

  
99 99
	private CrsProj target = null;
100
	
101
	
100

  
102 101
	/**
103 102
	 * Constructor.
104
	 * 
105
	 * @param from CRS fuente.
106
	 * @param to CRS destino.
103
	 *
104
	 * @param from
105
	 *            CRS fuente.
106
	 * @param to
107
	 *            CRS destino.
107 108
	 * @throws CrsException
108 109
	 */
109 110
	public COperation(ICrs from, ICrs to) throws CrsException {
110
		sourceCrs =  from;
111
		targetCrs =  to;
111
		sourceCrs = from;
112
		targetCrs = to;
112 113
		source = sourceCrs.getCrsProj();
113 114
		target = targetCrs.getCrsProj();
114 115
	}
115
	
116

  
116 117
	/**
117 118
	 * Construcctor.
118
	 * 
119
	 * @param sourceCrs CRS fuente.
120
	 * @param targetCrs CRS destino.
121
	 * @param sourceParams Par?metros de transformaci?n para la fuente (en formato proj4).
122
	 * @param targetParams Par?metros de transformaci?n para el destino (en formato proj4).
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).
123 130
	 * @throws CrsException
124 131
	 */
125
	public COperation(ICrs sourceCrs, ICrs targetCrs, String sourceParams, String targetParams) throws CrsException {
132
	public COperation(ICrs sourceCrs, ICrs targetCrs, String sourceParams,
133
			String targetParams) throws CrsException {
126 134
		this.sourceCrs = sourceCrs;
127 135
		this.targetCrs = targetCrs;
128 136
		this.sourceParams = sourceParams;
129 137
		this.targetParams = targetParams;
130
		
131
		if(sourceParams != null)
132
			source = new CrsProj(sourceCrs.getProj4String()+sourceParams);
138

  
139
		if (sourceParams != null)
140
			source = new CrsProj(sourceCrs.getProj4String() + sourceParams);
133 141
		else
134 142
			source = sourceCrs.getCrsProj();
135
		if(targetParams != null)
136
			target = new CrsProj(targetCrs.getProj4String()+targetParams);
143
		if (targetParams != null)
144
			target = new CrsProj(targetCrs.getProj4String() + targetParams);
137 145
		else
138 146
			target = targetCrs.getCrsProj();
139 147
	}
140 148

  
141 149
	/**
142
	 * Realiza la operaci?n de transformaci?n sobre un punto.
143
	 * Si exiten par?metros de transformaci?n (<code>paramsCrsProj</code>) se utilizan
144
	 * en lugar del CRS fuente o destino, seg?n indique <code>paramsInTarget</code>.
145
	 * @throws OperationCrsException 
146
	 * 
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
	 *
147 157
	 */
148
	private Point2D operate(Point2D pt) throws CrsException, OperationCrsException {
149
		double x[] = {pt.getX()};
150
		double y[] = {pt.getY()};
151
		double z[] = {0D};
158
	private Point2D operate(Point2D pt) throws CrsException,
159
			OperationCrsException {
160
		double x[] = { pt.getX() };
161
		double y[] = { pt.getY() };
162
		double z[] = { 0D };
152 163
		int errno = 0;
153
		
154
			errno = JNIBaseCrs.operate( x , y, z,source,target);
155
			if (errno != -38) // "failed to load NAD27-83 correction file" (pj_strerrno.c)
156
				return new Point2D.Double(x[0],y[0]);
157
			else{
158
				x[0] = pt.getX();
159
				y[0] = pt.getY();
160
				z[0] = 0D;
161
				JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj());
162
				return new Point2D.Double(x[0],y[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
				}
163 185
			}
164
			
165
			/*
166
			if (paramsCrsProj != null){
167
				if (paramsInTarget)
168
					errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), paramsCrsProj);
169
				else
170
					errno = JNIBaseCrs.operate( x , y, z,paramsCrsProj,targetCrs.getCrsProj());
171
				if (errno != -38) // "failed to load NAD27-83 correction file" (pj_strerrno.c)
172
					return new Point2D.Double(x[0],y[0]);
173
			}
174
			
175
			// Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
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 {
176 191
			x[0] = pt.getX();
177 192
			y[0] = pt.getY();
178 193
			z[0] = 0D;
179
			JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj());
180
			return new Point2D.Double(x[0],y[0]);*/
181
	//	System.out.println("x="+x[0]+"y="+y[0]);
182
	//	if(!targetCrs.isProjected())
183
	//		return new Point2D.Double(x[0]*((180)/Math.PI),y[0]*((180)/Math.PI));
184
	//	else
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
185 238
	}
186 239

  
187 240
	/**
188
	 * Realiza la operaci?n de transformaci?n sobre un punto.
189
	 * Si exiten par?metros de transformaci?n (<code>paramsCrsProj</code>) se utilizan
190
	 * en lugar del CRS fuente o destino, seg?n indique <code>paramsInTarget</code>.
191
	 * @throws CrsException 
192
	 * 
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
	 *
193 248
	 */
194
	//TODO Eliminar este m?todo. Me vale con el operate(Point2D).
195
	private double [] operate(double []ptOrig) throws OperationCrsException, CrsException {
196
		double x[] = {ptOrig[0]};
197
		double y[] = {ptOrig[1]};
198
		double z[] = {ptOrig[2]};
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] };
199 255
		int errno = 0;
200 256

  
201
			CrsProj source;
202
			CrsProj target;
203
			if(sourceParams != null)
204
				source = new CrsProj(sourceCrs.getProj4String()+sourceParams);
205
			else
206
				source = sourceCrs.getCrsProj();
207
			if(targetParams != null)
208
				target = new CrsProj(targetCrs.getProj4String()+targetParams);
209
			else
210
				target = targetCrs.getCrsProj();
211
			
212
			errno = JNIBaseCrs.operate( x , y, z,source,target);
213
			if (errno != -38){ // "failed to load NAD27-83 correction file" (pj_strerrno.c)
214
				double ptDest[] = {x[0], y[0], z[0]};
215
				return ptDest;
216
			}
217
			
218
			/*if (paramsCrsProj != null){
219
				if (paramsInTarget)
220
					errno = JNIBaseCrs.operate( x , y, z,sourceCrs.getCrsProj(), paramsCrsProj);
221
				else
222
					errno = JNIBaseCrs.operate( x , y, z,paramsCrsProj,targetCrs.getCrsProj());
223
				if (errno != -38){ // "failed to load NAD27-83 correction file" (pj_strerrno.c)
224
					double ptDest[] = {x[0], y[0], z[0]};
225
					return ptDest;
226
				}
227
			}*/
228
			
229
			// Si el punto estaba fuera del ?mbito del nadgrid operamos sin nadgrid (convertimos)
230
			 x[0] = ptOrig[0];
231
			 y[0] = ptOrig[1];
232
			 z[0] = ptOrig[2];
233
			 JNIBaseCrs.operate(x, y, z,sourceCrs.getCrsProj(), targetCrs.getCrsProj());
234
			 double ptDest[] = {x[0], y[0], z[0]};
235
			 return ptDest;
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;
236 293
	}
237 294

  
238 295
	public IProjection getPOrig() {
239
		return (IProjection)sourceCrs;
296
		return (IProjection) sourceCrs;
240 297
	}
241 298

  
242 299
	public IProjection getPDest() {
243
		return (IProjection)targetCrs;
300
		return (IProjection) targetCrs;
244 301
	}
245 302

  
246
	public Point2D convert(Point2D ptOrig, Point2D ptDest){
303
	public Point2D convert(Point2D ptOrig, Point2D ptDest) {
247 304
		try {
248 305
			ptDest = operate(ptOrig);
249 306
		} catch (CrsException e) {
250
			logger.error("Can't operate over point orig ("+ptOrig.toString()+").",e);
307
			logger.error("Can't operate over point orig (" + ptOrig.toString()
308
					+ ").", e);
251 309
			throw new IllegalStateException(e.getMessage());
252
		}catch (OperationCrsException e) {
253
			logger.error("Can't operate over point orig ("+ptOrig.toString()+").",e);
310
		} catch (OperationCrsException e) {
311
			logger.error("Can't operate over point orig (" + ptOrig.toString()
312
					+ ").", e);
254 313
			throw new IllegalStateException(e.getMessage());
255 314
		}
256 315
		return ptDest;
......
259 318
	public Rectangle2D convert(Rectangle2D rect) {
260 319
		Point2D pt1 = new Point2D.Double(rect.getMinX(), rect.getMinY());
261 320
		Point2D pt2 = new Point2D.Double(rect.getMaxX(), rect.getMaxY());
262
        Point2D pt3 = new Point2D.Double(rect.getMinX(), rect.getMaxY());
263
        Point2D pt4 = new Point2D.Double(rect.getMaxX(), rect.getMinY());
321
		Point2D pt3 = new Point2D.Double(rect.getMinX(), rect.getMaxY());
322
		Point2D pt4 = new Point2D.Double(rect.getMaxX(), rect.getMinY());
264 323
		try {
265 324
			pt1 = operate(pt1);
266
            pt2 = operate(pt2);
267
            pt3 = operate(pt3);
268
            pt4 = operate(pt4);
325
			pt2 = operate(pt2);
326
			pt3 = operate(pt3);
327
			pt4 = operate(pt4);
269 328
		} catch (CrsException e) {
270
			logger.error("Can't operate over rect ("+rect.toString()+").",e);
329
			logger.error("Can't operate over rect (" + rect.toString() + ").",
330
					e);
271 331
			throw new IllegalStateException(e.getMessage());
272
		}catch (OperationCrsException e) {
273
			logger.error("Can't operate over rect ("+rect.toString()+").",e);
332
		} catch (OperationCrsException e) {
333
			logger.error("Can't operate over rect (" + rect.toString() + ").",
334
					e);
274 335
			throw new IllegalStateException(e.getMessage());
275 336
		}
276
		
277
		double min_x = Math.min(
278
		    Math.min(pt1.getX(), pt2.getX()),
279
		    Math.min(pt3.getX(), pt4.getX()));
280
        double min_y = Math.min(
281
            Math.min(pt1.getY(), pt2.getY()),
282
            Math.min(pt3.getY(), pt4.getY()));
283
        double max_x = Math.max(
284
            Math.max(pt1.getX(), pt2.getX()),
285
            Math.max(pt3.getX(), pt4.getX()));
286
        double max_y = Math.max(
287
            Math.max(pt1.getY(), pt2.getY()),
288
            Math.max(pt3.getY(), pt4.getY()));
289
        
290
        return new Rectangle2D.Double(
291
            min_x, min_y, max_x - min_x, max_y - min_y);
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);
292 349
	}
293 350

  
294
	
295 351
	public ICoordTrans getInverted() {
296 352
		COperation operation;
297 353
		try {
298
			operation = new COperation(targetCrs, sourceCrs,targetParams,sourceParams);
354
			operation = new COperation(targetCrs, sourceCrs, targetParams,
355
					sourceParams);
299 356
			return operation;
300 357
		} catch (CrsException e) {
301
			logger.error("Can't get operation.",e);
358
			logger.error("Can't get operation.", e);
302 359
			return null;
303 360
		}
304 361
	}
305
/*	
306
	public void setParamsCrsProj(CrsProj paramsCrs, boolean inTarget){
307
		this.paramsInTarget = inTarget;
308
		this.paramsCrsProj = paramsCrs;
309
	}
310
	*/
362
	/*
363
	 * public void setParamsCrsProj(CrsProj paramsCrs, boolean inTarget){
364
	 * this.paramsInTarget = inTarget; this.paramsCrsProj = paramsCrs; }
365
	 */
311 366
}

Also available in: Unified diff