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 / GeoreferencingProcessActions.java @ 1717

History | View | Annotate | Download (7.23 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;
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.app.georeferencingclient.listener.ViewMapRequestManager;
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 GeoPointList                      gpList                         = null;
50
        private String                            fileName                       = null;
51
        
52
        //?ltima capa procesada con los puntos de control. Al cerrar la aplicaci?n 
53
        //esta ser? la que se use como resultado 
54
        private FLyrRaster                        lastTestLyr                    = null;
55

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

    
100
                        AffineTransform at = new AffineTransform(        
101
                                        result.getPixelToMapCoefX()[1], 
102
                                        result.getPixelToMapCoefY()[1], 
103
                                        result.getPixelToMapCoefX()[2], 
104
                                        result.getPixelToMapCoefY()[2], 
105
                                        result.getPixelToMapCoefX()[0], 
106
                                        result.getPixelToMapCoefY()[0]);
107
                        storeClon.setAffineTransform(at);
108
                        
109
                }
110
                
111
                DefaultFLyrRaster lyr = new DefaultFLyrRaster();
112
                lyr.setDataStore(storeClon);
113
                lastTestLyr = lyr;        
114
                
115
                if(algorithm == Georeferencing.POLYNOMIAL) {
116
                        RasterProcess process;
117
                        try {
118
                                process = RasterBaseAlgorithmLibrary.getManager().createRasterTask("GeoreferencingProcess");
119
                                process.addParam("fLayer", lyr);
120
                                process.addParam("filename", file);
121
                                process.addParam("method", new Integer(method));
122
                                process.addParam("gpcs", gpList);
123
                                process.addParam("orden", new Integer(order));
124
                                process.addParam("xCellSize", new Double(cellsizeX));
125
                                process.addParam("yCellSize", new Double(cellsizeY));
126
                                process.setActions(this);
127
                                process.start();
128
                        } catch (ProcessException e) {
129
                                GeoreferencingSwingImplLibrary.messageBoxError("error_georef_process", null, e);
130
                        }
131
                }
132

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

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

    
205
        public void interrupted() {
206
        }
207

    
208
        /**
209
         * Obtiene la ?ltima capa procesada con los puntos de  control
210
         * @return FLyrRaterSE
211
         */
212
        public FLyrRaster getLastTestLayer() {
213
                return lastTestLyr;
214
        }
215
        
216
}