Statistics
| Revision:

gvsig-raster / org.gvsig.raster.reproject / trunk / org.gvsig.raster.reproject / org.gvsig.raster.reproject.app.reprojectclient / src / main / java / org / gvsig / raster / reproject / app / PrepareLayerAskProjection.java @ 2129

History | View | Annotate | Download (11.5 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.reproject.app;
23

    
24
import java.io.File;
25
import java.util.HashMap;
26
import java.util.Map;
27

    
28
import org.cresques.cts.IProjection;
29
import org.gvsig.app.prepareAction.PrepareContext;
30
import org.gvsig.app.prepareAction.PrepareContextView;
31
import org.gvsig.app.prepareAction.PrepareDataStoreParameters;
32
import org.gvsig.app.prepareAction.PrepareLayer;
33
import org.gvsig.fmap.crs.CRSFactory;
34
import org.gvsig.fmap.dal.DALLocator;
35
import org.gvsig.fmap.dal.DataManager;
36
import org.gvsig.fmap.dal.DataStoreParameters;
37
import org.gvsig.fmap.dal.coverage.RasterLocator;
38
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
39
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
40
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
41
import org.gvsig.fmap.dal.coverage.util.CRSUtils;
42
import org.gvsig.fmap.mapcontext.MapContextLocator;
43
import org.gvsig.fmap.mapcontext.MapContextManager;
44
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
45
import org.gvsig.fmap.mapcontext.layers.FLayer;
46
import org.gvsig.raster.algorithm.RasterBaseAlgorithmLibrary;
47
import org.gvsig.raster.algorithm.process.DataProcess;
48
import org.gvsig.raster.algorithm.process.IProcessActions;
49
import org.gvsig.raster.algorithm.process.ProcessException;
50
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster;
51
import org.gvsig.raster.fmap.layers.FLyrRaster;
52
import org.gvsig.raster.mainplugin.config.Configuration;
53
import org.gvsig.raster.reproject.algorithm.ReprojectProcess;
54
import org.gvsig.raster.reproject.app.preparelayer.RasterProjectionActionsDialog;
55
import org.gvsig.raster.reproject.app.preparelayer.ReprojectionQueue;
56
import org.gvsig.raster.swing.RasterSwingLibrary;
57
import org.gvsig.tools.exception.BaseException;
58

    
59
public class PrepareLayerAskProjection implements PrepareDataStoreParameters, PrepareLayer, IProcessActions {
60
        protected CRSUtils              crsUtil                       = RasterLocator.getManager().getCRSUtils(); 
61
        private boolean                 changeReprojectionOption      = true;
62
        private int                     lastReprojectionOption        = RasterDataParameters.NEW_PROJETION_TO_THE_LAYER;
63
        private ReprojectionQueue       queue                         = ReprojectionQueue.getSingleton();
64
        
65
        @SuppressWarnings("deprecation")
66
        public DataStoreParameters prepare(DataStoreParameters storeParameters,
67
                        PrepareContext context) throws BaseException {
68
                if(!(storeParameters instanceof RasterDataParameters))
69
                        return storeParameters;
70
                
71
                DataManager dataManager = DALLocator.getDataManager();
72
                RasterDataStore dataStore = (RasterDataStore)dataManager.createStore(storeParameters);
73
                
74
                if (Configuration.getValue("general_ask_projection", Boolean.valueOf(false)).booleanValue()) {
75
                        IProjection proj = readProjection(dataStore);
76
                        if(proj != null) {
77
                                ((RasterDataParameters)storeParameters).setDynValue("CRS", proj);
78
                                compareProjections(proj, context.getViewProjection(), dataStore);
79
                        }
80
                }
81
                
82
                if(dataStore != null)
83
                        dataStore.close();
84
                
85
                if(((RasterDataParameters)storeParameters).getReprojectionOption() == RasterDataParameters.NEW_PROJETION_TO_THE_LAYER)
86
                        ((RasterDataParameters)storeParameters).setDynValue("CRS", null);
87
                        
88
                if(((RasterDataParameters)storeParameters).getReprojectionOption() == RasterDataParameters.NOT_LOAD)
89
                        return null;
90
                
91
                return storeParameters;
92
        }
93
        
94
        public void post(DataStoreParameters storeParameters, PrepareContext context) {
95
                changeReprojectionOption = true;
96
        }
97
        
98
        public void pre(DataStoreParameters storeParameters, PrepareContext context) {
99
        }
100
        
101
        /**
102
         * If this flag is true the reprojection options can be changed by the user 
103
         * @return
104
         */
105
        public boolean getChangeOption() {
106
                return changeReprojectionOption;
107
        }
108
        
109
        /**
110
         * If this flag is true the reprojection options can be changed by the user 
111
         * @param changeOption
112
         */
113
        public void setChangeOption(boolean changeOption) {
114
                this.changeReprojectionOption = changeOption;
115
        }
116
        
117
        public FLayer prepare(FLayer layer, PrepareContextView context) {
118
                if (!(layer instanceof DefaultFLyrRaster))
119
                        return layer;
120
                DefaultFLyrRaster lyrRaster = (DefaultFLyrRaster) layer;
121
                String layerName = lyrRaster.getName();
122

    
123
                RasterDataParameters params = (RasterDataParameters)lyrRaster.getDataStore().getParameters();
124
                int repOption = params.getReprojectionOption();
125
                
126
                if(repOption == RasterDataParameters.REPROJECT_DATA) {
127
                        RasterDataParameters rasterParams = reproject(lyrRaster, context.getViewProjection());
128
                        if(rasterParams != null) {
129
                                MapContextManager mcm = MapContextLocator.getMapContextManager();
130
                                try {
131
                                        return (DefaultFLyrRaster) mcm.createLayer(layerName, rasterParams);
132
                                } catch (LoadLayerException e) {
133
                                        return lyrRaster;
134
                                }
135
                                /*DefaultFLyrRaster lyr = new DefaultFLyrRaster();
136
                                DataManager dataManager = DALLocator.getDataManager();
137
                                try {
138
                                        DataStore dataStore = dataManager.createStore(rasterParams);
139
                                        lyr.setName(layerName);
140
                                        lyr.setDataStore(dataStore);
141
                                        return lyr;
142
                                } catch (ValidateDataParametersException e) {
143
                                        return lyrRaster;
144
                                } catch (InitializeException e) {
145
                                        return lyrRaster;
146
                                } catch (ProviderNotRegisteredException e) {
147
                                        return lyrRaster;
148
                                } catch (LoadLayerException e) {
149
                                        return lyrRaster;
150
                                }*/
151
                        }
152
                } else if(repOption == RasterDataParameters.REPROJECT_VIEW) {
153
                        try {
154
                                if (lyrRaster != null && lyrRaster.readProjection() != null) {
155
                                        context.getMapControl().setProjection(lyrRaster.readProjection());
156
                                        lyrRaster.setVisible(true);
157
                                }
158
                        } catch (RasterDriverException e) {
159
                                RasterSwingLibrary.messageBoxError("Error reading the projection", null, e);
160
                        }
161
                } else if(repOption == RasterDataParameters.DONT_CHANGE_PROJECTION) {
162
                        lyrRaster.setVisible(true);
163
                        return lyrRaster;
164
                } else if(repOption == RasterDataParameters.NEW_PROJETION_TO_THE_LAYER) {
165
                        IProjection proj = context.getViewProjection();
166
                        if(proj != null)
167
                                lyrRaster.setProjection(proj, false);
168
                        lyrRaster.setVisible(true);
169
                        return lyrRaster;
170
                }
171

    
172
                return lyrRaster;
173
        }
174
        
175
        private RasterDataParameters reproject(FLyrRaster lyrRaster, IProjection dstProj) {
176
                RasterDataParameters params = (RasterDataParameters)lyrRaster.getDataStore().getParameters();
177
                int repOption = params.getReprojectionOption();
178

    
179
                if(repOption == RasterDataParameters.REPROJECT_DATA) {
180
                        try {
181
                                IProjection srcProj = readProjection(lyrRaster.getDataStore());
182
                                if(srcProj == null)
183
                                        return null;
184

    
185
                                String uri = params.getURI();
186
                                uri = uri.substring(0, uri.lastIndexOf(".")) + "_" + dstProj.getAbrev() + uri.substring(uri.lastIndexOf("."));
187
                                uri = uri.replace(':', '_');
188

    
189
                                if(!new File(uri).exists()) {                                        
190
                                        DataProcess process = null;
191
                                        try {
192
                                                process = RasterBaseAlgorithmLibrary.getManager().createRasterTask("RasterReprojectionProcess");
193
                                        } catch (ProcessException e1) {
194
                                                RasterSwingLibrary.messageBoxError("error_creating_algorithm", null, e1);
195
                                                return params;
196
                                        }
197
                                        process.setActions(this);
198
                                        process.addParam(ReprojectProcess.RASTER_STORE, lyrRaster.getDataStore());
199
                                        process.addParam(ReprojectProcess.SIZEX, 0); //Calculo autom?tico de tama?o
200
                                        process.addParam(ReprojectProcess.SIZEY, 0);
201
                                        process.addParam(ReprojectProcess.PATH, uri);
202
                                        process.addParam(ReprojectProcess.SRC_PROJECTION, srcProj);
203
                                        process.addParam(ReprojectProcess.DST_PROJECTION, dstProj);
204
                                        process.addParam(ReprojectProcess.CELLSIZE, lyrRaster.getDataStore().getCellSize());
205

    
206
                                        //Despierta la cola que reproyecta im?genes
207
                                        synchronized (queue) {
208
                                                queue.add(process, this);        
209
                                                queue.notify();
210
                                        }
211

    
212
                                        //Se suspende hasta que la cola haya terminado
213
                                        try {
214
                                                synchronized (this) {
215
                                                        this.wait();
216
                                                }
217
                                        } catch (InterruptedException e) {
218
                                        }
219
                                        
220
                                        if(process.getResult() != null) {
221
                                                HashMap<String, Object> map = (HashMap<String, Object>)process.getResult();
222
                                                String filename = (String)map.get(ReprojectProcess.FILENAME);
223
                                                params.setURI(filename);
224
                                                return params;
225
                                        }
226
                                } else {
227
                                        params.setURI(uri);
228
                                        return params;
229
                                }
230
                                
231
                        } catch (RasterDriverException e) {
232
                                RasterSwingLibrary.messageBoxError("Error creating layer", null, e);
233
                        } 
234
                } else if(repOption == RasterDataParameters.REPROJECT_VIEW) {
235
                        //Esto se cambia en el prepare que tiene acceso al mapcontrol. Este prepare es a nivel de datos
236
                } 
237
                return null;
238
        }
239

    
240
        public void end(Object param) {
241
                
242
        }
243

    
244
        /**
245
         * Compares two projections and show a dialog to the user to make a decision
246
         * @param lyrProj
247
         * @param viewProj
248
         */
249
        private void compareProjections(IProjection lyrProj, IProjection viewProj, RasterDataStore dataStore) {
250
                if(!getChangeOption() || lyrProj == null || viewProj == null) {
251
                        ((RasterDataParameters)dataStore.getParameters()).setReprojectionOption(lastReprojectionOption);
252
                        return;
253
                }
254

    
255
                /*
256
                 * Si las proyecciones de vista y raster son distintas se lanza el
257
                 * dialogo de selecci?n de opciones. Este dialogo solo se lanza en caso
258
                 * de que el checkbox de aplicar a todos los ficheros no haya sido
259
                 * marcado. En este caso para el resto de ficheros de esa selecci?n se
260
                 * har? la misma acci?n que se hizo para el primero.
261
                 */
262
                if (!viewProj.getAbrev().endsWith(lyrProj.getAbrev())) {
263
                        String layerName = dataStore.getName().substring(dataStore.getName().lastIndexOf(File.separator) + 1, dataStore.getName().length());
264
                        RasterProjectionActionsDialog dialog = new RasterProjectionActionsDialog(
265
                                        dataStore.isReproyectable(), layerName, RasterDataParameters.REPROJECT_VIEW);
266
                        if (dialog != null) {
267
                                lastReprojectionOption = dialog.getRasterProjectionActionsPanel().getSelection();
268
                                ((RasterDataParameters)dataStore.getParameters()).setReprojectionOption(lastReprojectionOption);
269
                                setChangeOption(!dialog.getChangeProjectionOption());
270
                        }
271
                }
272
        }
273
        
274
        /**
275
         * Reads the projection from a DataStore
276
         * @param dataStore
277
         * @return
278
         * @throws RasterDriverException
279
         */
280
        @SuppressWarnings("deprecation")
281
        private IProjection readProjection(RasterDataStore dataStore) throws RasterDriverException {
282
                try {
283
                        crsUtil.setCRSFactory(CRSFactory.cp);
284
                        if( dataStore == null )
285
                                return null;
286
                        return crsUtil.convertWktToIProjection(dataStore.getWktProjection());
287
                } catch (Exception e) {
288
                        throw new RasterDriverException("Problems converting from WKT to IProjection", e);
289
                } catch (Error e) {
290
                        e.printStackTrace();
291
                        return null;
292
                }
293
        }
294

    
295
        public String getDescription() {
296
                return "Prepare projection for Raster Layer";
297
        }
298

    
299
        public String getName() {
300
                return "PrepareRasterLayerProjection";
301
        }
302

    
303
        public Object create() {
304
                return this;
305
        }
306

    
307
        public Object create(Object[] args) {
308
                return this;
309
        }
310

    
311
        @SuppressWarnings("rawtypes")
312
        public Object create(Map args) {
313
                return this;
314
        }
315

    
316
        public void interrupted() {
317
        }
318
        
319
        public void updateProgress(int current, int total) {
320
                
321
        }
322
}