Statistics
| Revision:

gvsig-raster / org.gvsig.raster.georeferencing / trunk / org.gvsig.raster.georeferencing / org.gvsig.raster.georeferencing.app / org.gvsig.raster.georeferencing.app.georeferencingclient / src / main / java / org / gvsig / raster / georeferencing / app / georeferencingclient / listener / GeoreferencingProcessActions.java @ 1752

History | View | Annotate | Download (7.18 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. 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
package org.gvsig.raster.georeferencing.app.georeferencingclient.listener;
20

    
21
import java.awt.geom.AffineTransform;
22
import java.io.File;
23

    
24
import org.gvsig.fmap.dal.coverage.RasterLocator;
25
import org.gvsig.fmap.dal.coverage.datastruct.GeoPointList;
26
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
27
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
28
import org.gvsig.i18n.Messages;
29
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster;
30
import org.gvsig.raster.fmap.layers.FLyrRaster;
31
import org.gvsig.raster.georeferencing.GeoreferencingLibrary;
32
import org.gvsig.raster.georeferencing.lib.impl.GeoTransformDataResult;
33
import org.gvsig.raster.georeferencing.swing.exception.InvalidRequestException;
34
import org.gvsig.raster.georeferencing.swing.impl.GeoreferencingSwingImplLibrary;
35
import org.gvsig.raster.tools.algorithm.base.RasterBaseAlgorithmLibrary;
36
import org.gvsig.raster.tools.algorithm.base.process.IProcessActions;
37
import org.gvsig.raster.tools.algorithm.base.process.ProcessException;
38
import org.gvsig.raster.tools.algorithm.base.process.RasterProcess;
39
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
40

    
41
/**
42
 * Gestor para operaciones con las vistas y zooms
43
 * 
44
 * 04/02/2008
45
 * @author Nacho Brodin nachobrodin@gmail.com
46
 */
47
public class GeoreferencingProcessActions implements IProcessActions {
48
        private ViewMapRequestManager             viewMapRequestManager          = null;
49
        private String                            fileName                       = null;
50
        
51
        //?ltima capa procesada con los puntos de control. Al cerrar la aplicaci?n 
52
        //esta ser? la que se use como resultado 
53
        private FLyrRaster                        lastTestLyr                    = null;
54

    
55
        public void setViewMapRequest(ViewMapRequestManager vMap) {
56
                viewMapRequestManager = vMap;
57
        }
58
        
59
        /**
60
         * A?ade una capa en la vista del mapa para previsualizar el resultado
61
         * @param lyr
62
         * @param algorithm Algoritmo con el que se realiza la transformaci?n
63
         * @throws LoadLayerException 
64
         */
65
        public void addTestRasterLayer(        RasterDataStore store, 
66
                                                                        int algorithm, 
67
                                                                        int method, 
68
                                                                        int order, 
69
                                                                        String file,
70
                                                                        double cellsizeX,
71
                                                                        double cellsizeY,
72
                                                                        GeoTransformDataResult result) throws LoadLayerException {
73
                if(!testNumberOfPoints(order, result.getGpcs()))
74
                        return;
75
                
76
                fileName = file;
77
                RasterDataStore storeClon = null;
78
                try {
79
                        storeClon = store.cloneDataStore();
80
                } catch (Exception e1) {
81
                        RasterToolsUtil.messageBoxError("error_clone_layer", this, e1);
82
                        return;
83
                }
84
                
85
                if(algorithm == GeoreferencingLibrary.AFFINE) {
86
                        if(result == null) {
87
                                RasterToolsUtil.messageBoxInfo("error_georef", this);
88
                                return;
89
                        }
90

    
91
                        AffineTransform at = new AffineTransform(        
92
                                        result.getPixelToMapCoefX()[1], 
93
                                        result.getPixelToMapCoefY()[1], 
94
                                        result.getPixelToMapCoefX()[2], 
95
                                        result.getPixelToMapCoefY()[2], 
96
                                        result.getPixelToMapCoefX()[0], 
97
                                        result.getPixelToMapCoefY()[0]);
98
                        storeClon.setAffineTransform(at);
99
                        
100
                }
101
                
102
                DefaultFLyrRaster lyr = new DefaultFLyrRaster();
103
                lyr.setDataStore(storeClon);
104
                lastTestLyr = lyr;        
105
                
106
                if(algorithm == GeoreferencingLibrary.POLYNOMIAL) {
107
                        RasterProcess process;
108
                        try {
109
                                process = RasterBaseAlgorithmLibrary.getManager().createRasterTask("GeoreferencingProcess");
110
                                process.addParam("RasterStore", lyr.getDataStore());
111
                                process.addParam("FileName", file);
112
                                process.addParam("InterpolationMethod", new Integer(method));
113
                                process.addParam("gcps", result.getGpcs());
114
                                process.addParam("Order", new Integer(order));
115
                                process.addParam("xCellSize", new Double(cellsizeX));
116
                                process.addParam("yCellSize", new Double(cellsizeY));
117
                                process.setActions(this);
118
                                process.start();
119
                        } catch (ProcessException e) {
120
                                GeoreferencingSwingImplLibrary.messageBoxError("error_georef_process", null, e);
121
                        }
122
                }
123

    
124
                //Con vista de de referencia cargamos la preview en esta
125
                if(viewMapRequestManager != null) {
126
                        if(algorithm == GeoreferencingLibrary.AFFINE) {
127
                                try {
128
                                        viewMapRequestManager.addTestRasterLayer(lyr);
129
                                } catch (InvalidRequestException e) {
130
                                        GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", null, e);
131
                                }
132
                        }                        
133
                } 
134
        }
135
        
136
        /**
137
         * Consulta si hay suficientes puntos de control en la lista para los calculos
138
         * @param order Orden del polinomio a utilizar
139
         * @return true si hay suficientes puntos de control y false si no los hay
140
         */
141
        private boolean testNumberOfPoints(int order, GeoPointList gpList) {
142
                if (gpList != null) {
143
                        if(gpList.size() <= 0) {
144
                                GeoreferencingSwingImplLibrary.messageBoxError("no_selected_points", null);
145
                                return false;
146
                        }
147
                        
148
                        // Obtenemos el n?mero de puntos activos
149
                        int nPointsActive = 0;
150
                        for (int i = 0; i < gpList.size(); i++) {
151
                                if (gpList.get(i).isActive())
152
                                        nPointsActive++;
153
                        }
154
                        int nPoints = (order + 1) * (order + 2) / 2;
155
                        if (nPointsActive < nPoints) {
156
                                GeoreferencingSwingImplLibrary.messageBoxError(Messages.getText("more_points") + ((int) Math.ceil(nPoints)), null);
157
                                return false;
158
                        }
159
                }
160
                return true;
161
        }
162
        
163
        /**
164
         * Elimina la capa de test de la vista de mapa
165
         * @throws InvalidRequestException 
166
         */
167
        public void removeTestRasterLayer() {
168
                if(viewMapRequestManager != null) {
169
                        try {
170
                                if(getLastTestLayer() != null)
171
                                        getLastTestLayer().setRemoveRasterFlag(false);
172
                                viewMapRequestManager.removeTestRasterLayer();
173
                                lastTestLyr = null;
174
                        } catch (InvalidRequestException e) {
175
                                GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", null);
176
                        }
177
                }
178
        }
179

    
180
        /**
181
         * Cuando termina el proceso de georreferenciaci?n carga la capa en la
182
         * vista con cartograf?a de referencia si esta existe.
183
         */
184
        public void end(Object param) {
185
                try {
186
                        String f = RasterLocator.getManager().getFileUtils().getLastPart(fileName, File.separator);
187
                        FLyrRaster lyr = DefaultFLyrRaster.createLayer(f, new File(fileName));
188
                        lastTestLyr = lyr;
189
                        if(viewMapRequestManager != null)
190
                                viewMapRequestManager.addTestRasterLayer(lyr);
191
                } catch (InvalidRequestException e) {
192
                        GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", null, e);
193
                } catch (LoadLayerException e) {
194
                        GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", null, e);
195
                }
196
        }
197

    
198
        public void interrupted() {
199
        }
200

    
201
        /**
202
         * Obtiene la ?ltima capa procesada con los puntos de  control
203
         * @return FLyrRaterSE
204
         */
205
        public FLyrRaster getLastTestLayer() {
206
                return lastTestLyr;
207
        }
208
        
209
}