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 @ 1712

History | View | Annotate | Download (7.02 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.mapcontext.exceptions.LoadLayerException;
27
import org.gvsig.i18n.Messages;
28
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster;
29
import org.gvsig.raster.fmap.layers.FLyrRaster;
30
import org.gvsig.raster.georeferencing.app.georeferencingclient.listener.ViewMapRequestManager;
31
import org.gvsig.raster.georeferencing.lib.impl.GeoTransformDataResult;
32
import org.gvsig.raster.georeferencing.swing.exception.InvalidRequestException;
33
import org.gvsig.raster.georeferencing.swing.impl.GeoreferencingSwingImplLibrary;
34
import org.gvsig.raster.tools.algorithm.base.RasterBaseAlgorithmLibrary;
35
import org.gvsig.raster.tools.algorithm.base.process.IProcessActions;
36
import org.gvsig.raster.tools.algorithm.base.process.ProcessException;
37
import org.gvsig.raster.tools.algorithm.base.process.RasterProcess;
38
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
39

    
40
/**
41
 * Gestor para operaciones con las vistas y zooms
42
 * 
43
 * 04/02/2008
44
 * @author Nacho Brodin nachobrodin@gmail.com
45
 */
46
public class GeoreferencingProcessActions implements IProcessActions {
47
        private ViewMapRequestManager             viewMapRequestManager          = null;
48
        private GeoPointList                      gpList                         = 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                        lastTestLayer                  = null;
54

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

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

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

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