svn-gvsig-desktop / branches / v10 / extensions / DielmoOpenLidar / src / com / dielmo / gvsig / lidar / drivers / LiDARDriver.java @ 25419
History | View | Annotate | Download (12.7 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.drivers; |
51 |
|
52 |
import java.awt.geom.Point2D; |
53 |
import java.awt.geom.Rectangle2D; |
54 |
import java.io.File; |
55 |
import java.io.FileInputStream; |
56 |
import java.io.FileNotFoundException; |
57 |
import java.io.FileOutputStream; |
58 |
import java.io.IOException; |
59 |
import java.nio.ByteOrder; |
60 |
import java.nio.channels.FileChannel; |
61 |
import java.util.Properties; |
62 |
|
63 |
import com.dielmo.lidar.InicializeLidar; |
64 |
import com.dielmo.lidar.LASHeader_1X; |
65 |
import com.dielmo.lidar.LidarHeader; |
66 |
import com.dielmo.lidar.LidarPoint; |
67 |
import com.hardcode.driverManager.Driver; |
68 |
import com.hardcode.gdbms.driver.DriverUtilities; |
69 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
70 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
71 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
72 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
73 |
import com.hardcode.gdbms.engine.values.Value; |
74 |
import com.iver.cit.gvsig.fmap.core.FShape; |
75 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
76 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
77 |
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes; |
78 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
79 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
80 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
81 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
82 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
83 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
84 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
85 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
86 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
87 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
88 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
89 |
import com.iver.utiles.bigfile.BigByteBuffer2; |
90 |
|
91 |
/**
|
92 |
* Driver for managing original data LiDAR
|
93 |
*
|
94 |
* @author Oscar Garcia
|
95 |
*/
|
96 |
public class LiDARDriver implements VectorialFileDriver, ObjectDriver, BoundedShapes, |
97 |
IWriteable, ISpatialWriter { |
98 |
|
99 |
/**
|
100 |
* Driver name.
|
101 |
*/
|
102 |
public static final String driverName = "gvSIG LIDAR driver"; |
103 |
// Variables para el manejo del fichero LiDAR
|
104 |
|
105 |
/**
|
106 |
* LiDAR file.
|
107 |
*/
|
108 |
private File m_Fich; |
109 |
|
110 |
/**
|
111 |
* Temporal LiDAR file for writer. This is used in edition time.
|
112 |
*/
|
113 |
private File fTemp; |
114 |
|
115 |
/**
|
116 |
* 8 Kbytes buffer
|
117 |
*/
|
118 |
private BigByteBuffer2 bb;
|
119 |
|
120 |
/**
|
121 |
* Channel to read, write and manipulate the LiDAR file
|
122 |
*/
|
123 |
private FileChannel channel; |
124 |
|
125 |
/**
|
126 |
* management of input read data from LiDAR file.
|
127 |
*/
|
128 |
private FileInputStream fin; |
129 |
|
130 |
// informacion del LiDAR
|
131 |
/**
|
132 |
* fullExtent of LiDAR layer.
|
133 |
*/
|
134 |
private Rectangle2D fullExtent; |
135 |
|
136 |
/**
|
137 |
* Definition of one type of LiDAR point.
|
138 |
*/
|
139 |
private LidarPoint lp;
|
140 |
|
141 |
/**
|
142 |
* Definition of one type of LiDAR header.
|
143 |
*/
|
144 |
private LidarHeader hdr;
|
145 |
|
146 |
/**
|
147 |
* Description of fields LiDAR
|
148 |
*/
|
149 |
private FieldDescription[] fields; |
150 |
|
151 |
// Writer
|
152 |
/**
|
153 |
* Writer assigned to LiDAR
|
154 |
*/
|
155 |
private LiDARWriter lidarWriter = new LiDARWriter(); |
156 |
|
157 |
/**
|
158 |
* Temporal folder to work in edition mode.
|
159 |
*/
|
160 |
private static String tempDirectoryPath = System.getProperty("java.io.tmpdir"); |
161 |
|
162 |
/**
|
163 |
* Function that test if passed layer is controlled by driver LiDARDriver
|
164 |
*
|
165 |
* @param layer layer to test
|
166 |
* @return true if layer is controlled by driver LiDARDriver, false in other case
|
167 |
*/
|
168 |
public static boolean isLidarLayer(FLayer layer) { |
169 |
|
170 |
if (layer instanceof FLyrVect && layer.isAvailable()) { |
171 |
|
172 |
FLyrVect lyrVect = (FLyrVect) layer; |
173 |
//ReadableVectorial rv = lyrVect.getSource();
|
174 |
//VectorialDriver driver = lyrVect.getSource().getDriver();
|
175 |
Driver driver = lyrVect.getSource().getDriver();
|
176 |
if(driver==null) |
177 |
return false; |
178 |
if (driver.getName().compareTo(driverName) == 0) { |
179 |
return true; |
180 |
} |
181 |
} |
182 |
return false; |
183 |
} |
184 |
|
185 |
/**
|
186 |
* return the LidarHeader of this layer
|
187 |
*/
|
188 |
public LidarHeader getLidarHeader() {
|
189 |
return hdr;
|
190 |
} |
191 |
|
192 |
/**
|
193 |
* return a new instance of LidarPoint type of this layer
|
194 |
*/
|
195 |
public LidarPoint getLidarPoint() {
|
196 |
return lp;
|
197 |
} |
198 |
|
199 |
// VectorialFileDriver Methods
|
200 |
public void initialize() throws IOException { |
201 |
|
202 |
File file = new File(m_Fich.getAbsolutePath()); |
203 |
hdr = InicializeLidar.InizializeLidarHeader(file); |
204 |
|
205 |
if(hdr == null) |
206 |
throw new IOException(); |
207 |
|
208 |
lp = InicializeLidar.InizializeLidarPoint(file); |
209 |
|
210 |
if(lp == null) |
211 |
throw new IOException(); |
212 |
|
213 |
// la informacion de fullExtent la contiene la cabecera
|
214 |
fullExtent = new Rectangle2D.Double(hdr.getMinX(), hdr.getMinY(), |
215 |
(hdr.getMaxX()-hdr.getMinX()), |
216 |
(hdr.getMaxY()-hdr.getMinY())); |
217 |
|
218 |
// obtiene la estructura de tabla
|
219 |
setFields(); |
220 |
} |
221 |
|
222 |
/*
|
223 |
* (non-Javadoc)
|
224 |
*
|
225 |
* @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
|
226 |
*/
|
227 |
public boolean accept(File f) { |
228 |
return ((f.getName().toUpperCase().endsWith("LAS")) || (f.getName().toUpperCase().endsWith("BIN"))); |
229 |
} |
230 |
|
231 |
public void close() throws IOException { |
232 |
|
233 |
/* IOException ret = null;
|
234 |
|
235 |
try {
|
236 |
channel.close();
|
237 |
} catch (IOException e) {
|
238 |
ret = e;
|
239 |
} finally {
|
240 |
try {
|
241 |
fin.close();
|
242 |
} catch (IOException e1) {
|
243 |
ret = e1;
|
244 |
}
|
245 |
}
|
246 |
|
247 |
if (ret != null) {
|
248 |
throw ret;
|
249 |
}
|
250 |
else // Si todo ha ido bien, preparamos para liberar memoria.
|
251 |
{
|
252 |
bb = null;
|
253 |
}
|
254 |
*/
|
255 |
} |
256 |
|
257 |
public File getFile() { |
258 |
return m_Fich;
|
259 |
} |
260 |
|
261 |
/*
|
262 |
* (non-Javadoc)
|
263 |
*
|
264 |
* @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
|
265 |
*/
|
266 |
public void open(File f) throws IOException { |
267 |
m_Fich = f; |
268 |
|
269 |
fin = new FileInputStream(f); |
270 |
|
271 |
// Open the file and then get a channel from the stream
|
272 |
channel = fin.getChannel(); |
273 |
|
274 |
// Get the file's size and then map it into memory
|
275 |
// bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
|
276 |
bb = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY); |
277 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
278 |
} |
279 |
|
280 |
/*
|
281 |
* (non-Javadoc)
|
282 |
*
|
283 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
284 |
*/
|
285 |
public DriverAttributes getDriverAttributes() {
|
286 |
return null; |
287 |
} |
288 |
|
289 |
/* (non-Javadoc)
|
290 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
|
291 |
*/
|
292 |
public Rectangle2D getFullExtent() throws IOException { |
293 |
|
294 |
return fullExtent;
|
295 |
} |
296 |
|
297 |
/* (non-Javadoc)
|
298 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
|
299 |
*/
|
300 |
public IGeometry getShape(int index) { |
301 |
|
302 |
Point2D.Double p = null; |
303 |
p = lp.readPoint2D(bb, hdr, index); |
304 |
return ShapeFactory.createPoint2D(p.getX(), p.getY());
|
305 |
} |
306 |
|
307 |
public int getShapeCount() throws IOException { |
308 |
return (int)hdr.getNumPointsRecord(); |
309 |
} |
310 |
|
311 |
/*
|
312 |
* (non-Javadoc)
|
313 |
*
|
314 |
* @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
|
315 |
*/
|
316 |
public int getShapeType() { |
317 |
return FShape.POINT;
|
318 |
} |
319 |
|
320 |
/**
|
321 |
* Indica si el fichero se puede modificar.
|
322 |
*/
|
323 |
public boolean isWritable() { |
324 |
return m_Fich.canWrite();
|
325 |
} |
326 |
|
327 |
/* (non-Javadoc)
|
328 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
|
329 |
*/
|
330 |
public void reload() throws DriverException, IOException { |
331 |
|
332 |
/*
|
333 |
* Codigo para cerrar los ficheros antes de intentar recargarlos
|
334 |
*/
|
335 |
IOException ret = null; |
336 |
|
337 |
try {
|
338 |
channel.close(); |
339 |
} catch (IOException e) { |
340 |
ret = e; |
341 |
} finally {
|
342 |
try {
|
343 |
fin.close(); |
344 |
} catch (IOException e1) { |
345 |
ret = e1; |
346 |
} |
347 |
} |
348 |
|
349 |
if (ret != null) { |
350 |
throw ret;
|
351 |
} else {
|
352 |
|
353 |
// Si todo ha ido bien, preparamos para liberar memoria.
|
354 |
bb = null;
|
355 |
} |
356 |
|
357 |
open(m_Fich); |
358 |
initialize(); |
359 |
} |
360 |
|
361 |
/*
|
362 |
* (non-Javadoc)
|
363 |
*
|
364 |
* @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
|
365 |
*/
|
366 |
public String getName() { |
367 |
return driverName;
|
368 |
} |
369 |
|
370 |
// ObjectDriver Methods
|
371 |
public int[] getPrimaryKeys() throws DriverException { |
372 |
// TODO Auto-generated method stub
|
373 |
return null; |
374 |
} |
375 |
|
376 |
public void write(DataWare dataWare) throws DriverException { |
377 |
// TODO Auto-generated method stub
|
378 |
|
379 |
} |
380 |
|
381 |
public void setDataSourceFactory(DataSourceFactory dsf) { |
382 |
// TODO Auto-generated method stub
|
383 |
} |
384 |
|
385 |
public int getFieldCount() throws DriverException { |
386 |
return fields.length;
|
387 |
} |
388 |
|
389 |
public String getFieldName(int fieldId) throws DriverException { |
390 |
return fields[fieldId].getFieldName();
|
391 |
} |
392 |
|
393 |
public int getFieldType(int i) throws DriverException { |
394 |
return fields[i].getFieldType();
|
395 |
} |
396 |
|
397 |
public Value getFieldValue(long rowIndex, int fieldId) throws DriverException { |
398 |
return lp.getFieldValueByIndex(bb, fieldId, hdr, rowIndex);
|
399 |
} |
400 |
|
401 |
public int getFieldWidth(int i) throws DriverException { |
402 |
return fields[i].getFieldLength();
|
403 |
} |
404 |
|
405 |
public long getRowCount() throws DriverException { |
406 |
return (int)hdr.getNumPointsRecord(); |
407 |
} |
408 |
|
409 |
private void setFields(){ |
410 |
fields = lp.getFieldDescription(); |
411 |
} |
412 |
|
413 |
//IWriteable Methods
|
414 |
public IWriter getWriter() {
|
415 |
return this; |
416 |
} |
417 |
|
418 |
//ISpatialWriter Methods
|
419 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
420 |
return lidarWriter.canWriteGeometry(gvSIGgeometryType);
|
421 |
} |
422 |
|
423 |
public boolean canAlterTable() { |
424 |
return false; |
425 |
} |
426 |
|
427 |
public boolean canSaveEdits() { |
428 |
return lidarWriter.canSaveEdits();
|
429 |
} |
430 |
|
431 |
public boolean canWriteAttribute(int sqlType) { |
432 |
return lidarWriter.canWriteAttribute(sqlType);
|
433 |
} |
434 |
|
435 |
public String getCapability(String capability) { |
436 |
return lidarWriter.getCapability(capability);
|
437 |
} |
438 |
|
439 |
public void setCapabilities(Properties capabilities) { |
440 |
lidarWriter.setCapabilities(capabilities); |
441 |
} |
442 |
|
443 |
public ITableDefinition getTableDefinition() {
|
444 |
return lidarWriter.getTableDefinition();
|
445 |
} |
446 |
|
447 |
public void initialize(ITableDefinition tableDefinition) |
448 |
throws EditionException {
|
449 |
|
450 |
// escribe en un temporal el fichero.
|
451 |
int aux = (int)(Math.random() * 1000); |
452 |
String extension;
|
453 |
if(hdr instanceof LASHeader_1X) |
454 |
extension= ".las";
|
455 |
else
|
456 |
extension = ".bin";
|
457 |
|
458 |
fTemp = new File(tempDirectoryPath + "/tmpLidar" + aux + extension); |
459 |
lidarWriter.setFile(fTemp); |
460 |
lidarWriter.setHeaderAndPoint(hdr, lp); |
461 |
lidarWriter.initialize(tableDefinition); |
462 |
} |
463 |
|
464 |
public boolean isWriteAll() { |
465 |
return true; |
466 |
} |
467 |
|
468 |
/**
|
469 |
* Trabajo a realizar antes de empezar a recorrer las geometrias.
|
470 |
*/
|
471 |
public void preProcess() throws EditionException { |
472 |
lidarWriter.preProcess(); |
473 |
} |
474 |
|
475 |
/**
|
476 |
* Trabajo a realizar durante recorremos las geometrias.
|
477 |
*/
|
478 |
public void process(IRowEdited row) throws EditionException { |
479 |
lidarWriter.process(row); |
480 |
} |
481 |
|
482 |
/**
|
483 |
* Trabajo a realizar despues de terminar de recorrer las geometrias.
|
484 |
*/
|
485 |
public void postProcess() throws EditionException { |
486 |
lidarWriter.postProcess(); |
487 |
|
488 |
try {
|
489 |
|
490 |
// close();
|
491 |
FileChannel fcinLidar = new FileInputStream(fTemp).getChannel(); |
492 |
FileChannel fcoutLidar = new FileOutputStream(m_Fich).getChannel(); |
493 |
DriverUtilities.copy(fcinLidar, fcoutLidar); |
494 |
|
495 |
// Borramos los temporales
|
496 |
fTemp.delete(); |
497 |
//reload();
|
498 |
|
499 |
} catch (FileNotFoundException e) { |
500 |
throw new EditionException(e); |
501 |
} catch (IOException e) { |
502 |
throw new EditionException(e); |
503 |
} |
504 |
} |
505 |
|
506 |
// BoundedShapes Methods
|
507 |
public Rectangle2D getShapeBounds(int index) throws IOException { |
508 |
|
509 |
Point2D p = new Point2D.Double(); |
510 |
Rectangle2D BoundingBox = new Rectangle2D.Double(); |
511 |
|
512 |
p = lp.readPoint2D(bb, hdr, index); |
513 |
BoundingBox = new Rectangle2D.Double(p.getX() - 0.1, |
514 |
p.getY() - 0.1, 0.2, 0.2); |
515 |
|
516 |
return BoundingBox;
|
517 |
} |
518 |
|
519 |
public int getShapeType(int index) { |
520 |
|
521 |
return FShape.POINT;
|
522 |
} |
523 |
} |