Statistics
| Revision:

root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / mosaic / process / MosaicProcess.java @ 27740

History | View | Annotate | Download (22.2 KB)

1 20729 amunoz
/* 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 Iba?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.remotesensing.mosaic.process;
42
43
import java.awt.geom.AffineTransform;
44 26322 amunoz
import java.awt.geom.Point2D;
45 25969 dguerrero
import java.awt.image.DataBuffer;
46 26076 amunoz
import java.io.File;
47 20729 amunoz
import java.io.IOException;
48
49
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
50
import org.gvsig.raster.RasterProcess;
51
import org.gvsig.raster.buffer.BufferFactory;
52 25520 dguerrero
import org.gvsig.raster.buffer.BufferInterpolation;
53 20729 amunoz
import org.gvsig.raster.buffer.RasterBuffer;
54
import org.gvsig.raster.buffer.RasterBufferInvalidException;
55
import org.gvsig.raster.buffer.WriterBufferServer;
56 25969 dguerrero
import org.gvsig.raster.dataset.FileNotOpenException;
57 20729 amunoz
import org.gvsig.raster.dataset.GeoRasterWriter;
58
import org.gvsig.raster.dataset.IBuffer;
59
import org.gvsig.raster.dataset.IRasterDataSource;
60
import org.gvsig.raster.dataset.InvalidSetViewException;
61
import org.gvsig.raster.dataset.NotSupportedExtensionException;
62
import org.gvsig.raster.dataset.io.RasterDriverException;
63
import org.gvsig.raster.grid.Grid;
64 26322 amunoz
import org.gvsig.raster.grid.GridCell;
65 20729 amunoz
import org.gvsig.raster.grid.GridExtent;
66
import org.gvsig.raster.grid.OutOfGridException;
67 25969 dguerrero
import org.gvsig.raster.grid.filter.RasterFilter;
68
import org.gvsig.raster.grid.filter.enhancement.EnhancementStretchListManager;
69
import org.gvsig.raster.grid.filter.enhancement.LinearStretchParams;
70 20729 amunoz
import org.gvsig.raster.util.RasterToolsUtil;
71 25969 dguerrero
import org.gvsig.remotesensing.RemoteSensingUtils;
72 20729 amunoz
73
import com.iver.andami.PluginServices;
74 22548 dguerrero
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
75 20729 amunoz
76
/**
77 22800 dguerrero
* Clase que implementa el proceso de construccion de un mosaico mediante los m?todos b?sicos.
78 20729 amunoz
*
79 22800 dguerrero
* @params
80
* <LI>FLyrRasterSE[] "inputRasterLayers": Capas raster de entrada</LI>
81
* <LI>int "methodCode": M?todo de construcci?n (0:Valor m?ximo, 1:Valor m?nimo, 2: Valor Medio,
82
* 3: Valor del pixel de la capa superior, 4:Valor del pixel de la capa inferior)</LI>
83
* <LI>String "outputPath": Ruta completa al fichero de salida del proceso</LI>
84
*
85
* @result
86
* <LI>outputRassterLayers[]: Capas raster resultantes</LI>
87
*
88
*
89 20729 amunoz
* @author aMu?oz (alejandro.mu?oz@uclm.es)
90
* @version 30/4/2008
91
* */
92
93 26975 amunoz
/**
94
 * @author root
95
 *
96
 */
