~ubuntu-branches/ubuntu/utopic/liblas/utopic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  >>> from liblas import file
  >>> file.File('notafile.las') #doctest: +IGNORE_EXCEPTION_DETAIL
  Traceback (most recent call last):
  ...
  OSError: missing file
  
  >>> f = file.File('../test/data/TO_core_last_clip.las')
  
  >>> f.header # doctest: +ELLIPSIS
  <liblas.header.Header object at ...>
  
  >>> f.header.point_records_count
  8L
  
  >>> h = f.header
  >>> h.data_offset
  229L
  >>> p = f.read(0)
  >>> p.x, p.y, p.z
  (630262.30000000005, 4834500.0, 51.530000000000001)
  
  >>> p = f.read(6)
  >>> p.x, p.y, p.z
  (630320.95999999996, 4834500.0, 55.009999999999998)
  
  >>> f.seek(5)
  True

  >>> for i in f:
  ...   p = i
  ...   break

  >>> p.x, p.y, p.z
  (630323.57000000007, 4834500.0, 55.020000000000003)
  
  >>> f.seek(4)
  True

  >>> for i in f:
  ...   p = i
  ...   break

  >>> p.x, p.y, p.z
  (630327.58999999997, 4834500.0, 54.730000000000004)

  >>> f.seek(1)
  True

  >>> for i in f:
  ...   p = i
  ...   break

  >>> p.x, p.y, p.z
  (630282.45000000007, 4834500.0, 51.630000000000003)
  
  >>> f.close()
  >>> f.open()


Test Reading different locations and proper internal overwriting of the file

  >>> f2 = file.File('../test/data/TO_core_last_clip.las')
  >>> f2.header.data_offset
  229L  
  >>> p2 = f2.read(6)
  >>> p2.x, p2.y, p2.z
  (630320.95999999996, 4834500.0, 55.009999999999998)

  >>> p2 == f2.read(3)
  False
  >>> p2.x == f2.read(6).x
  True


  >>> f2.close()
  >>> del f2
  
  >>> points = []
  >>> for i in f:
  ...   points.append(i)
  ...   print i # doctest: +ELLIPSIS
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>

  >>> f[0]
  <liblas.point.Point object at ...>

  >>> out = f[0:3]
  >>> out
  [<liblas.point.Point object at ...>, <liblas.point.Point object at ...>, <liblas.point.Point object at ...>]
  >>> len(out)
  3

  >>> len(points)
  8

  >>> for p in points:
  ...   print p.x, p.y
  630262.3 4834500.0
  630282.45 4834500.0
  630300.08 4834500.0
  630346.83 4834500.0
  630327.59 4834500.0
  630323.57 4834500.0
  630320.96 4834500.0
  630280.89 4834500.0

  >>> points = []
  >>> f.seek(0)
  True  
  >>> for i in f:
  ...   points.append(i)
  ...   print i # doctest: +ELLIPSIS
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>


  >>> len(points)
  8  

  >>> del f
  
  >>> f = file.File('junk.las', mode="w", header=h)
  >>> f.header.data_offset
  229L    
  >>> sum(h.offset)
  0.0
  >>> h.min
  [630262.30000000005, 4834500.0, 50.899999999999999]

  >>> for i in points:
  ...    f.write(i)
  
  >>> pts = file.File('junk.las')
  Traceback (most recent call last):
  ...
  LASException: File junk.las is already open for write.  Close the file or delete the reference to it
  
  >>> f.close()
  >>> f.header

  >>> del f
  
Go read the new header we've written.  It might be out of date because what 
was written in mode='w' might not equal what was passed in as the header= paramete

  >>> f2 = file.File('junk.las')
  >>> h = f2.header
  >>> h.data_offset
  229L    
  >>> f2.close()
  
  >>> f = file.File('junk.las', mode='w+', header=h)
  
  >>> for i in points:
  ...    f.write(i)
  
  >>> f.close()
  
  >>> f = file.File('junk.las')
  >>> cnt = 0
  >>> for i in f:
  ...     cnt += 1
  
  >>> cnt
  16

  >>> buffered_header = f.header
  >>> del f 

  >>> buffered_header.padding = 1024 - buffered_header.data_offset
  >>> buffered_header.padding
  795L

  >>> buffered_header.data_offset
  229L

  >>> f3 = file.File('junk2.las',mode='w',header=buffered_header)

  >>> for i in points:
  ...    f3.write(i)
    
  >>> f3.close()

  >>> del f3

  >>> f4 = file.File('junk2.las')

  >>> f4.header.data_offset
  1024L
  >>> del f4

  
  >>> f = file.File('junk2.las')
  >>> f.header.data_offset
  1024L
  
  >>> points = []
  >>> for i in f:
  ...   points.append(i)
  ...   print i # doctest: +ELLIPSIS
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>

  >>> for g in points:
  ...   print round(g.x, 6)
  630262.3
  630282.45
  630300.08
  630346.83
  630327.59
  630323.57
  630320.96
  630280.89

