-
Notifications
You must be signed in to change notification settings - Fork 2
/
pylasfile.py
1522 lines (1365 loc) · 47.3 KB
/
pylasfile.py
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#name: pylasfile
#created: July 2017
#by: [email protected]
#description: python module to read and write a ASPRS LAS file natively
#notes: See main at end of script for example how to use this
#based on ASPRS LAS 1.4-R13 15 July 2013
# See readme.md for more details
# LAS FORMAT DEFINITION:
# The format contains binary data consisting of
# A public header block,
# Any number of (optional) Variable Length Records (VLRs)
# The Point Data Records
# Any number of (optional) Extended Variable Length Records (EVLRs).
#
# All data are in little-endian format.
# The public header block contains generic data such as point numbers and point data bounds.
import os.path
import struct
import pprint
import time
import datetime
import math
import random
def main():
testreader("C:/development/python/samplev1.2.las")
# testreader("C:/development/python/samplev1.4.las")
# testwriter()
def testwriter():
'''
sample write script so we can see how to use the code
'''
outFileName = os.path.join(os.path.dirname(os.path.abspath("c:/development/python/laswriter.las")), "laswriter.las")
outFileName = createOutputFileName(outFileName)
print("outputfile %s" % outFileName)
# writer = laswriter(outFileName, 1.2)
writer = laswriter(outFileName, 1.4)
# write out a WGS variable length record so users know the coordinate reference system
writer.writeVLR_WGS84()
# now write some points
writer.hdr.PointDataRecordFormat = 10
pointslist = []
for _ in range(100):
# now add some random point data to the point lists
writer.x.append(round(random.uniform(1, 100000),6))
writer.y.append(round(random.uniform(1, 100000),6))
writer.z.append(round(random.uniform(1, 1000),6))
start_time = time.time() # time the process so we can keep it quick
# before we write any points, we need to compute the bounding box, scale and offsets
writer.computebbox_offsets()
writer.writepoints()
# we need to write the header after writing records so we can update the bounding box, point format etc
writer.writeHeader()
writer.close()
print("Write duration %.3fs" % (time.time() - start_time )) # time the process
# testreader(outFileName)
###############################################################################
class laswriter:
def __init__(self, filename, lasformat=1.4):
self.fileName = filename
self.fileptr = open(filename, 'wb+')
self.hdr = lashdr(lasformat)
# the lists of all the data we will populate, then write into whatever format the user desires.
# these could be numpy arrays, but that introduces a dependency, so we will leave them as lists
self.x = []
self.y = []
self.z = []
self.intensity = []
self.returnnumber = []
self.numberreturns = []
self.scandirectionflag = []
self.edgeflightline = []
self.classification = []
self.scananglerank = []
self.userdata = []
self.pointsourceid = []
self.gpstime = []
self.red = []
self.green = []
self.blue = []
self.wavepacketdescriptorindex = []
self.byteoffsettowaveformdata = []
self.waveformpacketsize = []
self.returnpointwaveformlocation = []
self.wavex = []
self.wavey = []
self.wavez = []
self.nir = []
self.classificationflags = []
self.scannerchannel = []
self.userdata = []
self.scanangle = []
self.supportedformats = self.hdr.getsuportedpointformats()
def writeVLR_WGS84(self):
'''
compose and write a standard variable length record for the WKTY of WGS84 CRS
'''
# before we write, we need to set the file pointer to the end of the VLR section , which is directly after the header block
vlrl = self.getVLRTotalLength()
self.fileptr.seek(self.hdr.HeaderSize + vlrl, 0)
# now write out the vlr record
vlrReserved = 0
vlrUserid = b'LASF_Projection'
vlrrecordid = 2112
byte_str = 'WKT OGC COORDINATE SYSTEM'.encode('utf-8')
byte_str = byte_str[:32].decode('utf-8', 'ignore').encode('utf-8')
vlrDescription = byte_str
vlrdata = b'GEOGCS["WGS 84",DATUM["World_Geodetic_System_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"],AXIS["Longitude",EAST],AXIS["Latitude",NORTH]]\x00'
# vlrdata = b'PROJCS["WGS 84 / UTM zone 55S",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",147],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32755"]]\x00'
vlrRecordLengthAfterHeader = len(vlrdata)
# now we have set the file pointer to the correct spot, write the record to disc
record_struct = struct.Struct(self.hdr.vlrhdr14fmt)
self.fileptr.write(record_struct.pack(vlrReserved, vlrUserid, vlrrecordid, vlrRecordLengthAfterHeader, vlrDescription))
self.fileptr.write(vlrdata)
self.hdr.NumberofVariableLengthRecords += 1
def getVLRTotalLength(self):
VLRTotalLength = 0
# seek to the start of the vlrdata
currentPosition = self.fileptr.tell()
self.fileptr.seek(self.hdr.hdr14len, 0)
for i in range (self.hdr.NumberofVariableLengthRecords):
data = self.fileptr.read(self.hdr.vlrhdr14len)
s = struct.unpack(self.hdr.vlrhdr14fmt, data)
vlrRecordLengthAfterHeader = s[3]
# now read the variable data
vlrdata = self.fileptr.read(vlrRecordLengthAfterHeader)
VLRTotalLength = VLRTotalLength + 54 + vlrRecordLengthAfterHeader
self.fileptr.seek(currentPosition)
return VLRTotalLength
def fit(self, s, l):
u = s.encode("utf8")
while True:
if len(s) <= l:
return s + "\0" * (l - len(s))
u = u[:-1]
s = u.encode("utf8")
return s
def computebbox_offsets(self):
'''
compute the bounding box of all records in the list
'''
self.hdr.MaxX = max(self.x)
self.hdr.MinX = min(self.x)
self.hdr.MaxY = max(self.y)
self.hdr.MinY = min(self.y)
self.hdr.MaxZ = max(self.z)
self.hdr.MinZ = min(self.z)
self.hdr.Xoffset = self.hdr.MinX
self.hdr.Yoffset = self.hdr.MinY
self.hdr.Zoffset = self.hdr.MinZ
digit2, afterDP2 = self.precision_and_scale(self.hdr.MaxX - self.hdr.MinX)
self.hdr.Xscalefactor = 10**-(8-digit2)
digit2, afterDP2 = self.precision_and_scale(self.hdr.MaxY - self.hdr.MinY)
self.hdr.Yscalefactor = 10**-(8-digit2)
digit2, afterDP2 = self.precision_and_scale(self.hdr.MaxZ - self.hdr.MinZ)
self.hdr.Zscalefactor = 10**-(8-digit2)
def precision_and_scale(self, x):
'''
compute the number of digits beafore and after the decimal place so we can accurately scale a float into an integer
'''
max_digits = 14
int_part = int(abs(x))
magnitude = 1 if int_part == 0 else int(math.log10(int_part)) + 1
if magnitude >= max_digits:
return (magnitude, 0)
frac_part = abs(x) - int_part
multiplier = 10 ** (max_digits - magnitude)
frac_digits = multiplier + int(multiplier * frac_part + 0.5)
while frac_digits % 10 == 0:
frac_digits /= 10
scale = int(math.log10(frac_digits))
return (magnitude, scale)
# return (scale) #return the number of digits after the decimal point only
def zerolistmaker(self, n):
listofzeros = [0] * n
return listofzeros
def onelistmaker(self, n):
listofones = [1] * n
return listofones
def fixemptylists(self):
if len(self.intensity) == 0:
self.intensity = self.zerolistmaker(len(self.x))
if len(self.returnnumber) == 0:
self.returnnumber = self.onelistmaker(len(self.x))
if len(self.numberreturns) == 0:
self.numberreturns = self.onelistmaker(len(self.x))
if len(self.scandirectionflag) == 0:
self.scandirectionflag = self.zerolistmaker(len(self.x))
if len(self.edgeflightline) == 0:
self.edgeflightline = self.zerolistmaker(len(self.x))
if len(self.classification) == 0:
self.classification = self.zerolistmaker(len(self.x))
if len(self.scananglerank) == 0:
self.scananglerank = self.zerolistmaker(len(self.x))
if len(self.userdata) == 0:
self.userdata = self.zerolistmaker(len(self.x))
if len(self.pointsourceid) == 0:
self.pointsourceid = self.zerolistmaker(len(self.x))
if len(self.gpstime) == 0:
self.gpstime = self.zerolistmaker(len(self.x))
if len(self.red) == 0:
self.red = self.zerolistmaker(len(self.x))
if len(self.green) == 0:
self.green = self.zerolistmaker(len(self.x))
if len(self.blue) == 0:
self.blue = self.zerolistmaker(len(self.x))
if len(self.wavepacketdescriptorindex) == 0:
self.wavepacketdescriptorindex = self.zerolistmaker(len(self.x))
if len(self.byteoffsettowaveformdata) == 0:
self.byteoffsettowaveformdata = self.zerolistmaker(len(self.x))
if len(self.waveformpacketsize) == 0:
self.waveformpacketsize = self.zerolistmaker(len(self.x))
if len(self.returnpointwaveformlocation) == 0:
self.returnpointwaveformlocation = self.zerolistmaker(len(self.x))
if len(self.wavex) == 0:
self.wavex = self.zerolistmaker(len(self.x))
if len(self.wavey) == 0:
self.wavey = self.zerolistmaker(len(self.x))
if len(self.wavez) == 0:
self.wavez = self.zerolistmaker(len(self.x))
if len(self.nir) == 0:
self.nir = self.zerolistmaker(len(self.x))
if len(self.classificationflags) == 0:
self.classificationflags = self.zerolistmaker(len(self.x))
if len(self.scannerchannel) == 0:
self.scannerchannel = self.zerolistmaker(len(self.x))
if len(self.userdata) == 0:
self.userdata = self.zerolistmaker(len(self.x))
if len(self.scanangle) == 0:
self.scanangle = self.zerolistmaker(len(self.x))
def writepoints(self):
'''
write points in the ASPRS version 1.4 format
'''
xs = self.hdr.Xscalefactor
ys = self.hdr.Yscalefactor
zs = self.hdr.Zscalefactor
xo = self.hdr.Xoffset
yo = self.hdr.Yoffset
zo = self.hdr.Zoffset
self.fixemptylists()
self.hdr.LegacyNumberofpointrecords += len(self.x)
self.hdr.LegacyNumberofpointsbyreturn1 += len(self.x)
self.hdr.Numberofpointrecords += len(self.x)
self.hdr.Numberofpointsbyreturn1 += len(self.x)
if self.hdr.lasformat == 1.2:
self.hdr.Offsettopointdata = self.hdr.hdr12len + self.getVLRTotalLength()
if self.hdr.lasformat == 1.4:
self.hdr.Offsettopointdata = self.hdr.hdr14len + self.getVLRTotalLength()
if self.hdr.PointDataRecordFormat == 0:
for i in range(len(self.x)):
flags = self.setpointflags(self.returnnumber[i], self.numberreturns[i], self.scandirectionflag[i], self.edgeflightline[i])
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flags,
self.classification[i],
self.scanangle[i],
self.userdata[i],
self.pointsourceid[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 1:
for i in range(len(self.x)):
flags = self.setpointflags(self.returnnumber[i], self.numberreturns[i], self.scandirectionflag[i], self.edgeflightline[i])
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flags,
self.classification[i],
self.scanangle[i],
self.userdata[i],
self.pointsourceid[i],
self.gpstime[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 2:
for i in range(len(self.x)):
flags = self.setpointflags(self.returnnumber[i], self.numberreturns[i], self.scandirectionflag[i], self.edgeflightline[i])
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flags,
self.classification[i],
self.scanangle[i],
self.userdata[i],
self.pointsourceid[i],
self.red[i],
self.green[i],
self.blue[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 3:
for i in range(len(self.x)):
flags = self.setpointflags(self.returnnumber[i], self.numberreturns[i], self.scandirectionflag[i], self.edgeflightline[i])
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flags,
self.classification[i],
self.scanangle[i],
self.userdata[i],
self.pointsourceid[i],
self.gpstime[i],
self.red[i],
self.green[i],
self.blue[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 4:
for i in range(len(self.x)):
flags = self.setpointflags(self.returnnumber[i], self.numberreturns[i], self.scandirectionflag[i], self.edgeflightline[i])
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flags,
self.classification[i],
self.scanangle[i],
self.userdata[i],
self.pointsourceid[i],
self.gpstime[i],
self.wavepacketdescriptorindex[i],
self.byteoffsettowaveformdata[i],
self.waveformpacketsize[i],
self.returnpointwaveformlocation[i],
self.wavex[i],
self.wavey[i],
self.wavez[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 5:
for i in range(len(self.x)):
flags = self.setpointflags(self.returnnumber[i], self.numberreturns[i], self.scandirectionflag[i], self.edgeflightline[i])
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flags,
self.classification[i],
self.scanangle[i],
self.userdata[i],
self.pointsourceid[i],
self.gpstime[i],
self.red[i],
self.green[i],
self.blue[i],
self.wavepacketdescriptorindex[i],
self.byteoffsettowaveformdata[i],
self.waveformpacketsize[i],
self.returnpointwaveformlocation[i],
self.wavex[i],
self.wavey[i],
self.wavez[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 6:
for i in range(len(self.x)):
flag1 = self.setpointflag1_6_10(self.returnnumber[i], self.numberreturns[i])
flag2 = self.setpointflag2_6_10(self.classificationflags[i], self.scannerchannel[i], self.scandirectionflag[i], self.edgeflightline[i] )
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flag1,
flag2,
self.classification[i],
self.userdata[i],
self.scanangle[i],
self.pointsourceid[i],
self.gpstime[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 7:
for i in range(len(self.x)):
flag1 = self.setpointflag1_6_10(self.returnnumber[i], self.numberreturns[i])
flag2 = self.setpointflag2_6_10(self.classificationflags[i], self.scannerchannel[i], self.scandirectionflag[i], self.edgeflightline[i] )
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flag1,
flag2,
self.classification[i],
self.userdata[i],
self.scanangle[i],
self.pointsourceid[i],
self.gpstime[i],
self.red[i],
self.green[i],
self.blue[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 8:
for i in range(len(self.x)):
flag1 = self.setpointflag1_6_10(self.returnnumber[i], self.numberreturns[i])
flag2 = self.setpointflag2_6_10(self.classificationflags[i], self.scannerchannel[i], self.scandirectionflag[i], self.edgeflightline[i] )
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flag1,
flag2,
self.classification[i],
self.userdata[i],
self.scanangle[i],
self.pointsourceid[i],
self.gpstime[i],
self.red[i],
self.green[i],
self.blue[i],
self.nir[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 9:
for i in range(len(self.x)):
flag1 = self.setpointflag1_6_10(self.returnnumber[i], self.numberreturns[i])
flag2 = self.setpointflag2_6_10(self.classificationflags[i], self.scannerchannel[i], self.scandirectionflag[i], self.edgeflightline[i] )
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flag1,
flag2,
self.classification[i],
self.userdata[i],
self.scanangle[i],
self.pointsourceid[i],
self.gpstime[i],
self.wavepacketdescriptorindex[i],
self.byteoffsettowaveformdata[i],
self.waveformpacketsize[i],
self.returnpointwaveformlocation[i],
self.wavex[i],
self.wavey[i],
self.wavez[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
if self.hdr.PointDataRecordFormat == 10:
for i in range(len(self.x)):
flag1 = self.setpointflag1_6_10(self.returnnumber[i], self.numberreturns[i])
flag2 = self.setpointflag2_6_10(self.classificationflags[i], self.scannerchannel[i], self.scandirectionflag[i], self.edgeflightline[i] )
n = (int((self.x[i] - xo) / xs),
int((self.y [i] - yo) / ys),
int((self.z [i] - zo) / zs),
int(self.intensity[i]),
flag1,
flag2,
self.classification[i],
self.userdata[i],
self.scanangle[i],
self.pointsourceid[i],
self.gpstime[i],
self.red[i],
self.green[i],
self.blue[i],
self.nir[i],
self.wavepacketdescriptorindex[i],
self.byteoffsettowaveformdata[i],
self.waveformpacketsize[i],
self.returnpointwaveformlocation[i],
self.wavex[i],
self.wavey[i],
self.wavez[i]
)
# now write the record to disc
record_struct = struct.Struct(self.supportedformats[self.hdr.PointDataRecordFormat][0])
self.fileptr.write(record_struct.pack(*n))
return
def close(self):
self.fileptr.close()
def rewind(self):
# go back to start of file
self.fileptr.seek(0, 0)
def seekPointRecordStart(self):
# set the file pointer to the start of the points block
self.fileptr.seek(self.hdr.Offsettopointdata, 0)
def seekPointRecordEnd(self):
# set the file pointer to the start of the points block
self.fileptr.seek(self.hdr.Offsettopointdata + (self.hdr.Numberofpointrecords * self.hdr.PointDataRecordLength), 0)
def writeHeader(self):
'''
convert the header variables into a list, then conver the list into a tuple so we can pack it
'''
values = self.hdr.hdr2tuple()
if self.hdr.lasformat == 1.2:
s = struct.Struct(self.hdr.hdr12fmt)
if self.hdr.lasformat == 1.4:
s = struct.Struct(self.hdr.hdr14fmt)
data = s.pack(*values)
self.fileptr.seek(0, 0)
self.fileptr.write(data)
def setpointflags(self, returnnumber, numberreturns, scandirectionflag, edgeflightline ):
flags = 0
flags = self.setBitsFor_returnNo(flags, returnnumber)
flags = self.setBitsFor_numberreturns(flags, numberreturns)
flags = self.setBitsFor_scandirectionflag(flags, scandirectionflag)
flags = self.setBitsFor_edgeflightline(flags, edgeflightline)
return flags
def setpointflag1_6_10(self, returnnumber, numberreturns):
flags = 0
flags = self.setBitsFor_returnNo6_10(flags, returnnumber)
flags = self.setBitsFor_numberreturns6_10(flags, numberreturns)
return flags
def setpointflag2_6_10(self, classificationflags, scannerchannel, scandirectionflag, edgeflightline ):
flags = 0
flags = self.setBitsFor_classificationflags6_10(flags, classificationflags)
flags = self.setBitsFor_scannerchannel6_10(flags, scannerchannel)
flags = self.setBitsFor_scandirectionflag(flags, scandirectionflag)
flags = self.setBitsFor_edgeflightline(flags, edgeflightline)
return flags
def isBitSet(self, int_type, offset):
'''testBit() returns a nonzero result, 2**offset, if the bit at 'offset' is one.'''
mask = 1 << offset
return (int_type & (1 << offset)) != 0
def bitSet(self, v, offset):
'''
Set the index:th bit of v to 1 and return the new value.
'''
mask = 1 << offset # Compute mask, an integer with just bit 'index' set.
v |= mask
return v
def setBitsFor_edgeflightline(self, int_type, edgeflightline):
'''
set the bit if this is the edge of a scan
'''
if edgeflightline:
int_type = self.bitSet(int_type, 7)
return int_type
def setBitsFor_scandirectionflag(self, int_type, scandirectionflag):
if scandirectionflag: #positive direction
int_type = self.bitSet(int_type, 6)
return int_type
def setBitsFor_numberreturns(self, int_type, numberreturns):
'''
packs the number of returns into the byte at the correct offset
'''
if numberreturns == 0:
return int_type
if numberreturns == 1:
int_type = self.bitSet(int_type, 3)
return int_type
if numberreturns == 2:
int_type = self.bitSet(int_type, 4)
return int_type
if numberreturns == 3:
int_type = self.bitSet(int_type, 3)
int_type = self.bitSet(int_type, 4)
return int_type
if numberreturns == 4:
int_type = self.bitSet(int_type, 5)
return int_type
if numberreturns == 5:
int_type = self.bitSet(int_type, 3)
int_type = self.bitSet(int_type, 5)
return int_type
return int_type
def setBitsFor_returnNo(self, int_type, returnNo):
'''
packs the return number into the byte at the correct offset
'''
if returnNo == 0:
return int_type
if returnNo == 1:
int_type = self.bitSet(int_type, 0)
return int_type
if returnNo == 2:
int_type = self.bitSet(int_type, 1)
return int_type
if returnNo == 3:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
return int_type
if returnNo == 4:
int_type = self.bitSet(int_type, 2)
return int_type
if returnNo == 5:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 2)
return int_type
return int_type
def setBitsFor_returnNo6_10(self, int_type, returnNo):
'''
packs the return number into the byte at the correct offset for the las v1.4
bits 0-3
'''
if returnNo == 0:
return int_type
if returnNo == 1:
int_type = self.bitSet(int_type, 0)
return int_type
if returnNo == 2:
int_type = self.bitSet(int_type, 1)
if returnNo == 3:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
return int_type
if returnNo == 4:
int_type = self.bitSet(int_type, 2)
return int_type
if returnNo == 5:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 2)
return int_type
if returnNo == 6:
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 2)
return int_type
if returnNo == 7:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 2)
return int_type
if returnNo == 8:
int_type = self.bitSet(int_type, 3)
return int_type
if returnNo == 9:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 3)
return int_type
if returnNo == 10:
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 3)
return int_type
if returnNo == 11:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 3)
return int_type
if returnNo == 12:
int_type = self.bitSet(int_type, 2)
int_type = self.bitSet(int_type, 3)
return int_type
if returnNo == 13:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 2)
int_type = self.bitSet(int_type, 3)
return int_type
if returnNo == 14:
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 2)
int_type = self.bitSet(int_type, 3)
return int_type
if returnNo == 15:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 2)
int_type = self.bitSet(int_type, 3)
return int_type
return int_type
def setBitsFor_numberreturns6_10(self, int_type, numberreturns):
'''
packs the number of return into the byte at the correct offset for the las v1.4
# bits 4-7
'''
if numberreturns == 0:
return int_type
if numberreturns == 1:
int_type = self.bitSet(int_type, 4)
return int_type
if numberreturns == 2:
int_type = self.bitSet(int_type, 5)
return int_type
if numberreturns == 3:
int_type = self.bitSet(int_type, 4)
int_type = self.bitSet(int_type, 5)
return int_type
if numberreturns == 4:
int_type = self.bitSet(int_type, 6)
return int_type
if numberreturns == 5:
int_type = self.bitSet(int_type, 4)
int_type = self.bitSet(int_type, 6)
return int_type
if numberreturns == 6:
int_type = self.bitSet(int_type, 5)
int_type = self.bitSet(int_type, 6)
return int_type
if numberreturns == 7:
int_type = self.bitSet(int_type, 4)
int_type = self.bitSet(int_type, 5)
int_type = self.bitSet(int_type, 6)
return int_type
if numberreturns == 8:
int_type = self.bitSet(int_type, 8)
return int_type
if numberreturns == 9:
int_type = self.bitSet(int_type, 4)
int_type = self.bitSet(int_type, 8)
return int_type
if numberreturns == 10:
int_type = self.bitSet(int_type, 5)
int_type = self.bitSet(int_type, 7)
return int_type
if numberreturns == 11:
int_type = self.bitSet(int_type, 4)
int_type = self.bitSet(int_type, 6)
int_type = self.bitSet(int_type, 7)
return int_type
if numberreturns == 12:
int_type = self.bitSet(int_type, 6)
int_type = self.bitSet(int_type, 7)
return int_type
if numberreturns == 13:
int_type = self.bitSet(int_type, 4)
int_type = self.bitSet(int_type, 6)
int_type = self.bitSet(int_type, 7)
return int_type
if numberreturns == 14:
int_type = self.bitSet(int_type, 5)
int_type = self.bitSet(int_type, 6)
int_type = self.bitSet(int_type, 7)
return int_type
if numberreturns == 15:
int_type = self.bitSet(int_type, 4)
int_type = self.bitSet(int_type, 5)
int_type = self.bitSet(int_type, 6)
int_type = self.bitSet(int_type, 7)
return int_type
return int_type
def setBitsFor_classificationflags6_10(self, int_type, classificationflags):
'''
packs the classification flag at the correct offset for the las v1.4
# bits 0-3
'''
if classificationflags == 0:
return int_type
if classificationflags == 1:
int_type = self.bitSet(int_type, 0)
return int_type
if classificationflags == 2:
int_type = self.bitSet(int_type, 1)
if classificationflags == 3:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
return int_type
if classificationflags == 4:
int_type = self.bitSet(int_type, 2)
return int_type
if classificationflags == 5:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 2)
return int_type
if classificationflags == 6:
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 2)
return int_type
if classificationflags == 7:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 2)
return int_type
if classificationflags == 8:
int_type = self.bitSet(int_type, 3)
return int_type
if classificationflags == 9:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 3)
return int_type
if classificationflags == 10:
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 3)
return int_type
if classificationflags == 11:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 3)
return int_type
if classificationflags == 12:
int_type = self.bitSet(int_type, 2)
int_type = self.bitSet(int_type, 3)
return int_type
if classificationflags == 13:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 2)
int_type = self.bitSet(int_type, 3)
return int_type
if classificationflags == 14:
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 2)
int_type = self.bitSet(int_type, 3)
return int_type
if classificationflags == 15:
int_type = self.bitSet(int_type, 0)
int_type = self.bitSet(int_type, 1)
int_type = self.bitSet(int_type, 2)
int_type = self.bitSet(int_type, 3)
return int_type
return int_type
def setBitsFor_scannerchannel6_10(self, int_type, scannerchannel):
'''
packs the scanner channel at the correct offset for the las v1.4
# bits 4 & 5
'''
if scannerchannel == 0:
return int_type
if scannerchannel == 1:
int_type = self.bitSet(int_type, 4)
return int_type
if scannerchannel == 2:
int_type = self.bitSet(int_type, 5)
return int_type
if scannerchannel == 3:
int_type = self.bitSet(int_type, 4)
int_type = self.bitSet(int_type, 5)
return int_type
return int_type
###############################################################################
class lashdr:
def __init__(self, lasformat=1.4):
# version 1.2 header format
self.hdr12fmt = "<4sHHLHH8sBB32s32sHHHLLBHL5L12d"
self.hdr12len = struct.calcsize(self.hdr12fmt)
# version 1.4 header format
self.hdr14fmt = "<4sHHLHH8sBB32s32sHHHLLBHL5LddddddddddddQQLQ15Q"
self.hdr14len = struct.calcsize(self.hdr14fmt)
# variable length record same for v1.2 and v1.4
self.vlrhdr14fmt = "<H16sHH32s"
self.vlrhdr14len = struct.calcsize(self.vlrhdr14fmt)
self.lasformat = lasformat # default to version 1.4.
# create a default template for a V1.4 header. We use this for writing purposes
self.FileSignature = b'LASF'
self.FileSourceID = 0
self.GlobalEncoding = 17
self.ProjectIDGUIDdata1 = 0
self.ProjectIDGUIDdata2 = 0
self.ProjectIDGUIDdata3 = 0
self.ProjectIDGUIDdata4 = b"0"
self.VersionMajor = 1
if self.lasformat == 1.2:
self.VersionMinor = 2
else:
self.VersionMinor = 4
self.SystemIdentifier = b'pylasfile'
self.GeneratingSoftware = b'pylasfile'
self.FileCreationDayofYear = datetime.datetime.now().timetuple().tm_yday
self.FileCreationYear = datetime.datetime.now().year
self.HeaderSize = 375
self.Offsettopointdata = 0
self.NumberofVariableLengthRecords = 0
self.PointDataRecordFormat = 1
self.PointDataRecordLength = 28
self.LegacyNumberofpointrecords = 0
self.LegacyNumberofpointsbyreturn1 = 0
self.LegacyNumberofpointsbyreturn2 = 0
self.LegacyNumberofpointsbyreturn3 = 0
self.LegacyNumberofpointsbyreturn4 = 0
self.LegacyNumberofpointsbyreturn5 = 0
self.Xscalefactor = 1
self.Yscalefactor = 1
self.Zscalefactor = 1
self.Xoffset = 0
self.Yoffset = 0
self.Zoffset = 0
self.MaxX = 0
self.MinX = 0
self.MaxY = 0
self.MinY = 0
self.MaxZ = 0
self.MinZ = 0
self.StartofWaveformDataPacketRecord = 0
self.StartoffirstExtendedVariableLengthRecord = 0
self.NumberofExtendedVariableLengthRecords = 0
self.Numberofpointrecords = 0
self.Numberofpointsbyreturn1 = 0
self.Numberofpointsbyreturn2 = 0
self.Numberofpointsbyreturn3 = 0
self.Numberofpointsbyreturn4 = 0
self.Numberofpointsbyreturn5 = 0
self.Numberofpointsbyreturn6 = 0
self.Numberofpointsbyreturn7 = 0
self.Numberofpointsbyreturn8 = 0
self.Numberofpointsbyreturn9 = 0
self.Numberofpointsbyreturn10 = 0
self.Numberofpointsbyreturn11 = 0
self.Numberofpointsbyreturn12 = 0
self.Numberofpointsbyreturn13 = 0
self.Numberofpointsbyreturn14 = 0
self.Numberofpointsbyreturn15 = 0
def __str__(self):
'''
pretty print this class
'''
return pprint.pformat(vars(self))
def getsuportedpointformats(self):