~ubuntu-branches/ubuntu/trusty/liblas/trusty-proposed

« back to all changes in this revision

Viewing changes to doc/tutorial/python.txt

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2014-01-05 17:00:29 UTC
  • mfrom: (7.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140105170029-ddtp0j63x5jvck2u
Tags: 1.7.0+dfsg-2
Fixed missing linking of system boost component.
(closes: #733282)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. _python_tutorial:
 
2
 
 
3
*********************************************
 
4
Python Tutorial
 
5
*********************************************
 
6
 
 
7
This basic tutorial explains how to use libLAS to read and write 
 
8
LIDAR data encoded in LAS file format from Python.
 
9
 
 
10
=============================================
 
11
Reading
 
12
=============================================
 
13
 
 
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.
 
16
 
 
17
.. code-block:: python
 
18
 
 
19
    >>> from liblas import file
 
20
    >>> f = file.File('file.las',mode='r')
 
21
    >>> for p in f:
 
22
    ...     print 'X,Y,Z: ', p.x, p.y, p.z
 
23
 
 
24
2. You can also read specific points from a file:
 
25
 
 
26
.. code-block:: python
 
27
 
 
28
    >>> from liblas import file
 
29
    >>> f = file.File('file.las', mode='r')
 
30
    >>> p = f.read(0)
 
31
    >>> p
 
32
    <liblas.point.Point object at 0x7377f0>
 
33
 
 
34
File versions and formats
 
35
------------------------------------------------------------------------------
 
36
 
 
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 
 
43
for them.  
 
44
 
 
45
    
 
46
.. csv-table:: Point format versions and their properties
 
47
    :header: "dataformat_id", "Time", "Color"
 
48
    
 
49
    "0", 
 
50
    "1", x
 
51
    "2", , x 
 
52
    "3", x, x
 
53
 
 
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 
 
58
disastrous results.
 
59
 
 
60
.. csv-table:: Base properties of all points regardless of dataformat_id
 
61
    :header: "Name"
 
62
    
 
63
    "x"
 
64
    "y"
 
65
    "z"
 
66
    "intensity"
 
67
    "return_number"
 
68
    "number_of_returns"
 
69
    "scan_direction"
 
70
    "flightline_edge"
 
71
    "classification"
 
72
    "scan_angle"
 
73
    "user_data"
 
74
    
 
75
=============================================
 
76
Header
 
77
=============================================
 
78
 
 
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:
 
82
 
 
83
.. code-block:: python
 
84
 
 
85
    >>> header = f.header
 
86
    >>> h
 
87
    <liblas.header.Header object at 0x737790>
 
88
 
 
89
Discussion
 
90
------------------------------------------------------------------------------
 
91
 
 
92
 
 
93
 
 
94
There are many properties of the header that you can get and set:
 
95
 
 
96
.. code-block:: python
 
97
 
 
98
    >>> h.major_version, h.minor_version
 
99
    (1, 0)
 
100
    >>> h.dataformat_id
 
101
    0
 
102
    >>> h.min
 
103
    [289020.90000000002, 4320942.6100000003, 166.78]
 
104
    >>> h.max
 
105
    [290106.02000000002, 4323641.5700000003, 215.48000000000002]
 
106
    >>> h.scale
 
107
    [0.01, 0.01, 0.01]
 
108
    >>> h.offset
 
109
    [-0.0, -0.0, -0.0]
 
110
 
 
111
    >>> h.point_records_count
 
112
    3265110L
 
113
 
 
114
    >>> from liblas import guid
 
115
    >>> h.project_id
 
116
    '00000000-0000-0000-0000-000000000000'
 
117
    >>> h.guid
 
118
    00000000-0000-0000-0000-000000000000
 
119
    >>> g = guid.GUID()
 
120
    >>> g
 
121
    5cb59173-124b-476b-9729-bafa87cfb27c
 
122
    >>> h.guid = g
 
123
    >>> h.guid
 
124
    5cb59173-124b-476b-9729-bafa87cfb27c
 
125
    >>> h.project_id
 
126
    '5cb59173-124b-476b-9729-bafa87cfb27c'
 
127
    
 
128
    # only works if you have libgeotiff and/or GDAL linked in
 
129
    >>> s = h.srs
 
130
    >>> s.proj4
 
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 
 
133
 
 
134
=============================================
 
135
Point
 
136
=============================================
 
137
 
 
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 
 
140
can set and get:
 
141
 
 
142
.. code-block:: python
 
143
 
 
144
    >>> p.x, p.y, p.z
 
145
    (289814.15000000002, 4320978.6100000003, 170.75999999999999)
 
146
 
 
147
    >>> p.scan_angle
 
148
    0
 
149
    >>> p.scan_direction
 
150
    0
 
151
    >>> p.return_number
 
152
    0
 
153
    >>> p.number_of_returns
 
154
    6
 
155
    >>> p.flightline_edge
 
156
    0
 
157
    >>> p.classification
 
158
    2
 
159
    >>> p.time
 
160
    datetime.datetime(1970, 1, 6, 12, 44, 10, 1)
 
161
 
 
162
    >>> p.intensity
 
163
    120
 
164
    
 
165
    >>> c = p.color
 
166
    >>> c.red
 
167
    255
 
168
    >>> c.blue
 
169
    255
 
170
    >>> c.green
 
171
    255
 
172
 
 
173
=============================================
 
174
VLRs
 
175
=============================================
 
176
 
 
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.  
 
182
 
 
183
The following code demonstrates how to write your own VLR by opening an 
 
184
XML file and inserting it into a new file.
 
185
 
 
186
.. code-block:: python
 
187
 
 
188
    from liblas import file as lasfile
 
189
    from liblas import vlr
 
190
    from liblas import header as lasheader
 
191
 
 
192
    f = lasfile.File('test/data/srs_utm17.las',None,'rb')
 
193
    h = f.header
 
194
 
 
195
    v = vlr.VLR()
 
196
 
 
197
    text =  open('schemas/las.xml','rb').read()
 
198
 
 
199
    import ctypes
 
200
 
 
201
    data = ctypes.create_string_buffer(text)
 
202
 
 
203
    v.userid='hobu'
 
204
    v.recordid = 12345
 
205
    v.data = data
 
206
 
 
207
    h.add_vlr(v)
 
208
 
 
209
    f2 = lasfile.File('junk.las',header=h,mode='w')
 
210
    for p in f:
 
211
        f2.write(p)
 
212
    f2.close()
 
213
 
 
214
    
 
215
=============================================
 
216
Writing
 
217
=============================================
 
218
 
 
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.
 
223
 
 
224
.. code-block:: python
 
225
 
 
226
    >>> from liblas import header
 
227
    >>> h = header.Header()
 
228
 
 
229
    ### Support storing time values
 
230
    >>> h.dataformat_id = 1
 
231
    
 
232
    ### Store a 1.1 version file
 
233
    >>> h.minor_version = 1
 
234
 
 
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:
 
238
 
 
239
.. code-block:: python
 
240
 
 
241
  >>> f = file.File('junk.las', mode="w", header=h)
 
242
  >>> f2 = file.File('junk.las')
 
243
  Traceback (most recent call last):
 
244
  ...
 
245
  LASException: ('File %s is already open.  Close the file or delete the reference to it', 'junk.las')
 
246
 
 
247
 
 
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:
 
251
 
 
252
.. code-block:: python
 
253
 
 
254
    >>> f = file.File('junk.las',mode='w', header= h)
 
255
    >>> pt = liblas.point.Point()
 
256
    >>> f.write(pt)
 
257
    >>> f.close()
 
258