97 20729 amunoz
public class MosaicProcess extends RasterProcess {
98 22800 dguerrero
99
        public static final int MAX                 = 0;
100
        public static final int MIN                 = 1;
101
        public static final int AVERAGE         = 2;
102
        public static final int FRONT                 = 3;
103
        public static final int BACK                 = 4;
104 20729 amunoz
105
        // Layers que intervienen en el proceso
106 22548 dguerrero
        private FLyrRasterSE inputRasterLayers[] = null;
107
108
        //Layer de salida
109
        private FLyrRasterSE outputRasterLayer = null;
110 20729 amunoz
111
        // Extend completo del mosaico
112
        private GridExtent fullExtend= null;
113
114
        // Grid resultante
115
        Grid mosaicGrid = null;
116
117
        // Buffers con las imagenes
118 25969 dguerrero
        IBuffer buffers[]= null;
119 20729 amunoz
120 20733 amunoz
        // Codigo operacion mayor, menor,media valor de la situada encima.
121
        int codOp= 0;
122 20729 amunoz
123
        // indicador de proceso
124 20829 amunoz
        int percent=0, proceso=0;
125 20733 amunoz
126
        // writer para escritura en fichero
127 20751 amunoz
        private WriterBufferServer writerBufferServer =null;
128
129 22800 dguerrero
        //  Numero de bandas 3 o 1 dependiendo de si es RGB o Nivel de gris
130
        int resultbandCount=0;
131 20733 amunoz
132 20795 amunoz
        // Fichero de salida
133
        private String fileName=null;
134 20751 amunoz
135 26975 amunoz
        private int lastY=-1;
136 20795 amunoz
137 26322 amunoz
        private Grid layersGrid[]= null;
138 20795 amunoz
139 26975 amunoz
        private GridExtent extendS[] = null;
140
141
        private GridCell cell = null;
142
143 20729 amunoz
        /** Inicializaci?n de los par?metros
144
         *  layers -  FLayers con los layers seleccionados para el mosaico.
145
         *
146
         *         En la inicializacion se calcula el grid resultante.
147
         * */
148
        public void init() {
149 26975 amunoz
150 22566 dguerrero
                inputRasterLayers= (FLyrRasterSE[])getParam("inputRasterLayers");
151 22800 dguerrero
                codOp= getIntParam("methodCode");
152
                resultbandCount= getIntParam("numbands");
153
                fileName = getStringParam("outputPath");
154 26975 amunoz
155
                IRasterDataSource dsetCopy = null;
156
                buffers= new RasterBuffer[inputRasterLayers.length];
157
                extendS=  new GridExtent[inputRasterLayers.length];
158 20795 amunoz
159 20751 amunoz
//                 Calculo del extend resultante
160 22548 dguerrero
                fullExtend= calculateExtend(inputRasterLayers);
161 26975 amunoz
                extendS=new GridExtent[inputRasterLayers.length];
162 20729 amunoz
163 26975 amunoz
                for(int i=0; i< inputRasterLayers.length;i++)
164
                {
165
                        dsetCopy = ((FLyrRasterSE)inputRasterLayers[i]).getDataSource().newDataset();
166
                        BufferFactory bufferFactory = new BufferFactory(dsetCopy);
167
                        bufferFactory.setAdjustToExtent(false);
168
                        bufferFactory.setDrawableBands(inputRasterLayers[i].getRenderBands());
169
                        if (!RasterBuffer.loadInMemory(dsetCopy))
170
                                bufferFactory.setReadOnly(true);
171
172
                        try {
173
                                extendS[i]= new GridExtent(inputRasterLayers[i].getFullExtent().getMinX(),
174
                                                inputRasterLayers[i].getFullExtent().getMinY(),
175
                                                inputRasterLayers[i].getFullExtent().getMaxX(),
176
                                                inputRasterLayers[i].getFullExtent().getMaxY(),
177
                                                inputRasterLayers[i].getAffineTransform().getScaleX(),
178
                                                inputRasterLayers[i].getAffineTransform().getScaleY());
179
180
                                bufferFactory.setAreaOfInterest(extendS[i].getMin().getX(), extendS[i].getMax().getY(),
181
                                                extendS[i].getMax().getX(), extendS[i].getMin().getY(), extendS[i].getNX(),extendS[i].getNY());
182
                                buffers[i]= bufferFactory.getRasterBuf();
183
                                buffers[i]= ((RasterBuffer)buffers[i]).getAdjustedWindow(extendS[i].getNX(), extendS[i].getNY(), BufferInterpolation.INTERPOLATION_Bilinear);
184
185
                                //        Aplicar filtro de realce si es necesario:
186
                                if(buffers[i].getDataType()!=DataBuffer.TYPE_BYTE){
187
                                        LinearStretchParams leParams = null;
188
                                        leParams = LinearStretchParams.createStandardParam(inputRasterLayers[i].getRenderBands(), 0.0, bufferFactory.getDataSource().getStatistics(), false);
189
190
                                        RasterFilter linearStretchEnhancementFilter = EnhancementStretchListManager.createEnhancedFilter(leParams, bufferFactory.getDataSource().getStatistics(),
191
                                                inputRasterLayers[i].getRenderBands(), false);
192
                                        linearStretchEnhancementFilter.addParam("raster", buffers[i]);
193
                                        linearStretchEnhancementFilter.execute();
194
                                        buffers[i] = (IBuffer)linearStretchEnhancementFilter.getResult("raster");
195
                                }
196
197
                        } catch (FileNotOpenException e) {
198
                                return;
199
                        } catch (RasterDriverException e) {
200
                                RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
201
                        } catch (InterruptedException e) {
202
                                RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
203
                        } catch (InvalidSetViewException e) {
204
                                e.printStackTrace();
205
                        }
206
                }
207 20729 amunoz
                try {
208 22800 dguerrero
                                mosaicGrid= new Grid(fullExtend,fullExtend,IBuffer.TYPE_BYTE,new int[] { 0, 1, 2 });
209
                                resultbandCount = mosaicGrid.getBandCount();
210 20729 amunoz
                } catch (RasterBufferInvalidException e) {
211
                        RasterToolsUtil.messageBoxError("buffer_incorrecto", this, e);
212
                }
213
        }
214
215
216
        /**
217
         *  Proceso
218
         * */
219
        public void process() throws InterruptedException {
220
221 20733 amunoz
//                 Construccion del mosaico: Operaci?n M?ximo
222 26322 amunoz
                if(codOp==MAX){
223 20750 amunoz
                        int progress = 0;
224 26975 amunoz
                        for(int i=0;i<buffers.length;i++){
225
                                for(int band=0; band<resultbandCount; band++){
226
                                        mosaicGrid.setBandToOperate(band);
227
                                        for(int row=0; row<buffers[i].getHeight(); row++){
228
                                                progress++;
229
                                                calculateCoords(-1,row,i);
230
                                                for(int col=0; col<buffers[i].getWidth();col++){
231
                                                                setValueMax(col,row,band,i);
232
                                                }
233
                                                percent=(int)( progress*100/((mosaicGrid.getLayerNY()*resultbandCount)))/buffers.length;
234 20750 amunoz
                                        }
235 20733 amunoz
                                }
236
                        }
237
                }
238 20729 amunoz
239 20751 amunoz
//                Construccion del mosaico: Operaci?n M?nimo
240 26322 amunoz
                if(codOp==MIN){
241 20762 amunoz
                        int progress = 0;
242 26975 amunoz
                        for(int i=0;i<buffers.length;i++){
243
                                for(int band=0; band<resultbandCount; band++){
244
                                        mosaicGrid.setBandToOperate(band);
245
                                        for(int row=0; row<buffers[i].getHeight(); row++){
246
                                                progress++;
247
                                                calculateCoords(-1,row,i);
248
                                                for(int col=0; col<buffers[i].getWidth();col++){
249
                                                                setValueMin(col,row,band,i);
250
                                                }
251
                                                percent=(int)( progress*100/((mosaicGrid.getLayerNY()*resultbandCount)))/buffers.length;
252 20762 amunoz
                                        }
253 20733 amunoz
                                }
254
                        }
255
                }
256 20729 amunoz
257 20750 amunoz
//                 Construccion del mosaico: Operaci?n Media
258 26322 amunoz
                if(codOp==AVERAGE){
259 20750 amunoz
                        int progress = 0;
260 26975 amunoz
                        for(int i=0;i<buffers.length;i++){
261
                                for(int band=0; band<resultbandCount; band++){
262
                                        mosaicGrid.setBandToOperate(band);
263
                                        for(int row=0; row<buffers[i].getHeight(); row++){
264
                                                progress++;
265
                                                calculateCoords(-1,row,i);
266
                                                for(int col=0; col<buffers[i].getWidth();col++){
267
                                                                setValueMean(col,row,band,i);
268
                                                }
269
                                                percent=(int)( progress*100/((mosaicGrid.getLayerNY()*resultbandCount)))/buffers.length;
270 20750 amunoz
                                        }
271 20733 amunoz
                                }
272
                        }
273
                }
274 20729 amunoz
275 20787 amunoz
//                Construccion del mosaico: Operacion valor de capa de delantera
276 26322 amunoz
                if(codOp==FRONT){
277 20780 amunoz
                        int progress = 0;
278 26975 amunoz
                        for(int i=0;i<buffers.length;i++){
279
                                for(int band=0; band<resultbandCount; band++){
280
                                        mosaicGrid.setBandToOperate(band);
281
                                        for(int row=0; row<buffers[i].getHeight(); row++){
282
                                                progress++;
283
                                                calculateCoords(-1,row,i);
284
                                                for(int col=0; col<buffers[i].getWidth();col++){
285
                                                                setValueFront(col,row,band,i);
286
                                                }
287
                                                percent=(int)( progress*100/((mosaicGrid.getLayerNY()*resultbandCount)))/buffers.length;
288 20780 amunoz
                                        }
289 20733 amunoz
                                }
290 20729 amunoz
                        }
291
                }
292
293 20787 amunoz
294
//                Construccion del mosaico: Operaci?n Valor de capa de trasera
295 26322 amunoz
                if(codOp==BACK){
296 20787 amunoz
                        int progress = 0;
297 26975 amunoz
                        for(int i=0;i<buffers.length;i++){
298
                                for(int band=0; band<resultbandCount; band++){
299
                                        mosaicGrid.setBandToOperate(band);
300
                                        for(int row=0; row<buffers[i].getHeight(); row++){
301
                                                progress++;
302
                                                calculateCoords(-1,row,i);
303
                                                for(int col=0; col<buffers[i].getWidth();col++){
304
                                                                setValueBack(col,row,band,i);
305
                                                }
306
                                                percent=(int)( progress*100/((mosaicGrid.getLayerNY()*resultbandCount)))/buffers.length;
307 20787 amunoz
                                        }
308
                                }
309
                        }
310
                }
311
312 20733 amunoz
                // Escritura en fichero
313 20829 amunoz
                proceso=2;
314 22548 dguerrero
                createLayer();
315
                if (externalActions != null)
316
                        externalActions.end(outputRasterLayer);
317 20729 amunoz
        }
318
319
320
        /**
321 20733 amunoz
         *  M?todo que establece para la coordenada x,y el valor m?ximo
322
         *  de todos los valores para ese p?xel en cualquiera de las imagenes.
323
         *  @param cordenada x
324
         *  @param coordenada y
325 27361 nbrodin
         * @throws InterruptedException
326 20733 amunoz
         * */
327 27361 nbrodin
        public void setValueMax(int x, int y, int band,int layer) throws InterruptedException{
328 26975 amunoz
329
                byte result=Byte.MIN_VALUE;
330
                byte data=Byte.MIN_VALUE;
331
                Point2D worldPoint=(Point2D) extendS[layer].getWorldCoordsFromGridCoords(new GridCell(x,y,(double)0));;
332 26322 amunoz
                int numLayers= buffers.length;
333 26975 amunoz
334 26322 amunoz
                for(int i=0; i<numLayers;i++){
335 26975 amunoz
                        if(extendS[i].isAt(worldPoint))
336
                        {
337
                                GridCell cell=extendS[i].getGridCoordsFromWorldCoords(worldPoint);
338
                                if(!(cell.getX()>=buffers[i].getWidth()||cell.getY()>=buffers[i].getHeight()))
339
                                        data = (byte)RemoteSensingUtils.getCellValueInLayerCoords(buffers[i], cell.getX(), cell.getY(), band);
340 26322 amunoz
                                result=(byte) Math.max((byte)result,(byte)data);
341
                        }
342 20750 amunoz
                }
343
                try {
344 26975 amunoz
                        cell.setX(cell.getX()+1);
345
                        mosaicGrid.setCellValue(cell.getX(),cell.getY(),(byte)result);
346 20750 amunoz
                } catch (OutOfGridException e) {
347 26975 amunoz
                                //e.printStackTrace();
348 20733 amunoz
                }
349
        }
350
351 26975 amunoz
352 20733 amunoz
        /**
353
         *  M?todo que establece para la coordenada x,y el valor m?ximo
354
         *  de todos los valores para ese p?xel en cualquiera de las imagenes.
355
         *  @param cordenada x
356
         *  @param coordenada y
357 27361 nbrodin
         * @throws InterruptedException
358 20733 amunoz
         * */
359 27361 nbrodin
        public void setValueMin(int x, int y,int band,int layer) throws InterruptedException{
360 26975 amunoz
                byte result=Byte.MAX_VALUE;
361
                byte data=Byte.MAX_VALUE;
362
                Point2D worldPoint=(Point2D) extendS[layer].getWorldCoordsFromGridCoords(new GridCell(x,y,(double)0));;
363 26322 amunoz
                int numLayers= buffers.length;
364 26975 amunoz
365 26322 amunoz
                for(int i=0; i<numLayers;i++){
366 26975 amunoz
                        if(extendS[i].isAt(worldPoint))
367
                        {
368
                                GridCell cell=extendS[i].getGridCoordsFromWorldCoords(worldPoint);
369
                                if(!(cell.getX()>=buffers[i].getWidth()||cell.getY()>=buffers[i].getHeight()))
370
                                        data = (byte)RemoteSensingUtils.getCellValueInLayerCoords(buffers[i], cell.getX(), cell.getY(), band);
371
                                result=(byte) Math.min((byte)result,(byte)data);
372
                        }
373 26322 amunoz
                }
374
                try {
375 26975 amunoz
                        cell.setX(cell.getX()+1);
376
                        mosaicGrid.setCellValue(cell.getX(),cell.getY(),(byte)result);
377 26322 amunoz
                } catch (OutOfGridException e) {
378 26975 amunoz
                                //e.printStackTrace();
379 26322 amunoz
                }
380 20733 amunoz
        }
381
382
383 20750 amunoz
        /**
384
         *  M?todo que establece para la coordenada x,y el valor medio
385
         *  de todos los valores para ese p?xel en cualquiera de las imagenes.
386
         *  Si el valor en cualquiera de las imagenes es noData no es tenido en cuenta
387
         *  @param cordenada x
388
         *  @param coordenada y
389 27361 nbrodin
         * @throws InterruptedException
390 20750 amunoz
         * */
391 27361 nbrodin
        public void setValueMean(int x, int y, int band,int layer) throws InterruptedException{
392 26975 amunoz
393
                double result=0; int buffTotales=0;
394
                Point2D worldPoint=(Point2D) extendS[layer].getWorldCoordsFromGridCoords(new GridCell(x,y,(double)0));;
395 26322 amunoz
                int numLayers= buffers.length;
396 26975 amunoz
397 26322 amunoz
                for(int i=0; i<numLayers;i++){
398 26975 amunoz
                        if(extendS[i].isAt(worldPoint))
399
                        {
400
                                GridCell cell=extendS[i].getGridCoordsFromWorldCoords(worldPoint);
401
                                if(!(cell.getX()>=buffers[i].getWidth()||cell.getY()>=buffers[i].getHeight()))
402
                                        result+= RemoteSensingUtils.getCellValueInLayerCoords(buffers[i], cell.getX(), cell.getY(), band);
403 26322 amunoz
                                buffTotales++;
404 20750 amunoz
                        }
405
                }
406
                try {
407 26975 amunoz
                        if(buffTotales==0)
408
                                buffTotales=1;
409
                        result=result/(buffTotales);
410
                        cell.setX(cell.getX()+1);
411
                        mosaicGrid.setCellValue(cell.getX(),cell.getY(),(byte)result);
412 20750 amunoz
                } catch (OutOfGridException e) {
413 26975 amunoz
                                //e.printStackTrace();
414 20750 amunoz
                }
415 26975 amunoz
416 20750 amunoz
        }
417 20733 amunoz
418 20750 amunoz
419 20780 amunoz
        /**
420
         *  M?todo que establece para la coordenada x,y el valor de la capa superior
421
         *  en caso de solape. Se parte de un array de buffer ordenados, de tal manera que
422
         *  el primer elemento corresponde a la capa situada mas al frente. El ?ltimo por
423
         *  contra es el situado al fondo.
424

425
         *  @param cordenada x
426
         *  @param coordenada y
427 27361 nbrodin
         * @throws InterruptedException
428 20780 amunoz
         * */
429 27361 nbrodin
        public void setValueFront(int x, int y, int band,int layer) throws InterruptedException{
430 26975 amunoz
                Point2D worldPoint=(Point2D) extendS[layer].getWorldCoordsFromGridCoords(new GridCell(x,y,(double)0));;
431 26322 amunoz
                int numLayers= buffers.length;
432 26975 amunoz
            byte result=0;
433 26322 amunoz
                for(int i=0; i<numLayers;i++){
434 26975 amunoz
                        if(extendS[i].isAt(worldPoint))
435
                        {
436
                                GridCell cell=extendS[i].getGridCoordsFromWorldCoords(worldPoint);
437
                                if(!(cell.getX()>=buffers[i].getWidth()||cell.getY()>=buffers[i].getHeight()))
438
                                        result = (byte)RemoteSensingUtils.getCellValueInLayerCoords(buffers[i], cell.getX(), cell.getY(), band);
439
                                    break;
440
                        }
441 20780 amunoz
                }
442
                try {
443 26975 amunoz
                        cell.setX(cell.getX()+1);
444
                        mosaicGrid.setCellValue(cell.getX(),cell.getY(),(byte)result);
445 20780 amunoz
                } catch (OutOfGridException e) {
446 26975 amunoz
                                //e.printStackTrace();
447 20780 amunoz
                }
448
        }
449
450
451 20787 amunoz
        /**
452
         *  M?todo que establece para la coordenada x,y el valor de la capa inferior
453
         *  en caso de solape. Se parte de un array de buffer ordenados, de tal manera que
454
         *  el primer elemento corresponde a la capa situada mas al frente. El ?ltimo por
455
         *  contra es el situado al fondo.
456

457
         *  @param cordenada x
458
         *  @param coordenada y
459 27361 nbrodin
         * @throws InterruptedException
460 20787 amunoz
         * */
461 27361 nbrodin
        public void setValueBack(int x, int y, int band,int layer) throws InterruptedException{
462 26975 amunoz
                Point2D worldPoint=(Point2D) extendS[layer].getWorldCoordsFromGridCoords(new GridCell(x,y,(double)0));;
463 26322 amunoz
                int numLayers= buffers.length;
464 26975 amunoz
            byte result=0;
465 26322 amunoz
                for(int i=0; i<numLayers;i++){
466 26975 amunoz
                        if(extendS[i].isAt(worldPoint))
467
                        {
468
                                GridCell cell=extendS[i].getGridCoordsFromWorldCoords(worldPoint);
469
                                if(!(cell.getX()>=buffers[i].getWidth()||cell.getY()>=buffers[i].getHeight()))
470
                                        result = (byte)RemoteSensingUtils.getCellValueInLayerCoords(buffers[i], cell.getX(), cell.getY(), band);
471
                        }
472 20787 amunoz
                }
473
                try {
474 26975 amunoz
                        cell.setX(cell.getX()+1);
475
                        mosaicGrid.setCellValue(cell.getX(),cell.getY(),(byte)result);
476 20787 amunoz
                } catch (OutOfGridException e) {
477 26975 amunoz
                                //e.printStackTrace();
478 20787 amunoz
                }
479
        }
480 20780 amunoz
481 26975 amunoz
        /**
482
         *  M?todo que calcula la cordenadas iniciales de una fila
483
         *  @param cordenada x
484
         *  @param coordenada y
485
         *  @param layer
486
         * */
487
        private void calculateCoords(int x, int y, int layer){
488
489
                Point2D worldPoint=(Point2D) extendS[layer].getWorldCoordsFromGridCoords(new GridCell(x,y,(double)0));;
490
                // Se obtiene la coordenada correspondiente a la fila
491
                cell=mosaicGrid.getGridExtent().getGridCoordsFromWorldCoords(worldPoint);
492
                if(lastY==cell.getY())
493
                        cell.setY(cell.getY()+1);
494
                lastY= cell.getY();
495
        }
496 20787 amunoz
497 26975 amunoz
498
499 20733 amunoz
        /**
500 20729 amunoz
         * M?todo que calcula el extend resultante para la operaci?n de mosaico
501
         *
502
         * @param layers que intervienen en la operacion.
503
         * @return GridExtend del mosaico
504
         * */
505
506 22548 dguerrero
        private GridExtent calculateExtend (FLyrRasterSE layers[]){
507 20829 amunoz
508 20729 amunoz
                GridExtent result= null;
509 20829 amunoz
                IRasterDataSource dsetCopy = null;
510 26975 amunoz
                double cellSizeX=java.lang.Double.MAX_VALUE;
511
                double cellSizeY=java.lang.Double.MAX_VALUE;
512 20829 amunoz
                double minX=0,maxX=0,minY=0,maxY=0;
513 27740 nbrodin
514 25520 dguerrero
                // Se obtiene el menor tama?o de celda mayor
515 27740 nbrodin
516
                for(int i=0; i< layers.length;i++) {
517
                        double csX = ((FLyrRasterSE)layers[i]).getCellSize();
518
                        double csY = ((FLyrRasterSE)layers[i]).getWCHeight() / ((FLyrRasterSE)layers[i]).getPxHeight();
519
                        cellSizeX = Math.min(cellSizeX, csX);
520
                        cellSizeY = Math.min(cellSizeY, csY);
521 22548 dguerrero
                }
522 20729 amunoz
523 22548 dguerrero
                minX = layers[0].getFullExtent().getMinX();
524
                minY = layers[0].getFullExtent().getMinY();
525
                maxX = layers[0].getFullExtent().getMaxX();
526
                maxY = layers[0].getFullExtent().getMaxY();
527
528
                for(int i=1; i<layers.length;i++){
529 20729 amunoz
530 22548 dguerrero
                        minX= Math.min(minX,layers[i].getFullExtent().getMinX());
531
                        minY= Math.min(minY,layers[i].getFullExtent().getMinY());
532
                        maxX= Math.max(maxX,layers[i].getFullExtent().getMaxX());
533
                        maxY= Math.max(maxY,layers[i].getFullExtent().getMaxY());
534 20729 amunoz
                }
535
536 26975 amunoz
                result = new GridExtent(minX,minY,maxX,maxY,cellSizeX,cellSizeY);
537 20729 amunoz
                return result;
538
        }
539
540 20733 amunoz
        /**
541 20803 amunoz
         * Escritura del resultado en disco y carga en la vista
542 20795 amunoz
         */
543 22548 dguerrero
        public void createLayer(){
544 20729 amunoz
                try{
545 20795 amunoz
                        // Escritura de los datos a fichero temporal
546
                        int endIndex = fileName.lastIndexOf(".");
547
                        if (endIndex < 0)
548
                                 endIndex = fileName.length();
549 20729 amunoz
                        GeoRasterWriter grw = null;
550 20795 amunoz
                        writerBufferServer = new WriterBufferServer(mosaicGrid.getRasterBuf());
551 20729 amunoz
                        AffineTransform aTransform = new AffineTransform(fullExtend.getCellSize(),0.0,0.0,-fullExtend.getCellSize(),fullExtend.getMin().getX(),fullExtend.getMax().getY());
552 22761 dguerrero
                        grw = GeoRasterWriter.getWriter(writerBufferServer, fileName, mosaicGrid.getBandCount(),aTransform, mosaicGrid.getRasterBuf().getWidth(), mosaicGrid.getRasterBuf().getHeight(), mosaicGrid.getRasterBuf().getDataType(), GeoRasterWriter.getWriter(fileName).getParams(), inputRasterLayers[0].getProjection());
553 20729 amunoz
                        grw.dataWrite();
554 22548 dguerrero
                        grw.setWkt((String)((FLyrRasterSE)inputRasterLayers[0]).getWktProjection());
555 20729 amunoz
                        grw.writeClose();
556 20829 amunoz
                        mosaicGrid.getRasterBuf().free();
557 26076 amunoz
                        outputRasterLayer = FLyrRasterSE.createLayer(fileName.substring(fileName.lastIndexOf(File.separator) + 1, endIndex),
558 22548 dguerrero
                                        fileName, null);
559 20795 amunoz
560 20729 amunoz
                } catch (NotSupportedExtensionException e) {
561
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e);
562
                } catch (IOException e) {
563
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
564 20795 amunoz
                } catch (InterruptedException e) {
565
                                Thread.currentThread().interrupt();
566
                } catch (RasterDriverException e) {
567 21789 dguerrero
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "raster_buffer_invalid_extension"), this, e);
568 22548 dguerrero
                } catch (LoadLayerException e) {
569
                        RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
570 20795 amunoz
                }
