Statistics
| Revision:

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

History | View | Annotate | Download (11.7 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.Rectangle2D;
22
import java.awt.image.BufferedImage;
23
import java.util.ArrayList;
24

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

    
42
import com.iver.andami.PluginServices;
43
import com.iver.andami.ui.mdiFrame.MDIFrame;
44
import com.iver.andami.ui.mdiManager.IWindow;
45
import com.iver.andami.ui.mdiManager.WindowInfo;
46
import com.iver.cit.gvsig.ProjectExtension;
47
import com.iver.cit.gvsig.fmap.MapControl;
48
import com.iver.cit.gvsig.fmap.ViewPort;
49
import com.iver.cit.gvsig.project.Project;
50
import com.iver.cit.gvsig.project.documents.view.ProjectView;
51
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
52
import com.iver.cit.gvsig.project.documents.view.gui.IView;
53

    
54
/**
55
 * Clase principal para la georreferenciaci?n. Se encarga de la inicializaci?n
56
 * de la funcionalidad. Las acciones a realizar son:
57
 * <UL>
58
 * <LI>Lanzamiento del dialogo de inicializaci?n.</LI>
59
 * <LI>Carga de la capa raster a georreferenciar.</LI>
60
 * <LI>Creaci?n de las ventanas si no han sido creadas previamente.</LI>
61
 * <LI>Asignaci?n de tama?os y posiciones a las ventanas.</LI>
62
 * </UL>
63
 * 26/12/2007
64
 * @author Nacho Brodin (nachobrodin@gmail.com)
65
 */
66
public class Georeferencing implements ButtonsPanelListener, ToolListener {
67
        public static final int               UNDEFINED                      = -1;
68
        //Algoritmo
69
        public static final int               POLYNOMIAL                     = 0;
70
        public static final int               AFFINE                         = 1;
71
        
72
        //Tipo de georreferenciacion
73
        public static final int               WITH_MAP                       = 0;
74
        public static final int               WITHOUT_MAP                    = 1;
75
        
76
        private static int                    DEFAULT_DEGREE                 = 3;
77
        
78
        private GeorefLauncherDialog          initDialog                     = null;
79
        private FLyrRasterSE                  lyrToGeoref                    = null;
80
        private String                        selectedView                   = null;
81
        private static GCPTablePanel          table                          = null;
82
        
83
        private static final int              smallWindowsHeight             = 100;
84
        private static final double           smallWindowsWidthPercent       = 0.5;
85
        
86
        private static ViewDialog             viewRaster                     = null;
87
        private static ViewDialog             viewMap                        = null;
88
        
89
        private static ZoomMapDialog          zoomMap                        = null;
90
        private static ZoomRasterDialog       zoomPixel                      = null;
91
        
92
        private ViewRasterRequestManager      viewRasterRequestManager       = null;
93
        private ViewMapRequestManager         viewMapRequestManager          = null;
94
        
95
        private ViewRasterRequestManager      zoomRasterRequestManager       = null;
96
        private ViewMapRequestManager         zoomMapRequestManager          = null;
97
        
98
        public Georeferencing() {
99
                
100
        }
101
                
102
        public void initialize() {
103
                launcher();
104
        }
105
        
106
        /**
107
         * Colocaci?n inicial de las ventanas del espacio de trabajo. Carga la configuraci?n
108
         * de ventanas con cartograf?a de referencia.
109
         */
110
        private void initWindowsWithoutMap() {
111
                
112
        }
113
        
114
        /**
115
         * Colocaci?n inicial de las ventanas del espacio de trabajo. Carga la configuraci?n
116
         * de ventanas con cartograf?a de referencia.
117
         */
118
        private void initWindowsWithMap() {
119
                MDIFrame p = (MDIFrame)PluginServices.getMainFrame();
120
                int wViews = p.getWidth() >> 1;
121
                int hViews = p.getHeight() - 178 - smallWindowsHeight;
122
                int posYViews = p.getHeight() - 138 - smallWindowsHeight;
123
                int smallWindowsWidth = (int)(p.getWidth() * (smallWindowsWidthPercent * 0.5));
124
                int tableWidth = (int)(p.getWidth() * (1 - smallWindowsWidthPercent));
125
                
126
                //Si hay ventanas de georreferenciaci?n abiertas las cerramos
127
                if(zoomMap != null)
128
                        PluginServices.getMDIManager().closeWindow(zoomMap);
129
                if(zoomPixel != null)
130
                        PluginServices.getMDIManager().closeWindow(zoomPixel);
131
                if(viewRaster != null)
132
                        PluginServices.getMDIManager().closeWindow(viewRaster);
133
                if(viewRaster != null)
134
                        PluginServices.getMDIManager().closeWindow(viewMap);
135
                if(table != null)
136
                        PluginServices.getMDIManager().closeWindow(table);
137
                
138
                MapControl mapControl = null;
139
                IWindow[] windowList = PluginServices.getMDIManager().getAllWindows();
140
                for (int i = 0; i < windowList.length; i++) {
141
                        if(windowList[i] instanceof IView && windowList[i].getWindowInfo().getTitle().endsWith(": " + selectedView)) 
142
                                mapControl = ((IView)windowList[i]).getMapControl();
143
                }
144
                
145
                if(mapControl == null) {
146
                        RasterToolsUtil.messageBoxError("error_lookingfor_view", this);
147
                        return;
148
                }
149
                
150
                table = new GCPTablePanel(smallWindowsWidth, posYViews, tableWidth, smallWindowsHeight);
151
                
152
                //Inicializaci?n de la ventana con la vista mapa
153
                viewMap = new ViewDialog(0, 0, wViews, hViews, this);
154
                viewMap.setShowInfo(true);
155
                viewMapRequestManager = new ViewMapRequestManager(viewMap, mapControl);
156
                viewMap.setExtensionRequest(viewMapRequestManager);
157
                
158
                //Inicializaci?n de la ventana con la vista raster
159
                viewRaster = new ViewDialog(wViews, 0, wViews, hViews, this);
160
                viewRaster.setShowInfo(true);
161
                viewRasterRequestManager = new ViewRasterRequestManager(viewRaster, lyrToGeoref);
162
                viewRaster.setExtensionRequest(viewRasterRequestManager);
163
                
164
                //Inicializaci?n de la ventana del zoom de la vista raster
165
                zoomPixel = new ZoomRasterDialog(smallWindowsWidth + tableWidth, posYViews, smallWindowsWidth, smallWindowsHeight);
166
                zoomRasterRequestManager = new ViewRasterRequestManager(zoomPixel, lyrToGeoref);
167
                zoomPixel.setExtensionRequest(zoomRasterRequestManager);
168
                
169
                //Inicializaci?n de la ventana del zoom de la vista de referencia
170
                zoomMap = new ZoomMapDialog(0, posYViews, smallWindowsWidth, smallWindowsHeight);
171
                zoomMapRequestManager = new ViewMapRequestManager(zoomMap, mapControl);
172
                zoomMap.setExtensionRequest(zoomMapRequestManager);
173
                
174
                /*GeoPoint gp = new GeoPoint(new Point2D.Double(100, 100), null);
175
                GPGraphic gpg = new GPGraphic(GPGraphic.PIXEL, gp);
176
                view.getPixelGraphicLayer().addGeoPoint(gpg);
177
                */
178
                
179
                //A?adimos las ventanas a gvSIG
180
                PluginServices.getMDIManager().addWindow(viewRaster);
181
                PluginServices.getMDIManager().addWindow(viewMap);
182
                PluginServices.getMDIManager().addWindow(table);
183
                PluginServices.getMDIManager().addWindow(zoomMap);
184
                PluginServices.getMDIManager().addWindow(zoomPixel);
185
                
186
                try {
187
                        //Inicializamos la vista de mapa
188
                        viewMapRequestManager.initRequest(mapControl.getMapContext().getLayers().getFullExtent());
189
                        viewMap.setZoomCursorSize(zoomMap.getCanvasWidth() / 3, zoomMap.getCanvasHeight() / 3);
190

    
191
                        //Inicializamos la vista de raster
192
                        viewRasterRequestManager.initRequest(lyrToGeoref.getFullExtent());
193
                        viewRaster.setZoomCursorSize(zoomPixel.getCanvasWidth() / 3, zoomPixel.getCanvasHeight() / 3);
194
                        
195
                        //Inicializamos el zoom de mapa
196
                        Rectangle2D ext = viewMap.getCursorAdjustedWorldCoordinates(zoomMap.getCanvasWidth(), zoomMap.getCanvasHeight());
197
                        zoomMapRequestManager.initRequest(mapControl.getMapContext().getFullExtent());
198
                        zoomMapRequestManager.request(new Rectangle2D.Double(ext.getX(), 
199
                                                                                        viewRaster.getViewExtent().getMaxY() - (viewRaster.getViewExtent().getMinY() - ext.getY()), 
200
                                                                                        ext.getWidth(), 
201
                                                                                        ext.getHeight()));
202

    
203
                        //Inicializamos el zoom de raster
204
                        ext = viewRaster.getCursorAdjustedWorldCoordinates(zoomPixel.getCanvasWidth(), zoomPixel.getCanvasHeight());
205
                        zoomRasterRequestManager.initRequest(lyrToGeoref.getFullExtent());
206
                        zoomRasterRequestManager.request(new Rectangle2D.Double(ext.getX(), 
207
                                                                                        viewRaster.getViewExtent().getMaxY() - (viewRaster.getViewExtent().getMinY() - ext.getY()), 
208
                                                                                        ext.getWidth(), 
209
                                                                                        ext.getHeight()));
210
                        
211
                } catch (InvalidRequestException e1) {
212
                        RasterToolsUtil.messageBoxError("error_setview_preview", this);
213
                }catch (Exception e1) {
214
                        RasterToolsUtil.messageBoxError("error_setview_preview", this);
215
                }
216
                
217
        }
218
        
219
        /**
220
         * Lanzador del cuadro de inicializaci?n de propiedades de georreferenciaci?n.
221
         */
222
        private void launcher() {
223
                Project p = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
224
                ArrayList docs = p.getDocumentsByType(ProjectViewFactory.registerName);
225
                String[] viewList = new String[docs.size()];
226
                for (int i = 0; i < docs.size(); i++) 
227
                        viewList[i] = (((ProjectView)docs.get(i)).getName());
228
                initDialog = new GeorefLauncherDialog(viewList, DEFAULT_DEGREE, this);
229
                PluginServices.getMDIManager().addWindow(initDialog);
230
        }
231
        
232
        /**
233
         * Captura de eventos de los botones Aceptar y Cancelar del cuadro
234
         * inicial con las propiedades de georreferenciaci?n.
235
         */
236
        public void actionButtonPressed(ButtonsPanelEvent e) {
237
                // Bot?n de Aceptar
238
                if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
239
                        if(initDialog != null) {
240
                                selectedView = initDialog.getSelectedView();
241
                                lyrToGeoref = initDialog.getLayer();
242
                                if(lyrToGeoref == null) 
243
                                        RasterToolsUtil.messageBoxError("layer_not_loaded", this);
244
                                else {
245
                                        PluginServices.getMDIManager().closeWindow(initDialog);
246
                                        if(initDialog.getType() == Georeferencing.WITH_MAP)
247
                                                initWindowsWithMap();
248
                                        if(initDialog.getType() == Georeferencing.WITHOUT_MAP)
249
                                                initWindowsWithoutMap();
250
                                }
251
                        }
252
                }
253
                // Bot?n de Cerrar
254
                if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
255
                        if(initDialog != null)
256
                                PluginServices.getMDIManager().closeWindow(initDialog);
257
                }
258
        }
259

    
260
        /**
261
         * Evento de finalizaci?n de la capa con el 
262
         * cursor gr?fico que controla el ?rea de la miniimagen asociada
263
         */
264
        public void endAction(ToolEvent ev) {
265
                if(ev.getSource() instanceof ZoomCursorGraphicLayer) {
266
                        try {                                
267
                                Rectangle2D ext = viewRaster.getCursorAdjustedWorldCoordinates(zoomPixel.getCanvasWidth(), zoomPixel.getCanvasHeight());
268
                                zoomRasterRequestManager.request(ext);
269
                                ext = viewMap.getCursorAdjustedWorldCoordinates(zoomMap.getCanvasWidth(), zoomMap.getCanvasHeight());
270
                                zoomMapRequestManager.request(ext);
271
                        } catch (InvalidRequestException e1) {
272
                                RasterToolsUtil.messageBoxError("error_setview_preview", this);
273
                        }
274
                }
275
        }
276
}