Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / main / GeoPointsPersistence.java @ 18530

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

    
21
import java.awt.geom.Point2D;
22
import java.io.BufferedReader;
23
import java.io.BufferedWriter;
24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.FileNotFoundException;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.io.InputStreamReader;
30
import java.io.OutputStreamWriter;
31
import java.util.ArrayList;
32

    
33
import javax.swing.JFileChooser;
34

    
35
import org.gvsig.georeferencing.ui.table.GCPTablePanel;
36
import org.gvsig.georeferencing.ui.zoom.layers.GPGraphic;
37
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
38
import org.gvsig.raster.dataset.IRasterDataSource;
39
import org.gvsig.raster.datastruct.GeoPoint;
40
import org.gvsig.raster.util.ExtendedFileFilter;
41
import org.gvsig.raster.util.RasterToolsUtil;
42

    
43

    
44
/**
45
 * Operaciones de lectura y escritura en disco de puntos de control.
46
 * 
47
 * 26/12/2007
48
 * @author Nacho Brodin nachobrodin@gmail.com
49
 */
50
public class GeoPointsPersistence {
51
        
52
        /**
53
         * Salva a rmf la lista de puntos
54
         * @param pointList
55
         * @param parent
56
         * @param dataset
57
         */
58
        public void saveToRMF(ArrayList pointList, IRasterDataSource dataset) {
59
                try {
60
                        if(!RasterToolsUtil.messageBoxYesOrNot("sobreescribir_puntos", null))
61
                                return;
62
                        
63
                        Object[] gpgList = pointList.toArray();
64
                        GeoPoint[] gpList = new GeoPoint[gpgList.length];
65
                        for (int i = 0; i < gpList.length; i++) 
66
                                gpList[i] = ((GPGraphic)gpgList[i]).getGeoPoint();
67
                        dataset.saveGeoPointsToRmf(gpList);
68
                } catch (IOException e) {
69
                        RasterToolsUtil.messageBoxYesOrNot("error_salvando_rmf", null);
70
                }
71
        }
72
        
73
        /**
74
         * Carga desde el rmf asociado los puntos de control guardados si los tiene
75
         * @param pointList
76
         * @param parent
77
         * @param dataset
78
         */
79
        public void loadFromRMF(IRasterDataSource dataset, LayersPointManager layerPointsManager, GCPTablePanel tablePanel) {
80
                try {
81
                        if(!RasterToolsUtil.messageBoxYesOrNot("eliminar_puntos", null))
82
                                return;
83
                        
84
                        GeoPoint[] pointList = dataset.loadGeoPointsFromRmf();
85
                        for (int i = 0; i < pointList.length; i++) {
86
                                long id = layerPointsManager.addPoint(pointList[i].mapPoint, pointList[i].pixelPoint);
87
                                tablePanel.addRow(true, pointList[i].mapPoint, pointList[i].pixelPoint, 0, 0, 0, 0, id);
88
                        }
89
                        layerPointsManager.calcPointsNumeration(tablePanel.getTable());
90
                } catch (IOException e) {
91
                        RasterToolsUtil.messageBoxYesOrNot("error_salvando_rmf", null);
92
                } catch (NotInitializeException e) {
93
                        RasterToolsUtil.messageBoxYesOrNot("table_not_initialize", tablePanel);
94
                }
95
        }
96
        
97
        /**
98
         * Funci?n que se ejecuta al pulsar el bot?n de export a ascii
99
         */
100
        public void exportToCSV(ArrayList pointList, boolean error) {
101
                for (int i = 0; i < pointList.size(); i++) {
102
                        if(!(pointList.get(i) instanceof GPGraphic))
103
                                return;
104
                }
105

    
106
                JFileChooser chooser = new JFileChooser();
107
                chooser.setDialogTitle(RasterToolsUtil.getText(this, "seleccionar_fichero"));
108

    
109
                int returnVal = chooser.showSaveDialog(null);
110
                if(returnVal == JFileChooser.APPROVE_OPTION){
111
                        String fName = chooser.getSelectedFile().toString();
112
                        if(!fName.endsWith(".csv"))
113
                                fName = fName + ".csv";
114
                        saveCSVPointList(fName, pointList, error);
115
                }
116
        }
117

    
118
        /**
119
         * Funci?n que se ejecuta al pulsar el bot?n de importar desde CSV
120
         */
121
        public void importFromCSV(LayersPointManager layerPointsManager, GCPTablePanel tablePanel) {
122
                JFileChooser chooser = new JFileChooser();
123
                chooser.setDialogTitle(RasterToolsUtil.getText(this, "seleccionar_fichero"));
124
                ExtendedFileFilter fileFilter = null;
125

    
126
                fileFilter = new ExtendedFileFilter();
127
                fileFilter.addExtension("csv");
128
                fileFilter.setDescription("CSV File");
129
                        
130
                chooser.addChoosableFileFilter(fileFilter);
131

    
132
                if (fileFilter != null)
133
                        chooser.setFileFilter(fileFilter);
134

    
135
                int returnVal = chooser.showOpenDialog(null);
136
                if (returnVal == JFileChooser.APPROVE_OPTION) {
137
                        File f = chooser.getSelectedFile();
138
                        if(!f.exists()) {
139
                                RasterToolsUtil.messageBoxError("error_file_not_found", null);
140
                                return;
141
                        }
142
                        if(!RasterToolsUtil.messageBoxYesOrNot("eliminar_puntos", null))
143
                                return;
144
                        loadCSVPointList(f.getAbsolutePath(), layerPointsManager, tablePanel);                        
145
                }
146
        }
147
        
148
        /**
149
         * Crea el fichero Ascii con la lista de puntos y la salva a disco
150
         * @param file
151
         */
152
        public void saveCSVPointList(String file, ArrayList pointList, boolean error){
153
                try{
154
                        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
155
                        if(error)
156
                                out.write("\"Pt\",\"X\",\"Y\",\"X'\",\"Y'\",\"ErrX\",\"ErrY\",\"RMS\"\n");
157
                        else
158
                                out.write("\"Pt\",\"X\",\"Y\",\"X'\",\"Y'\"\n");
159

    
160
                        for(int i = 0; i < pointList.size(); i++) {
161
                                GeoPoint geoPoint =  (GeoPoint)((GPGraphic)pointList.get(i)).getGeoPoint();
162
                                String point = i + "," +
163
                                                                geoPoint.pixelPoint.getX() + "," +
164
                                                                geoPoint.pixelPoint.getY() + "," +
165
                                                                geoPoint.mapPoint.getX() + "," +
166
                                                                geoPoint.mapPoint.getY();
167
                                String errorTxt = "";
168
                                try{
169
                                        errorTxt = geoPoint.getErrorX() + "," + geoPoint.getErrorY() + "," + geoPoint.getRms();
170
                                        if(error)
171
                                                out.write(point + "," + errorTxt + "\n");
172
                                        else
173
                                                out.write(point + "\n");
174
                                }catch(ArrayIndexOutOfBoundsException ex){
175
                                        out.write(point + "\n");
176
                                }
177
                        }
178
                        out.close();
179
                }catch(FileNotFoundException ex){
180
                        //No salvamos el csv
181
                }catch(IOException ex){
182
                        //No salvamos el csv
183
                }
184
        }
185
        
186
        /**
187
         * Crea la lista de puntos a partir de un fichero CSV
188
         * @param file
189
         */
190
        private void loadCSVPointList(String file, LayersPointManager layerPointsManager, GCPTablePanel tablePanel){
191
                try{
192
                        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
193
                        String line = in.readLine();
194
                        int nPoint = 0;
195
                        while(line != null) {
196
                                if(nPoint == 0) {
197
                                        if(!line.equals("\"Pt\",\"X\",\"Y\",\"X'\",\"Y'\",\"ErrX\",\"ErrY\",\"RMS\"") && 
198
                                                        !line.equals("\"Pt\",\"X\",\"Y\",\"X'\",\"Y'\"") && 
199
                                                        !line.equals("\"Pt\",\"X\",\"Y\",\"X'\",\"Y'\",\"Z\",\"ErrX\",\"ErrY\",\"RMS\"") &&
200
                                                        !line.equals("\"Pt\",\"X\",\"Y\",\"X'\",\"Y'\",\"Z\"")) {
201
                                                RasterToolsUtil.messageBoxError("error_point_file", null);
202
                                                return;
203
                                        } else {
204
                                                layerPointsManager.removeAllPoints();
205
                                                tablePanel.removeAllPoints();
206
                                        }
207
                                } else {
208
                                        double x = 0D, y = 0D, xx = 0D, yy = 0D;
209
                                        String[] tokens = line.split(",");
210
                                    for(int tok = 0; tok < tokens.length; tok++){
211
                                            try{
212
                                                    if(tok == 1)
213
                                                            x = Double.parseDouble(tokens[tok]);
214
                                                    if(tok == 2)
215
                                                            y = Double.parseDouble(tokens[tok]);
216
                                                    if(tok == 3)
217
                                                            xx = Double.parseDouble(tokens[tok]);
218
                                                    if(tok == 4)
219
                                                            yy = Double.parseDouble(tokens[tok]);
220
                                            }catch(NumberFormatException ex){
221
                                                    break;
222
                                            }
223
                                    }
224
                                        
225
                                        if(x == 0 && y == 0 && xx == 0 && yy == 0){
226
                                                line = in.readLine();
227
                                                continue;
228
                                        }
229
        
230
                                    Point2D p = new Point2D.Double();
231
                                        p.setLocation(x, y);
232
                                        Point2D m = new Point2D.Double();
233
                                        m.setLocation(xx, yy);
234
                                        long id = layerPointsManager.addPoint(m, p);
235
                                        tablePanel.addRow(true, m, p, 0, 0, 0, 0, id);
236
                                }
237
                                nPoint++;
238
                                line = in.readLine();
239
                        }
240
                        in.close();
241
                        layerPointsManager.calcPointsNumeration(tablePanel.getTable());
242
                } catch(FileNotFoundException ex){
243
                        //No salvamos el csv
244
                } catch(IOException ex){
245
                        //No salvamos el csv
246
                } catch(NotInitializeException ex){
247
                        RasterToolsUtil.messageBoxYesOrNot("table_not_initialize", tablePanel);
248
                }
249
        }
250
}