3
*********************************************
5
*********************************************
7
This basic tutorial explains how to use libLAS to read and write
8
LIDAR data encoded in LAS file format from Python.
10
=============================================
12
=============================================
14
1. Reading LAS data in Python is a simple as opening the file with the
15
file.File class and using the iterator to chug through the points.
17
.. code-block:: python
19
>>> from liblas import file
20
>>> f = file.File('file.las',mode='r')
22
... print 'X,Y,Z: ', p.x, p.y, p.z
24
2. You can also read specific points from a file:
26
.. code-block:: python
28
>>> from liblas import file
29
>>> f = file.File('file.las', mode='r')
32
<liblas.point.Point object at 0x7377f0>
34
File versions and formats
35
------------------------------------------------------------------------------
37
The LAS format, as of this writing, provides three different file format
38
versions (1.0, 1.1, and 1.2) and four different point format verions (0, 1, 2, 3).
39
It is important that you be mindful of the minor_version and the dataformat_id
40
when working with files. If the dataformat_id is not correct for the type of
41
data you want to store (color, time, or color + time), none of that data will
42
be read or written, even though placeholders will exist on the liblas.point
46
.. csv-table:: Point format versions and their properties
47
:header: "dataformat_id", "Time", "Color"
54
You control whether or not color, time, or color + time is stored in the
55
file by setting the dataformat_id in the header that you use to create
56
the file.File. This has to be done when you instantiate the file, it
57
cannot be changed after the fact. Attempting to do so may result in
60
.. csv-table:: Base properties of all points regardless of dataformat_id
75
=============================================
77
=============================================
79
Headers, points, VLRs, colors, and GUIDs are *copies*, not references in
80
the libLAS Python bindings. After opening a LAS file, you can fetch
81
the header with the following property:
83
.. code-block:: python
87
<liblas.header.Header object at 0x737790>
90
------------------------------------------------------------------------------
94
There are many properties of the header that you can get and set:
96
.. code-block:: python
98
>>> h.major_version, h.minor_version
103
[289020.90000000002, 4320942.6100000003, 166.78]
105
[290106.02000000002, 4323641.5700000003, 215.48000000000002]
111
>>> h.point_records_count
114
>>> from liblas import guid
116
'00000000-0000-0000-0000-000000000000'
118
00000000-0000-0000-0000-000000000000
121
5cb59173-124b-476b-9729-bafa87cfb27c
124
5cb59173-124b-476b-9729-bafa87cfb27c
126
'5cb59173-124b-476b-9729-bafa87cfb27c'
128
# only works if you have libgeotiff and/or GDAL linked in
131
+proj=tmerc +lat_0=0.000000000 +lon_0=-93.000000000 +k=0.999600 \
132
+x_0=500000.000 +y_0=0.000 +ellps=WGS84 +units=m
134
=============================================
136
=============================================
138
The liblas.point module contains a Point class that you can use to manipulate
139
LAS point data. It is fairly basic and contains a number of properties you
142
.. code-block:: python
145
(289814.15000000002, 4320978.6100000003, 170.75999999999999)
153
>>> p.number_of_returns
155
>>> p.flightline_edge
160
datetime.datetime(1970, 1, 6, 12, 44, 10, 1)
173
=============================================
175
=============================================
177
Variable Length Records (VLR) are frequently used by applications to store
178
anything they wish in the file as a "blob" written into the header of the
179
file. libLAS supports writing and creating your own VLRs in addition to
180
taking on the work of interpreting and using VLR records related to
181
spatial reference systems if GDAL and proj.4 are linked into the library.
183
The following code demonstrates how to write your own VLR by opening an
184
XML file and inserting it into a new file.
186
.. code-block:: python
188
from liblas import file as lasfile
189
from liblas import vlr
190
from liblas import header as lasheader
192
f = lasfile.File('test/data/srs_utm17.las',None,'rb')
197
text = open('schemas/las.xml','rb').read()
201
data = ctypes.create_string_buffer(text)
209
f2 = lasfile.File('junk.las',header=h,mode='w')
215
=============================================
217
=============================================
219
To write a new LAS file, you are first required to have a header. The header
220
will have a number of default values, but it is important to set the
221
dataformat_id and version_minor if you wish to have 1.1 files or records that
222
also include time values.
224
.. code-block:: python
226
>>> from liblas import header
227
>>> h = header.Header()
229
### Support storing time values
230
>>> h.dataformat_id = 1
232
### Store a 1.1 version file
233
>>> h.minor_version = 1
235
Another important item to not is possible to have the same file open for
236
read and write at the same time because LAS files are sequential. For example,
237
the following will fail:
239
.. code-block:: python
241
>>> f = file.File('junk.las', mode="w", header=h)
242
>>> f2 = file.File('junk.las')
243
Traceback (most recent call last):
245
LASException: ('File %s is already open. Close the file or delete the reference to it', 'junk.las')
248
Writing to a LAS file is as simple as opening the file for write mode
249
with the header you want to write and issuing the write() command with some
250
liblas.point.Point instances:
252
.. code-block:: python
254
>>> f = file.File('junk.las',mode='w', header= h)
255
>>> pt = liblas.point.Point()