Now try writing a 1.2 version of the junk2.las file above to ensure that
the data_offset *doesn't* change at all

  >>> buffered_header.data_offset = 1024
  >>> buffered_header.minor_version = 2
  >>> f3 = file.File('junk3.las',mode='w',header=buffered_header)

  >>> f3.header.data_offset
  1024L

  >>> for i in points:
  ...    f3.write(i)
    
  >>> f3.close()

  >>> del f3

  >>> f = file.File('junk3.las')
  >>> f.header.data_offset
  1024L
  
  >>> points = []
  >>> for i in f:
  ...   points.append(i)
  ...   print i # doctest: +ELLIPSIS
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>

  >>> for g in points:
  ...   print round(g.x, 6)
  630262.3
  630282.45
  630300.08
  630346.83
  630327.59
  630323.57
  630320.96
  630280.89


The header's offset will change +=2 if there isn't enough room in the 
header after you subtract the VLRs

  >>> from liblas import header
  >>> h2 = header.Header()
  >>> h2.data_offset = 227
  >>> h2.minor_version = 0
  >>> h2.scale = [0.01, 0.01, 0.01]
  >>> f4 = file.File('junk4.las',mode='w',header=h2)

  >>> f4.header.data_offset
  229L

  >>> for i in points:
  ...    f4.write(i)
    
  >>> f4.close()

  >>> del f4


  >>> f = file.File('junk4.las')
  >>> f.header.data_offset
  229L
  
  >>> points = []
  >>> for i in f:
  ...   points.append(i)
  ...   print i # doctest: +ELLIPSIS
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>
  <liblas.point.Point object at ...>

  >>> for g in points:
  ...   print round(g.x, 6)
  630262.3
  630282.45
  630300.08
  630346.83
  630327.59
  630323.57
  630320.96
  630280.89

  >>> comp_header = header.Header()
  >>> comp_header.minor_version = 2
  >>> comp_header.compressed = True
  >>> comp_header.scale = [0.01, 0.01, 0.01]  
  >>> compressed = file.File('output.laz', mode='w', header=comp_header)

  >>> comp_header.compressed
  True

  >>> for i in points:
  ...    compressed.write(i)
  
  >>> compressed.close()
  >>> del compressed

  >>> read_compressed = file.File('output.laz', mode='r')

  >>> read_compressed.header.compressed
  True

  >>> for i in read_compressed:
  ...   print round(i.x, 6)
  630262.3
  630282.45
  630300.08
  630346.83
  630327.59
  630323.57
  630320.96
  630280.89

  
  >>> read_compressed.close()
  >>> del read_compressed    

# The following tests writing out point data that are larger in size than 
# the base point format, allowing you to store extra data.  
# 
#   >>> f6 = file.File('../test/data/TO_core_last_clip.las')
#   >>> p2 = f6.read(6)
#   >>> p2.x, p2.y, p2.z
#   (630320.95999999996, 4834500.0, 55.009999999999998)
# 
#   >>> h6 = f6.header
#   >>> f = h6.schema
#   >>> f.time = True
#   >>> f.color = True
#   >>> f.size = 52
#   >>> h6.schema = f
#   >>> h6.data_record_length
#   52
# 
#   
# f.size - f.base_size will be 16 bytes of space (h6.data_record_length - 34 bytes for point format 3)
# 
# >>> import ctypes
#  
#   >>> d = (ctypes.c_ubyte * (f.size - f.base_size))() 
#   >>> d[10]
#   0
#   >>> d[0] = 13
# 
#   >>> d2 = (ctypes.c_ubyte * 6)() 
#   >>> d2[0] = 11
#     
#   >>> f7 = file.File('junk5.las',mode='w', header=h6)
#   >>> i = 0
#   >>> for p in points:
#   ...     if i == 0:
#   ...         p.data = d
#   ...     if i == 1:
#   ...         p.data = d2
#   ...     i = i + 1
#   ...     f7.write(p)
#   >>> f7.close()
#   >>> del f7
#   
#   >>> f8 = file.File('junk5.las')
#   >>> f8.header.data_record_length
#   52
# 
#   >>> p = f8[0].data[0]
#   >>> p
#   13
#   >>> p = f8[0].data[15]
#   >>> p
#   0
#   
#   >>> p = f8[1].data[0]
#   >>> p
#   11
  
  >>> import os
  >>> os.remove('junk.las')
  >>> os.remove('junk2.las')
  >>> os.remove('junk3.las')
  >>> os.remove('junk4.las')

#  >>> os.remove('junk5.las')
       
  >>> f = file.File('junk.las', mode="w", header=h)
  >>> import liblas.core
  >>> liblas.core.las.LASWriter_Destroy(f.handle)
  >>> print 'destroyed once'
  destroyed once
  >>> f.handle = None
  >>> liblas.core.las.LASWriter_Destroy(f.handle)
  Traceback (most recent call last):
  ...
  LASException: LASError in "LASWriter_Destroy": Pointer 'hWriter' is NULL in 'LASWriter_Destroy'.

  >>> import os
  >>> os.remove('junk.las')