Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / DielmoOpenLidar / src-test / com / dielmo / gvsig / lidar / test / LidarToXYZ.java @ 25419

History | View | Annotate | Download (5.14 KB)

1
/* DielmoOpenLiDAR
2
 *
3
 * Copyright (C) 2008 DIELMO 3D S.L. (DIELMO) and Infrastructures  
4
 * and Transports Department of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 *
21
 * For more information, contact:
22
 *
23
 * DIELMO 3D S.L.
24
 * Plaza Vicente Andr?s Estell?s 1 Bajo E
25
 * 46950 Xirivella, Valencia
26
 * SPAIN
27
 *   
28
 * +34 963137212
29
 * dielmo@dielmo.com
30
 * www.dielmo.com
31
 * 
32
 * or
33
 * 
34
 * Generalitat Valenciana
35
 * Conselleria d'Infraestructures i Transport
36
 * Av. Blasco Ib??ez, 50
37
 * 46010 VALENCIA
38
 * SPAIN
39
 *
40
 * +34 963862235
41
 * gvsig@gva.es
42
 * www.gvsig.gva.es
43
 */
44

    
45
/*
46
 * AUTHORS (In addition to DIELMO and CIT):
47
 *  
48
 */
49

    
50
package com.dielmo.gvsig.lidar.test;
51

    
52
import java.io.File;
53
import java.io.FileNotFoundException;
54
import java.io.FileWriter;
55
import java.io.IOException;
56

    
57
import javax.swing.JFileChooser;
58
import javax.swing.JOptionPane;
59

    
60
import org.cresques.cts.IProjection;
61

    
62
import com.dielmo.gvsig.lidar.drivers.LiDARDriver;
63
import com.hardcode.gdbms.engine.values.DoubleValue;
64
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
65
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
66
import com.iver.cit.gvsig.fmap.layers.FLayer;
67
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
68
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
69
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
70
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
71

    
72
/**
73
 * Crear un XYZ a partir de un fichero Lidar
74
 * 
75
 * @author Oscar Garcia Gonzalez
76
 */
77
public class LidarToXYZ{
78

    
79
        public static void main(String[] args){
80
                
81
                JFileChooser fileChooser = new JFileChooser();
82
                //fileChooser.addChoosableFileFilter(new GenericFileFilter("Archivos lidar", "*.las;*.bin"));
83
                File file;
84

    
85
                int result = fileChooser.showOpenDialog(null);
86

    
87
        if (result == JFileChooser.APPROVE_OPTION && (file = fileChooser.getSelectedFile()) != null) {
88
                
89
                IProjection projection = CRSFactory.getCRS("EPSG:23030");
90
                //IProjection projection = Project.getDefaultProjection();
91
                createXYZByLidarFile(file, projection);
92

    
93
        } else {
94

    
95
                // si se eligio fichero correctamente.
96
                if(result == JFileChooser.APPROVE_OPTION )
97
                        JOptionPane.showMessageDialog(null, "Selecciona un fichero Lidar en formato LAS o BIN");
98
        }
99
        }
100
        
101
        /**
102
         * escribe en formato XYZ un archivo lidar pasado por entrada.
103
         * @param file fichero lidar del que extraer la informacion.
104
         */
105
        public static void createXYZByLidarFile(File file, IProjection projection){
106
                
107
                DoubleValue valueX, valueY, valueZ;
108
                int k, posTableX = 0, posTableY=1, posTableZ=2;
109
                
110
                if(file.getAbsolutePath().toUpperCase().endsWith("LAS") || file.getAbsolutePath().toUpperCase().endsWith("BIN")){
111
                try {
112

    
113
                        LiDARDriver lidarDriver = new LiDARDriver();
114
                        lidarDriver.open(file);
115
                        lidarDriver.initialize();
116
                        
117
                        FLayer lidarLayer;
118
                        lidarLayer = LayerFactory.createLayer("lidarLayer", lidarDriver, projection);
119
                        
120
                        ReadableVectorial rvLidar = ((FLyrVect)lidarLayer).getSource();
121
                        SelectableDataSource dsLidar = rvLidar.getRecordset();
122
                        rvLidar.start();
123
                                
124
                        String[] fd = dsLidar.getFieldNames();
125
                        for(k=0;k<fd.length;k++){
126
                                
127
                                if(fd[k].equals("X"))
128
                                        posTableX = k;
129
                                else if(fd[k].equals("Y"))
130
                                        posTableY = k;
131
                                else if(fd[k].equals("Z"))
132
                                        posTableZ = k;
133
                        }
134
                        
135
                        
136
                        FileWriter out = new FileWriter(file+".xyz");
137
                                int rvlidarCount = rvLidar.getShapeCount();
138
                                for(k=0;k<rvlidarCount;k++){
139
                                        
140
                                        valueX = (DoubleValue)dsLidar.getFieldValue(k, posTableX);
141
                                        valueY = (DoubleValue)dsLidar.getFieldValue(k, posTableY);
142
                                        valueZ = (DoubleValue)dsLidar.getFieldValue(k, posTableZ);
143
                                        
144
                                        out.write(valueX.getValue() + " " + valueY.getValue() + " " + valueZ.getValue() + "\n" );
145
                                }
146

    
147
                                out.close();
148
                                rvLidar.stop();
149
                                
150
                        } catch (DriverIOException e) {
151
                                // TODO Auto-generated catch block
152
                                e.printStackTrace();
153
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
154
                                // TODO Auto-generated catch block
155
                                e.printStackTrace();
156
                        } catch (FileNotFoundException e) {
157
                                // TODO Auto-generated catch block
158
                                e.printStackTrace();
159
                        } catch (IOException e) {
160
                                // TODO Auto-generated catch block
161
                                e.printStackTrace();
162
                        }
163
        }
164
        }
165
        
166
}