Statistics
| Revision:

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

History | View | Annotate | Download (10.6 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 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
import java.io.File;
45
import java.io.IOException;
46

    
47
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
48
import org.gvsig.raster.RasterProcess;
49
import org.gvsig.raster.buffer.BufferFactory;
50
import org.gvsig.raster.buffer.RasterBuffer;
51
import org.gvsig.raster.buffer.RasterBufferInvalidException;
52
import org.gvsig.raster.buffer.WriterBufferServer;
53
import org.gvsig.raster.dataset.GeoRasterWriter;
54
import org.gvsig.raster.dataset.IBuffer;
55
import org.gvsig.raster.dataset.IRasterDataSource;
56
import org.gvsig.raster.dataset.InvalidSetViewException;
57
import org.gvsig.raster.dataset.NotSupportedExtensionException;
58
import org.gvsig.raster.dataset.io.RasterDriverException;
59
import org.gvsig.raster.grid.Grid;
60
import org.gvsig.raster.grid.GridExtent;
61
import org.gvsig.raster.grid.OutOfGridException;
62
import org.gvsig.raster.util.RasterToolsUtil;
63

    
64
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
65
import com.iver.andami.PluginServices;
66
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
67
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
68
import com.iver.cit.gvsig.fmap.layers.FLayer;
69
import com.iver.cit.gvsig.fmap.layers.FLayers;
70

    
71

    
72
/** 
73
* Clase que implementa el de construccion de un mosaico.
74
* 
75
* @author aMu?oz (alejandro.mu?oz@uclm.es)
76
* @version 30/4/2008
77
* */
78

    
79
public class MosaicProcess extends RasterProcess {
80

    
81
        // Layers que intervienen en el proceso
82
        private FLayers layers = null;
83

    
84
        // Extend completo del mosaico
85
        private GridExtent fullExtend= null;
86
        
87
        // Grid resultante
88
        Grid mosaicGrid = null;
89
        
90
        // Buffers con las imagenes
91
        RasterBuffer buffers[]= null;
92
        
93
        // codigo operacion maypr, menor, valor de la situada encima.
94
        int codOp= 0;
95
        
96
        // indicador de proceso
97
        int proceso=0; // Carga de datos
98
        
99
         int percent=0;
100

    
101
        /** Inicializaci?n de los par?metros
102
         *  layers -  FLayers con los layers seleccionados para el mosaico.
103
         * 
104
         *         En la inicializacion se calcula el grid resultante.
105
         * */
106
        
107
        public void init() {
108
                
109
                layers= (FLayers)getParam("layers");
110
                codOp= getIntParam("codOp");
111
                
112
                // Calculo del extend resultante
113
                fullExtend= calculateExtend(layers);
114
                
115
                try {
116
                                mosaicGrid= new Grid(fullExtend,fullExtend,IBuffer.TYPE_BYTE,new int[]{0,1,2});
117
                } catch (RasterBufferInvalidException e) {
118
                        RasterToolsUtil.messageBoxError("buffer_incorrecto", this, e);
119
                }
120
        }
121

    
122
        
123
        
124
        /**
125
         *  Proceso
126
         * */
127
        public void process() throws InterruptedException {
128
                
129
                // Parte de carga de los datos
130
                buffers= new RasterBuffer[layers.getLayersCount()];
131
                IRasterDataSource dsetCopy = null; 
132
                try {
133
                
134
                        double minX= fullExtend.getMin().getX();
135
                        double minY= fullExtend.getMin().getY();
136
                        double maxX=  fullExtend.getMax().getX();
137
                        double maxY=  fullExtend.getMax().getY();
138
                        // Se cargan todos los raster en los grid correspondientes
139
                        for(int i=0; i< layers.getLayersCount();i++)
140
                        {
141
                                dsetCopy = ((FLyrRasterSE)layers.getLayer(i)).getDataSource().newDataset();
142
                                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
143
                                bufferFactory.setAdjustToExtent(false);
144
                                bufferFactory.setDrawableBands(((FLyrRasterSE)layers.getLayer(i)).getRenderBands());
145
                                bufferFactory.setAreaOfInterest(minX,minY,maxX,maxY,fullExtend.getNX(),fullExtend.getNY());
146
                                buffers[i]= (RasterBuffer) bufferFactory.getRasterBuf();
147
                        }
148
                }catch (RasterDriverException e) {
149
                        // TODO Auto-generated catch block
150
                        e.printStackTrace();
151
                } catch (InvalidSetViewException e) {
152
                        // TODO Auto-generated catch block
153
                        e.printStackTrace();
154
                } catch (InterruptedException e) {
155
                        // TODO Auto-generated catch block
156
                        e.printStackTrace();
157
                }
158
                
159
                
160
                percent= 22;
161
                
162
                // CONSTRUCCION DE LA IMAGEN
163
                
164
                for(int row=0; row<mosaicGrid.getLayerNY(); row++){
165
                        for(int col=0; col<mosaicGrid.getLayerNX();col++){
166
                                setValue(row,col,0);
167
                        }
168
                }
169
                
170
        }
171

    
172
        
173

    
174
        /**
175
         * M?todo que calcula el extend resultante para la operaci?n de mosaico
176
         * 
177
         * @param layers que intervienen en la operacion.
178
         * @return GridExtend del mosaico
179
         * */
180
        
181
        private GridExtent calculateExtend (FLayers layers){
182
                GridExtent result= null;
183
                
184
                double minX=0,maxX=0,minY=0,maxY=0, cellSize=25;
185
                try {
186
                        
187
                        minX = layers.getLayer(0).getFullExtent().getMinX();
188
                        minY = layers.getLayer(0).getFullExtent().getMinY();
189
                        maxX = layers.getLayer(0).getFullExtent().getMaxX();
190
                        maxY = layers.getLayer(0).getFullExtent().getMaxY();
191
                        
192
                        for(int i=1; i<layers.getLayersCount();i++){
193
                                minX= Math.min(minX,layers.getLayer(i).getFullExtent().getMinX());
194
                                minY= Math.min(minY,layers.getLayer(i).getFullExtent().getMinY());
195
                                maxX= Math.max(maxX,layers.getLayer(i).getFullExtent().getMaxX());
196
                                maxY= Math.max(maxY,layers.getLayer(i).getFullExtent().getMaxY());
197
                        }
198
                        
199
                } catch (ExpansionFileReadException e) {
200
                        e.printStackTrace();
201
                } catch (ReadDriverException e) {
202
                        e.printStackTrace();
203
                }
204
                
205
                result = new GridExtent(minX,minY,maxX,maxY,25);
206
                return result;
207
        }
208
        
209
        
210
        public void writeToFile(){
211

    
212
                try{
213
                        
214
                        String filename= "mosaico.img";
215
                        GeoRasterWriter grw = null;
216
                        WriterBufferServer writerBufferServer = new WriterBufferServer(mosaicGrid.getRasterBuf());
217
                        AffineTransform aTransform = new AffineTransform(fullExtend.getCellSize(),0.0,0.0,-fullExtend.getCellSize(),fullExtend.getMin().getX(),fullExtend.getMax().getY());
218
                        grw = GeoRasterWriter.getWriter(writerBufferServer, filename, mosaicGrid.getBandCount(),aTransform, mosaicGrid.getRasterBuf().getWidth(), mosaicGrid.getRasterBuf().getHeight(), mosaicGrid.getRasterBuf().getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
219
                        grw.dataWrite();
220
                        grw.setWkt((String)((FLyrRasterSE)layers.getLayer(0)).getWktProjection());
221
                        grw.writeClose();
222
                        
223
                        //mapContext.beginAtomicEvent();
224
                        FLayer lyr = null;
225
                        int endIndex = filename.lastIndexOf(".");
226
                        if (endIndex < 0)
227
                                endIndex = filename.length();
228
                
229
                        lyr = FLyrRasterSE.createLayer(
230
                                        filename.substring(filename.lastIndexOf(File.separator) + 1, endIndex),
231
                                        filename,
232
                                        ((FLyrRasterSE)layers.getLayer(0)).getProjection()
233
                                        );
234

    
235
                /*        mapContext.getLayers().addLayer(lyr);
236
                        //mapContext.endAtomicEvent();
237
                        mapContext.invalidate();*/
238
                
239
                } catch (NotSupportedExtensionException e) {
240
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e);
241
                } catch (RasterDriverException e) {
242
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);        
243
                } catch (IOException e) {
244
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
245
                }catch (LoadLayerException e) {
246
                        RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
247
                }catch (InterruptedException e) {
248
                        Thread.currentThread().interrupt();
249
                }         
250
        }
251
        
252
        
253
        
254
        public byte[] setValue(int x, int y, int codop){
255
                byte result[]= new byte[3];
256
                result[0]=(byte) -128;
257
                for(int band=0; band<3;band++){
258
                        for(int buf=0;buf<buffers.length;buf++){
259
                                result[band]=(byte) Math.max(result[band],(byte)(buffers[buf].getElemByte(x,y,band)&0xff));
260
                                
261
                        }
262
                        try {
263
                                mosaicGrid.setBandToOperate(band);
264
                                mosaicGrid.setCellValue(y,x,(byte)result[band]);
265
                        } catch (OutOfGridException e) {
266
                                // TODO Auto-generated catch block
267
                                e.printStackTrace();
268
                        }
269
                }
270
                
271
                
272
                return result;
273
                
274
        }
275
        
276
        
277
        public String getTitle() {
278
                return PluginServices.getText(this,"mosaic_process");
279
        }
280

    
281
        
282
        public int getPercent() {
283
                // TODO Auto-generated method stub
284
                return percent;
285
        }
286

    
287

    
288
        
289
        
290
        
291
        
292
        
293
        /*
294
        // Identificaci?n de zonas de solapamiento
295
        public boolean getSolapes(FLyrRasterSE raster1, FLyrRasterSE raster2){
296
                
297
                Grid grid1=null, grid2=null, aux=null;;
298
                IRasterDataSource dsetCopy = null; 
299
                dsetCopy =raster1.getDataSource().newDataset();
300
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
301
                
302
                IRasterDataSource dsetCopy2 = null; 
303
                dsetCopy2 =raster2.getDataSource().newDataset();
304
                BufferFactory bufferFactory2 = new BufferFactory(dsetCopy2);
305
                
306
                
307
                if (!RasterBuffer.loadInMemory(dsetCopy))
308
                        bufferFactory.setReadOnly(true);        
309
                
310
                try {
311
                        grid1 = new Grid(bufferFactory,raster1.getRenderBands());
312
                        grid2= new Grid(bufferFactory2,raster2.getRenderBands());
313
                } catch (RasterBufferInvalidException e) {
314
                        e.printStackTrace();
315
                }        
316
                
317
                // En grid1 la imagen con la cordenada x menor.
318
                if(grid2.getGridExtent().getMin().getX()< grid1.getGridExtent().getMin().getX())
319
                        {
320
                                try {
321
                                        grid1 = new Grid(bufferFactory2,raster2.getRenderBands());
322
                                        grid2= new Grid(bufferFactory,raster1.getRenderBands());
323
                                } catch (RasterBufferInvalidException e) {
324
                                        e.printStackTrace();
325
                                }        
326
                                
327
                        }
328
                
329
                double xmin= grid1.getGridExtent().getMin().getX();
330
                double xmax= grid1.getGridExtent().getMax().getX();
331
                double ymin= grid1.getGridExtent().getMin().getY();
332
                double ymax= grid1.getGridExtent().getMax().getY();
333
                
334
                double xmin2= grid2.getGridExtent().getMin().getX();
335
                double ymin2= grid2.getGridExtent().getMin().getY();
336
                
337
                if(!(xmin2>xmin && xmin2<xmax)){
338
                        System.out.print("Las imagenes no se solapan en las X");
339
                        return false;
340
                }
341
        
342
                if(!(ymin2>ymin && ymin2<ymax)){
343
                        System.out.print("Las imagenes no se solapan en las Y");
344
                        return false;
345
                }
346
                
347
                // Detectado el solapamiento
348
                System.out.print("Rango x["+ xmin2 + ","+ Math.min(xmax,grid2.getGridExtent().getMax().getX())+"].");
349
                System.out.print("Rango y["+ ymin2 + ","+ Math.min(ymax,grid2.getGridExtent().getMax().getY())+"].");
350
                
351
                return true;
352
        }*/
353

    
354

    
355

    
356
}