Revision 21794 trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/mosaic/process/FeatherProcessBuff.java

View differences:

FeatherProcessBuff.java
80 80
	private FLyrRasterSE inputRasterLayers[] = null;
81 81
	private IBuffer inputBuffers[] = null;
82 82
	private int xResolution, yResolution = 0;
83
	
83

  
84 84
	private FLyrRasterSE outputRasterLayer = null;
85
	
85

  
86 86
	private Line2D borders[][] = null;
87
	
87

  
88 88
	private Rectangle2D inputExtents[] = null;
89 89
	private Rectangle2D intersection = null;
90 90

  
91 91
	private Grid resultGrid = null;
92 92
	private GridExtent resultGridExtent = null;
93 93
	private int resultbandCount = 0;
94
	
94

  
95 95
	private WriterBufferServer writerBufferServer = null;
96 96
	private String fileName = null;
97 97
	int percent = 0;
......
102 102
		inputRasterLayers = (FLyrRasterSE[]) getParam("inputRasterLayers");
103 103

  
104 104
		inputExtents = new Rectangle2D.Double[inputRasterLayers.length];
105
		for (int i = 0; i<inputRasterLayers.length;i++)
105
		for (int i = 0; i < inputRasterLayers.length; i++)
106 106
			inputExtents[i] = inputRasterLayers[i].getFullExtent();
107
		
107

  
108 108
		intersection = new Rectangle2D.Double();
109 109
		Rectangle2D.intersect(inputExtents[0], inputExtents[1], intersection);
110 110

  
......
142 142

  
143 143
		// Construccion de los grid de entrada:
144 144
		IRasterDataSource dsetCopy = null;
145
		
145

  
146 146
		inputBuffers = new IBuffer[inputRasterLayers.length];
147
		int drawableBands[] = {0,1,2};
147
		int drawableBands[] = { 0, 1, 2 };
