libLAS Python Tutorial

This basic tutorial explains how to use libLAS to read and write LIDAR data encoded in LAS file format from Python.

Reading

1. Reading LAS data in Python is a simple as opening the file with the file.File class and using the iterator to chug through the points.

>>> from liblas import file
>>> f = file.File('file.las',mode='r')
>>> for p in f:
...     print 'X,Y: ', p.x, p.y
  1. You can also read specific points from a file:
>>> from liblas import file
>>> f = file.File('file.las', mode='r')
>>> p = f.read(0)
>>> p
<liblas.point.Point object at 0x7377f0>

Point

The liblas.point module contains a Point class that you can use to manipulate LAS point data. It is fairly basic and contains a number of properties you can set and get:

>>> p.x, p.y, p.z
(289814.15000000002, 4320978.6100000003, 170.75999999999999)
>>> p.scan_angle
0
>>> p.scan_direction
0
>>> p.return_number
0
>>> p.number_of_returns
6
>>> p.flightline_edge
0
>>> p.classification
2
>>> p.time
datetime.datetime(1970, 1, 6, 12, 44, 10, 1)

Writing

To write a new LAS file, you are first required to have a header. The header will have a number of default values, but it is important to set the dataformat_id and version_minor if you wish to have 1.1 files or records that also include time values.

>>> from liblas import header
>>> h = header.Header()
### Support storing time values
>>> h.dataformat_id = 1
### Store a 1.1 version file
>>> h.minor_version = 1

Another important item to not is possible to have the same file open for read and write at the same time because LAS files are sequential. For example, the following will fail:

>>> f = file.File('junk.las', mode="w", header=h)
>>> f2 = file.File('junk.las')
Traceback (most recent call last):
...
LASException: ('File %s is already open.  Close the file or delete the reference to it', 'junk.las')

Writing to a LAS file is as simple as opening the file for write mode with the header you want to write and issuing the write() command with some liblas.point.Point instances:

>>> f = file.File('junk.las',mode='w', header= h)
>>> pt = liblas.point.Point()
>>> f.write(pt)
>>> f.close()