Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / Georeferencing.java @ 18321

History | View | Annotate | Download (15.3 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.rastertools.georeferencing;
20

    
21
import java.awt.geom.AffineTransform;
22
import java.awt.geom.Point2D;
23
import java.awt.geom.Rectangle2D;
24
import java.util.ArrayList;
25

    
26
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
27
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
28
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
29
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
30
import org.gvsig.raster.datastruct.GeoPoint;
31
import org.gvsig.raster.util.RasterToolsUtil;
32
import org.gvsig.rastertools.georeferencing.ui.launcher.GeorefLauncherDialog;
33
import org.gvsig.rastertools.georeferencing.ui.table.GCPTablePanel;
34
import org.gvsig.rastertools.georeferencing.ui.zoom.InvalidRequestException;
35
import org.gvsig.rastertools.georeferencing.ui.zoom.ViewMapRequestManager;
36
import org.gvsig.rastertools.georeferencing.ui.zoom.ViewRasterRequestManager;
37
import org.gvsig.rastertools.georeferencing.ui.zoom.layers.GCPsGraphicLayer;
38
import org.gvsig.rastertools.georeferencing.ui.zoom.layers.GPGraphic;
39
import org.gvsig.rastertools.georeferencing.ui.zoom.layers.ZoomCursorGraphicLayer;
40
import org.gvsig.rastertools.georeferencing.ui.zoom.tools.ToolEvent;
41
import org.gvsig.rastertools.georeferencing.ui.zoom.tools.ToolListener;
42
import org.gvsig.rastertools.georeferencing.view.BaseZoomView;
43
import org.gvsig.rastertools.georeferencing.view.ViewDialog;
44
import org.gvsig.rastertools.georeferencing.view.ZoomMapDialog;
45
import org.gvsig.rastertools.georeferencing.view.ZoomRasterDialog;
46

    
47
import com.iver.andami.PluginServices;
48
import com.iver.andami.ui.mdiFrame.MDIFrame;
49
import com.iver.andami.ui.mdiManager.IWindow;
50
import com.iver.cit.gvsig.ProjectExtension;
51
import com.iver.cit.gvsig.fmap.MapControl;
52
import com.iver.cit.gvsig.project.Project;
53
import com.iver.cit.gvsig.project.documents.view.ProjectView;
54
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
55
import com.iver.cit.gvsig.project.documents.view.gui.IView;
56

    
57
/**
58
 * Clase principal para la georreferenciaci?n. Se encarga de la inicializaci?n
59
 * de la funcionalidad. Las acciones a realizar son:
60
 * <UL>
61
 * <LI>Lanzamiento del dialogo de inicializaci?n.</LI>
62
 * <LI>Carga de la capa raster a georreferenciar.</LI>
63
 * <LI>Creaci?n de las ventanas si no han sido creadas previamente.</LI>
64
 * <LI>Asignaci?n de tama?os y posiciones a las ventanas.</LI>
65
 * </UL>
66
 * 26/12/2007
67
 * @author Nacho Brodin (nachobrodin@gmail.com)
68
 */
69
public class Georeferencing implements ButtonsPanelListener, ToolListener {
70
        public static final int               UNDEFINED                      = -1;
71
        //Algoritmo
72
        public static final int               POLYNOMIAL                     = 0;
73
        public static final int               AFFINE                         = 1;
74
        
75
        //Tipo de georreferenciacion
76
        public static final int               WITH_MAP                       = 0;
77
        public static final int               WITHOUT_MAP                    = 1;
78
        
79
        private static int                    DEFAULT_DEGREE                 = 3;
80
        
81
        private GeorefLauncherDialog          initDialog                     = null;
82
        private FLyrRasterSE                  lyrToGeoref                    = null;
83
        private String                        selectedView                   = null;
84
        private static GCPTablePanel          table                          = null;
85
        
86
        private static final int              smallWindowsHeight             = 100;
87
        private static final double           smallWindowsWidthPercent       = 0.5;
88
        
89
        private ViewDialog                    viewRaster                     = null;
90
        private ViewDialog                    viewMap                        = null;
91
        
92
        private ZoomMapDialog                 zoomMap                        = null;
93
        private ZoomRasterDialog              zoomPixel                      = null;
94
        
95
        private ViewRasterRequestManager      viewRasterRequestManager       = null;
96
        private ViewMapRequestManager         viewMapRequestManager          = null;
97
        
98
        private ViewRasterRequestManager      zoomRasterRequestManager       = null;
99
        private ViewMapRequestManager         zoomMapRequestManager          = null;
100
        
101
        private ApplicationControlsListener   buttonsListener                = null;
102
        
103
        public Georeferencing() {
104
                
105
        }
106
                
107
        public void initialize() {
108
                launcher();
109
        }
110
        
111
        /**
112
         * Colocaci?n inicial de las ventanas del espacio de trabajo. Carga la configuraci?n
113
         * de ventanas con cartograf?a de referencia.
114
         */
115
        private void initWindowsWithoutMap() {
116
                MDIFrame p = (MDIFrame)PluginServices.getMainFrame();
117
                int totalWidth = p.getWidth(); 
118
                int wViews = totalWidth;
119
                int hViews = p.getHeight() - 178 - smallWindowsHeight;
120
                int posYViews = p.getHeight() - 138 - smallWindowsHeight;
121
                int smallWindowsWidth = (int)(totalWidth * 0.25);
122
                int tableWidth = (int)(totalWidth * 0.75);
123
                
124
                //Si hay ventanas de georreferenciaci?n abiertas las cerramos
125
                closeAllWindows();
126
                
127
                //Inicializaci?n de la tabla
128
                table = new GCPTablePanel(0, posYViews, tableWidth, smallWindowsHeight);
129
                buttonsListener = new ApplicationControlsListener(table, this);
130
                table.initialize(buttonsListener);
131
                                
132
                //Inicializaci?n de la ventana con la vista raster
133
                viewRaster = new ViewDialog(0, 0, wViews, hViews, this);
134
                viewRaster.setShowInfo(true);
135
                viewRasterRequestManager = new ViewRasterRequestManager(viewRaster, lyrToGeoref);
136
                viewRaster.setExtensionRequest(viewRasterRequestManager);
137
                
138
                //Inicializaci?n de la ventana del zoom de la vista raster
139
                zoomPixel = new ZoomRasterDialog(tableWidth, posYViews, smallWindowsWidth, smallWindowsHeight);
140
                zoomRasterRequestManager = new ViewRasterRequestManager(zoomPixel, lyrToGeoref);
141
                zoomPixel.setExtensionRequest(zoomRasterRequestManager);
142
        
143
                //A?adimos las ventanas a gvSIG
144
                PluginServices.getMDIManager().addWindow(viewRaster);
145
                PluginServices.getMDIManager().addWindow(table);
146
                PluginServices.getMDIManager().addWindow(zoomPixel);
147
                
148
                try {
149
                        //Inicializamos la vista de raster
150
                        viewRasterRequestManager.initRequest(lyrToGeoref.getFullExtent());
151
                        viewRaster.setZoomCursorSize(zoomPixel.getCanvasWidth() / 3, zoomPixel.getCanvasHeight() / 3);
152
                        
153
                        //Inicializamos el zoom de raster
154
                        Rectangle2D ext = viewRaster.getCursorAdjustedWorldCoordinates(zoomPixel.getCanvasWidth(), zoomPixel.getCanvasHeight());
155
                        zoomRasterRequestManager.initRequest(lyrToGeoref.getFullExtent());
156
                        zoomRasterRequestManager.request(ext);
157
                        /*zoomRasterRequestManager.request(new Rectangle2D.Double(ext.getX(), 
158
                                                                                        viewRaster.getViewExtent().getMaxY() - (viewRaster.getViewExtent().getMinY() - ext.getY()), 
159
                                                                                        ext.getWidth(), 
160
                                                                                        ext.getHeight()));*/
161
                        
162
                } catch (InvalidRequestException e1) {
163
                        RasterToolsUtil.messageBoxError("error_setview_preview", this);
164
                        PluginServices.getMDIManager().closeWindow(zoomPixel);
165
                        PluginServices.getMDIManager().closeWindow(viewRaster);
166
                        PluginServices.getMDIManager().closeWindow(table);
167
                }catch (Exception e1) {
168
                        RasterToolsUtil.messageBoxError("error_setview_preview", this);
169
                        PluginServices.getMDIManager().closeWindow(zoomPixel);
170
                        PluginServices.getMDIManager().closeWindow(viewRaster);
171
                        PluginServices.getMDIManager().closeWindow(table);
172
                }
173
        }
174
        
175
        /**
176
         * Colocaci?n inicial de las ventanas del espacio de trabajo. Carga la configuraci?n
177
         * de ventanas con cartograf?a de referencia.
178
         */
179
        private void initWindowsWithMap() {
180
                MDIFrame p = (MDIFrame)PluginServices.getMainFrame();
181
                int wViews = p.getWidth() >> 1;
182
                int hViews = p.getHeight() - 178 - smallWindowsHeight;
183
                int posYViews = p.getHeight() - 138 - smallWindowsHeight;
184
                int smallWindowsWidth = (int)(p.getWidth() * (smallWindowsWidthPercent * 0.5));
185
                int tableWidth = (int)(p.getWidth() * (1 - smallWindowsWidthPercent));
186
                
187
                //Si hay ventanas de georreferenciaci?n abiertas las cerramos
188
                closeAllWindows();
189
                
190
                MapControl mapControl = null;
191
                IWindow[] windowList = PluginServices.getMDIManager().getAllWindows();
192
                for (int i = 0; i < windowList.length; i++) {
193
                        if(windowList[i] instanceof IView && windowList[i].getWindowInfo().getTitle().endsWith(": " + selectedView)) 
194
                                mapControl = ((IView)windowList[i]).getMapControl();
195
                }
196
                
197
                if(mapControl == null) {
198
                        RasterToolsUtil.messageBoxError("error_lookingfor_view", this);
199
                        return;
200
                }
201
                
202
                //Inicializamos el control de tabla
203
                table = new GCPTablePanel(smallWindowsWidth, posYViews, tableWidth, smallWindowsHeight);
204
                buttonsListener = new ApplicationControlsListener(table, this);
205
                table.initialize(buttonsListener);
206
                
207
                //Inicializaci?n de la ventana con la vista mapa
208
                viewMap = new ViewDialog(0, 0, wViews, hViews, this);
209
                viewMap.setShowInfo(true);
210
                viewMapRequestManager = new ViewMapRequestManager(viewMap, mapControl);
211
                viewMap.setExtensionRequest(viewMapRequestManager);
212
                
213
                //Inicializaci?n de la ventana con la vista raster
214
                viewRaster = new ViewDialog(wViews, 0, wViews, hViews, this);
215
                //viewRaster.setMinxMaxyUL(false);
216
                viewRaster.setShowInfo(true);
217
                viewRasterRequestManager = new ViewRasterRequestManager(viewRaster, lyrToGeoref);
218
                viewRaster.setExtensionRequest(viewRasterRequestManager);
219
                
220
                //Inicializaci?n de la ventana del zoom de la vista raster
221
                zoomPixel = new ZoomRasterDialog(smallWindowsWidth + tableWidth, posYViews, smallWindowsWidth, smallWindowsHeight);
222
                zoomPixel.setMinxMaxyUL(false);
223
                zoomRasterRequestManager = new ViewRasterRequestManager(zoomPixel, lyrToGeoref);
224
                zoomPixel.setExtensionRequest(zoomRasterRequestManager);
225
                
226
                //Inicializaci?n de la ventana del zoom de la vista de referencia
227
                zoomMap = new ZoomMapDialog(0, posYViews, smallWindowsWidth, smallWindowsHeight);
228
                zoomMapRequestManager = new ViewMapRequestManager(zoomMap, mapControl);
229
                zoomMap.setExtensionRequest(zoomMapRequestManager);
230
                                                
231
                //A?adimos las ventanas a gvSIG
232
                PluginServices.getMDIManager().addWindow(viewRaster);
233
                PluginServices.getMDIManager().addWindow(viewMap);
234
                PluginServices.getMDIManager().addWindow(table);
235
                PluginServices.getMDIManager().addWindow(zoomMap);
236
                PluginServices.getMDIManager().addWindow(zoomPixel);
237
                
238
                try {
239
                        //Inicializamos la vista de mapa
240
                        viewMapRequestManager.initRequest(mapControl.getMapContext().getLayers().getFullExtent());
241
                        viewMap.setZoomCursorSize(zoomMap.getCanvasWidth() / 3, zoomMap.getCanvasHeight() / 3);
242

    
243
                        //Inicializamos la vista de raster
244
                        viewRasterRequestManager.initRequest(lyrToGeoref.getFullExtent());
245
                        viewRaster.setZoomCursorSize(zoomPixel.getCanvasWidth() / 3, zoomPixel.getCanvasHeight() / 3);
246
                        
247
                        //Inicializamos el zoom de mapa
248
                        Rectangle2D ext = viewMap.getCursorAdjustedWorldCoordinates(zoomMap.getCanvasWidth(), zoomMap.getCanvasHeight());
249
                        zoomMapRequestManager.initRequest(ext);
250
                        
251
                        //Inicializamos el zoom de raster
252
                        ext = viewRaster.getCursorAdjustedWorldCoordinates(zoomPixel.getCanvasWidth(), zoomPixel.getCanvasHeight());
253
                        zoomRasterRequestManager.initRequest(lyrToGeoref.getFullExtent());
254
                        zoomRasterRequestManager.request(ext);
255
                                                
256
                        createGCPLayer(viewRaster);
257
                        
258
                } catch (InvalidRequestException e1) {
259
                        RasterToolsUtil.messageBoxError("error_setview_preview", this);
260
                }catch (Exception e1) {
261
                        RasterToolsUtil.messageBoxError("error_setview_preview", this);
262
                }
263
        }
264
        
265
        /**
266
         * Cierra todas las ventanas de georreferenciaci?n
267
         */
268
        public void closeAllWindows() {
269
                if(zoomMap != null)
270
                        PluginServices.getMDIManager().closeWindow(zoomMap);
271
                if(zoomPixel != null)
272
                        PluginServices.getMDIManager().closeWindow(zoomPixel);
273
                if(viewRaster != null)
274
                        PluginServices.getMDIManager().closeWindow(viewRaster);
275
                if(viewMap != null)
276
                        PluginServices.getMDIManager().closeWindow(viewMap);
277
                if(table != null)
278
                        PluginServices.getMDIManager().closeWindow(table);
279
        }
280
        
281
        /**
282
         * Crea una capa de puntos de control y la a?ade a la vista
283
         * que se le indica por par?metro
284
         * @param view
285
         */
286
        public void createGCPLayer(BaseZoomView view) {
287
                GCPsGraphicLayer viewRasterGCPs = new GCPsGraphicLayer(GPGraphic.PIXEL, view.getControl());
288
                view.addGraphicLayer(viewRasterGCPs);
289
                //Puntos de prueba
290
                GeoPoint gp1 = new GeoPoint(new Point2D.Double(100, 100), new Point2D.Double(645344, 4927206));
291
                GPGraphic gpg1 = new GPGraphic(GPGraphic.PIXEL, gp1);
292
                GeoPoint gp2 = new GeoPoint(new Point2D.Double(150, 150), new Point2D.Double(646213, 4925620));
293
                GPGraphic gpg2 = new GPGraphic(GPGraphic.PIXEL, gp2);
294
                viewRasterGCPs.addPixelGeoPoint(gpg1);
295
                viewRasterGCPs.addPixelGeoPoint(gpg2);
296
        }
297
        
298
        /**
299
         * Lanzador del cuadro de inicializaci?n de propiedades de georreferenciaci?n.
300
         */
301
        private void launcher() {
302
                Project p = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
303
                ArrayList docs = p.getDocumentsByType(ProjectViewFactory.registerName);
304
                String[] viewList = new String[docs.size()];
305
                for (int i = 0; i < docs.size(); i++) 
306
                        viewList[i] = (((ProjectView)docs.get(i)).getName());
307
                initDialog = new GeorefLauncherDialog(viewList, DEFAULT_DEGREE, this);
308
                PluginServices.getMDIManager().addWindow(initDialog);
309
        }
310
        
311
        /**
312
         * Captura de eventos de los botones Aceptar y Cancelar del cuadro
313
         * inicial con las propiedades de georreferenciaci?n.
314
         */
315
        public void actionButtonPressed(ButtonsPanelEvent e) {
316
                // Bot?n de Aceptar
317
                if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
318
                        if(initDialog != null) {
319
                                selectedView = initDialog.getSelectedView();
320
                                lyrToGeoref = initDialog.getLayer();
321
                                //Inicializamos la matriz de transformaci?n en la imagen que vamos a georreferenciar para
322
                                //que nos de coordenadas pixel
323
                                //lyrToGeoref.setAffineTransform(new AffineTransform(1, 0, 0, -1, 0, 0));
324
                                if(lyrToGeoref == null) 
325
                                        RasterToolsUtil.messageBoxError("layer_not_loaded", this);
326
                                else {
327
                                        PluginServices.getMDIManager().closeWindow(initDialog);
328
                                        if(initDialog.getType() == Georeferencing.WITH_MAP)
329
                                                initWindowsWithMap();
330
                                        if(initDialog.getType() == Georeferencing.WITHOUT_MAP)
331
                                                initWindowsWithoutMap();
332
                                }
333
                        }
334
                }
335
                // Bot?n de Cerrar
336
                if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
337
                        if(initDialog != null)
338
                                PluginServices.getMDIManager().closeWindow(initDialog);
339
                }
340
        }
341

    
342
        /**
343
         * Evento de finalizaci?n de la capa con el 
344
         * cursor gr?fico que controla el ?rea de la miniimagen asociada
345
         */
346
        public void endAction(ToolEvent ev) {
347
                if(ev.getSource() instanceof ZoomCursorGraphicLayer) {
348
                        try {        
349
                                Rectangle2D ext = null;
350
                                if(viewRaster != null) {
351
                                        ext = viewRaster.getCursorAdjustedWorldCoordinates(zoomPixel.getCanvasWidth(), zoomPixel.getCanvasHeight());
352
                                        zoomRasterRequestManager.request(ext);
353
                                }
354
                                if(viewMap != null) {
355
                                        ext = viewMap.getCursorAdjustedWorldCoordinates(zoomMap.getCanvasWidth(), zoomMap.getCanvasHeight());
356
                                        zoomMapRequestManager.request(ext);
357
                                }
358
                        } catch (InvalidRequestException e1) {
359
                                RasterToolsUtil.messageBoxError("error_setview_preview", this);
360
                        }
361
                }
362
        }
363
}