571
572 20729 amunoz
        }
573
574 22548 dguerrero
        public Object getResult() {
575
                return outputRasterLayer;
576 26975 amunoz
577 22548 dguerrero
        }
578
579
580 20733 amunoz
        /**
581
         * @return descripcion
582
         * */
583 20729 amunoz
        public String getTitle() {
584
                return PluginServices.getText(this,"mosaic_process");
585
        }
586
587
588 20733 amunoz
        /**
589 20829 amunoz
         *  @return String con el log en cada parte del proceso
590
         * */
591
        public String getLog()
592
        {
593
                if(proceso==0)
594
                        return PluginServices.getText(this,"load_buffer_data");
595
                else if (proceso==1)
596
                        return PluginServices.getText(this,"generate_mosaic");
597
                else
598
                        return PluginServices.getText(this,"write_to_file");
599
        }
600
601
        /**
602 20733 amunoz
         * @return  indicador de progreso
603
         * */
604 20729 amunoz
        public int getPercent() {
605 20733 amunoz
                if(writerBufferServer==null)
606
                        return percent;
607
                else
608
                        return writerBufferServer.getPercent();
609 20729 amunoz
        }
610
611
612
        /*
613
        // Identificaci?n de zonas de solapamiento
614
        public boolean getSolapes(FLyrRasterSE raster1, FLyrRasterSE raster2){
615

616
                Grid grid1=null, grid2=null, aux=null;;
617
                IRasterDataSource dsetCopy = null;
618
                dsetCopy =raster1.getDataSource().newDataset();
619
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
620

621
                IRasterDataSource dsetCopy2 = null;
622
                dsetCopy2 =raster2.getDataSource().newDataset();
623
                BufferFactory bufferFactory2 = new BufferFactory(dsetCopy2);
624

625

626
                if (!RasterBuffer.loadInMemory(dsetCopy))
627
                        bufferFactory.setReadOnly(true);
628

629
                try {
630
                        grid1 = new Grid(bufferFactory,raster1.getRenderBands());
631
                        grid2= new Grid(bufferFactory2,raster2.getRenderBands());
632
                } catch (RasterBufferInvalidException e) {
633
                        e.printStackTrace();
634
                }
635

636
                // En grid1 la imagen con la cordenada x menor.
637
                if(grid2.getGridExtent().getMin().getX()< grid1.getGridExtent().getMin().getX())
638
                        {
639
                                try {
640
                                        grid1 = new Grid(bufferFactory2,raster2.getRenderBands());
641
                                        grid2= new Grid(bufferFactory,raster1.getRenderBands());
642
                                } catch (RasterBufferInvalidException e) {
643
                                        e.printStackTrace();
644
                                }
645

646
                        }
647

648
                double xmin= grid1.getGridExtent().getMin().getX();
649
                double xmax= grid1.getGridExtent().getMax().getX();
650
                double ymin= grid1.getGridExtent().getMin().getY();
651
                double ymax= grid1.getGridExtent().getMax().getY();
652

653
                double xmin2= grid2.getGridExtent().getMin().getX();
654
                double ymin2= grid2.getGridExtent().getMin().getY();
655

656
                if(!(xmin2>xmin && xmin2<xmax)){
657
                        System.out.print("Las imagenes no se solapan en las X");
658
                        return false;
659
                }
660

661
                if(!(ymin2>ymin && ymin2<ymax)){
662
                        System.out.print("Las imagenes no se solapan en las Y");
663
                        return false;
664
                }
665

666
                // Detectado el solapamiento
667
                System.out.print("Rango x["+ xmin2 + ","+ Math.min(xmax,grid2.getGridExtent().getMax().getX())+"].");
668
                System.out.print("Rango y["+ ymin2 + ","+ Math.min(ymax,grid2.getGridExtent().getMax().getY())+"].");
669

670
                return true;
671
        }*/
672
673
674
}