148 148
		for (int l = 0; l < inputRasterLayers.length; l++) {
149 149
			dsetCopy = inputRasterLayers[l].getDataSource().newDataset();
150 150
			BufferFactory bufferFactory = new BufferFactory(dsetCopy);
151
			bufferFactory.setAdjustToExtent(false);
151 152
			bufferFactory.setDrawableBands(drawableBands);
152
			
153

  
153 154
			try {
154
				if (RasterBuffer.isBufferTooBig(new double[] { resultGridExtent.getMin().getX(), resultGridExtent.getMin().getY(),
155
						resultGridExtent.getMax().getX(), resultGridExtent.getMax().getY() }, drawableBands.length))
155
				if (RasterBuffer.isBufferTooBig(new double[] {
156
						resultGridExtent.getMin().getX(),
157
						resultGridExtent.getMin().getY(),
158
						resultGridExtent.getMax().getX(),
159
						resultGridExtent.getMax().getY() },
160
						drawableBands.length))
156 161
					bufferFactory.setReadOnly(true);
157 162

  
158
				bufferFactory.setAreaOfInterest(resultGridExtent.getMin().getX(),resultGridExtent.getMin().getY(),
159
						resultGridExtent.getMax().getX()-resultGridExtent.getMin().getX(), 
160
						resultGridExtent.getMax().getY()-resultGridExtent.getMin().getY());
163
				bufferFactory.setAreaOfInterest(resultGridExtent.getMin()
164
						.getX(), resultGridExtent.getMax().getY(),
165
						resultGridExtent.getMax().getX()
166
								- resultGridExtent.getMin().getX(),
167
						resultGridExtent.getMax().getY()
168
								- resultGridExtent.getMin().getY());
161 169
			} catch (InvalidSetViewException e) {
162 170
				// TODO Auto-generated catch block
163 171
				e.printStackTrace();
......
169 177

  
170 178
			insertLineLog(RasterToolsUtil.getText(this, "interpolando"));
171 179

  
172
			inputBuffers[l] = ((RasterBuffer) inputBuffers[l]).getAdjustedWindow(xResolution, yResolution, GridInterpolated.INTERPOLATION_NearestNeighbour);
180
			inputBuffers[l] = ((RasterBuffer) inputBuffers[l])
181
					.getAdjustedWindow(xResolution, yResolution,
182
							GridInterpolated.INTERPOLATION_NearestNeighbour);
173 183
		}
174 184

  
175 185
		// Recorrido del grid resultante
176
		//for (int band = 0; band < resultGrid.getBandCount(); band++){
177
			//resultGrid.setBandToOperate(band);
178
			for (int col = 0; col < resultGrid.getNX(); col++){
179
				//percent = col*resultGrid.getBandCount()*100/resultGrid.getNX()*resultGrid.getBandCount();
180
				for (int row = 0; row < resultGrid.getNY(); row++) {
181
					try {
182
						setValue(col, row);
183
						//resultGrid.setCellValue(col, row, getValue(col, row, band));
184
					} catch (OutOfGridException e) {
185
						RasterToolsUtil.messageBoxError(PluginServices.getText(
186
								this, "bad_access_grid"), this, e);
187
					}
186
		// for (int band = 0; band < resultGrid.getBandCount(); band++){
187
		// resultGrid.setBandToOperate(band);
188
		for (int col = 0; col < resultGrid.getNX(); col++) {
189
			// percent =
190
			// col*resultGrid.getBandCount()*100/resultGrid.getNX()*resultGrid.getBandCount();
191
			for (int row = 0; row < resultGrid.getNY(); row++) {
192
				try {
193
					setValue(col, row);
194
					// resultGrid.setCellValue(col, row, getValue(col, row,
195
					// band));
196
				} catch (OutOfGridException e) {
197
					RasterToolsUtil.messageBoxError(PluginServices.getText(
198
							this, "bad_access_grid"), this, e);
188 199
				}
189 200
			}
190
		//}
201
		}
202
		// }
191 203
		createLayer();
192 204
		if (externalActions != null)
193 205
			externalActions.end(outputRasterLayer);
......
227 239
		}
228 240
	}
229 241

  
230
	
231 242
	private void setValue(int col, int row) throws OutOfGridException {
232
		//?Est? el punto en la intersecci?n?
233
		Point2D  worldCoords = rasterToWorld(col, row);
234
		if (intersection.contains(worldCoords)){
243
		// ?Est? el punto en la intersecci?n?
244
		Point2D worldCoords = rasterToWorld(col, row);
245
		if (intersection.contains(worldCoords)) {
235 246
			setFeatherValue(col, row);
236
			/*for (int band = 0; band<resultbandCount; band++){
237
				resultGrid.setBandToOperate(band);
238
				resultGrid.setCellValue(col, row, (byte)255);
239
			}
240
			*/
247
			/*
248
			 * for (int band = 0; band<resultbandCount; band++){
249
			 * resultGrid.setBandToOperate(band); resultGrid.setCellValue(col,
250
			 * row, (byte)255); }
251
			 */
241 252
			return;
242 253
		}
243
	
254

  
244 255
		byte values[] = new byte[resultbandCount];
245 256
		for (int g = 0; g < inputBuffers.length; g++) {
246 257
			inputBuffers[g].getElemByte(row, col, values);
247
			for (int band = 0; band<resultbandCount; band++){
258
			for (int band = 0; band < resultbandCount; band++) {
248 259
				resultGrid.setBandToOperate(band);
249
				if(inputBuffers[g].getNoDataValue()!=values[band]){
260
				if (inputBuffers[g].getNoDataValue() != values[band]) {
250 261
					resultGrid.setCellValue(col, row, values[band]);
262
				} else {
263
					resultGrid.setCellValue(col, row, (byte) resultGrid
264
							.getNoDataValue());
251 265
				}
252
				else{
253
					resultGrid.setCellValue(col, row,(byte) resultGrid.getNoDataValue());
254
				}
255 266
			}
256 267
			return;
257 268
		}
......
259 270

  
260 271
	private void setFeatherValue(int col, int row) throws OutOfGridException {
261 272
		Point2D worldPoint = rasterToWorld(col, row);
262
		
273

  
263 274
		double distances[] = new double[borders.length];
264
		byte values[]= new byte[resultbandCount];
275
		byte values[] = new byte[resultbandCount];
265 276
		double totalD = 0;
266 277
		double result[] = new double[resultbandCount];
267
		
268
		for (int i = 0; i<borders.length;i++){
269
			
270
			//C?lculo de la distancia m?nima a los bordes de cada imagen
271
			distances[i]= Double.POSITIVE_INFINITY;
272
			for(int j = 0; j<borders[0].length;j++)
273
				if (borders[i][j] != null){
278

  
279
		for (int i = 0; i < borders.length; i++) {
280

  
281
			// C?lculo de la distancia m?nima a los bordes de cada imagen
282
			distances[i] = Double.POSITIVE_INFINITY;
283
			for (int j = 0; j < borders[0].length; j++)
284
				if (borders[i][j] != null) {
274 285
					double newDistance = borders[i][j].ptLineDist(worldPoint);
275
					if(distances[i]>newDistance)
276
						distances[i]=newDistance;
286
					if (distances[i] > newDistance)
287
						distances[i] = newDistance;
277 288
				}
278 289
			totalD = totalD + distances[i];
279
			
290

  
280 291
			inputBuffers[i].getElemByte(row, col, values);
281
			for (int band = 0; band<resultbandCount;band++){
282
				result[band] = result[band] + values[band]*distances[i];
292
			for (int band = 0; band < resultbandCount; band++) {
293
				result[band] = result[band] + values[band] * distances[i];
283 294
			}
284 295
		}
285
		
286
		//Escribir el resultado de cada banda.
287
		for (int band = 0; band<resultbandCount;band++){
288
			result[band] = result[band]/totalD;
296

  
297
		// Escribir el resultado de cada banda.
298
		for (int band = 0; band < resultbandCount; band++) {
299
			result[band] = result[band] / totalD;
289 300
			resultGrid.setBandToOperate(band);
290
			resultGrid.setCellValue(col, row, (byte)result[band]);
291
			result[band]=0;
292
		}						
301
			resultGrid.setCellValue(col, row, (byte) result[band]);
302
			result[band] = 0;
303
		}
293 304
	}
294 305

  
295 306
	public int getPercent() {
......
306 317
	 * proceso.
307 318
	 */
308 319
	private void calculateBorders() {
309
		
320

  
310 321
		borders = new Line2D.Double[2][4];
311 322

  
312
		Line2D.Double border = new Line2D.Double(inputExtents[0].getMinX(), inputExtents[0]
313
				.getMaxY(), inputExtents[0].getMaxX(), inputExtents[0].getMaxY());
323
		Line2D.Double border = new Line2D.Double(inputExtents[0].getMinX(),
324
				inputExtents[0].getMaxY(), inputExtents[0].getMaxX(),
325
				inputExtents[0].getMaxY());
314 326
		if (inputExtents[1].intersectsLine(border))
315 327
			borders[0][0] = border;
316 328
		else
317 329
			borders[0][0] = null;
318 330

  
319
		border = new Line2D.Double(inputExtents[0].getMaxX(), inputExtents[0].getMaxY(),
320
				inputExtents[0].getMaxX(), inputExtents[0].getMinY());
331
		border = new Line2D.Double(inputExtents[0].getMaxX(), inputExtents[0]
332
				.getMaxY(), inputExtents[0].getMaxX(), inputExtents[0]
333
				.getMinY());
321 334
		if (inputExtents[1].intersectsLine(border))
322 335
			borders[0][1] = border;
323 336
		else
324 337
			borders[0][1] = null;
325 338

  
326
		border = new Line2D.Double(inputExtents[0].getMaxX(), inputExtents[0].getMinY(),
327
				inputExtents[0].getMinX(), inputExtents[0].getMinY());
339
		border = new Line2D.Double(inputExtents[0].getMaxX(), inputExtents[0]
340
				.getMinY(), inputExtents[0].getMinX(), inputExtents[0]
341
				.getMinY());
328 342
		if (inputExtents[1].intersectsLine(border))
329 343
			borders[0][2] = border;
330 344
		else
331 345
			borders[0][2] = null;
332 346

  
333
		border = new Line2D.Double(inputExtents[0].getMinX(), inputExtents[0].getMinY(),
334
				inputExtents[0].getMinX(), inputExtents[0].getMaxY());
347
		border = new Line2D.Double(inputExtents[0].getMinX(), inputExtents[0]
348
				.getMinY(), inputExtents[0].getMinX(), inputExtents[0]
349
				.getMaxY());
335 350
		if (inputExtents[1].intersectsLine(border))
336 351
			borders[0][3] = border;
337 352
		else
338 353
			borders[0][3] = null;
339 354

  
340
		border = new Line2D.Double(inputExtents[1].getMinX(), inputExtents[1].getMaxY(),
341
				inputExtents[1].getMaxX(), inputExtents[1].getMinY());
355
		border = new Line2D.Double(inputExtents[1].getMinX(), inputExtents[1]
356
				.getMaxY(), inputExtents[1].getMaxX(), inputExtents[1]
357
				.getMinY());
342 358
		if (inputExtents[0].intersectsLine(border))
343 359
			borders[1][0] = border;
344 360
		else
345 361
			borders[1][0] = null;
346 362

  
347
		border = new Line2D.Double(inputExtents[1].getMaxX(), inputExtents[1].getMaxY(),
348
				inputExtents[1].getMaxX(), inputExtents[1].getMinY());
363
		border = new Line2D.Double(inputExtents[1].getMaxX(), inputExtents[1]
364
				.getMaxY(), inputExtents[1].getMaxX(), inputExtents[1]
365
				.getMinY());
349 366
		if (inputExtents[0].intersectsLine(border))
350 367
			borders[1][1] = border;
351 368
		else
352 369
			borders[1][1] = null;
353 370

  
354
		border = new Line2D.Double(inputExtents[1].getMaxX(), inputExtents[1].getMinY(), 
355
				inputExtents[1].getMinX(), inputExtents[1].getMinY());
371
		border = new Line2D.Double(inputExtents[1].getMaxX(), inputExtents[1]
372
				.getMinY(), inputExtents[1].getMinX(), inputExtents[1]
373
				.getMinY());
356 374
		if (inputExtents[0].intersectsLine(border))
357 375
			borders[1][2] = border;
358 376
		else
359 377
			borders[1][2] = null;
360 378

  
361
		border = new Line2D.Double(inputExtents[1].getMinX(), inputExtents[1].getMinY(),
362
				inputExtents[1].getMinX(), inputExtents[1].getMaxY());
379
		border = new Line2D.Double(inputExtents[1].getMinX(), inputExtents[1]
380
				.getMinY(), inputExtents[1].getMinX(), inputExtents[1]
381
				.getMaxY());
363 382
		if (inputExtents[0].intersectsLine(border))
364 383
			borders[1][3] = border;
365 384
		else
366 385
			borders[1][3] = null;
367 386
	}
368
	
369
	private Point2D rasterToWorld (int col, int row){
370
		double worldX = resultGridExtent.getMin().getX()+(col+0.5)*resultGridExtent.getCellSizeX();
371
		double worldY = resultGridExtent.getMax().getY()+(row+0.5)*resultGridExtent.getCellSizeY();
372
		return new Point2D.Double(worldX,worldY);
387

  
388
	private Point2D rasterToWorld(int col, int row) {
389
		double worldX = resultGridExtent.getMin().getX() + (col + 0.5)
390
				* resultGridExtent.getCellSizeX();
391
		double worldY = resultGridExtent.getMax().getY() + (row + 0.5)
392
				* resultGridExtent.getCellSizeY();
393
		return new Point2D.Double(worldX, worldY);
373 394
	}
374 395
}

Also available in: Unified diff