File: header.py

package info (click to toggle)
python-laspy 2.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,928 kB
  • sloc: python: 9,065; makefile: 20
file content (984 lines) | stat: -rw-r--r-- 33,129 bytes parent folder | download
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
import enum
import io
import logging
import struct
import typing
from datetime import date, timedelta
from typing import BinaryIO, Iterable, List, NamedTuple, Optional, Union
from uuid import UUID

import numpy as np

from . import __version__, extradims
from ._compression.format import (
    compressed_id_to_uncompressed,
    is_point_format_compressed,
    uncompressed_id_to_compressed,
)
from .errors import LaspyException
from .point import dims
from .point.format import ExtraBytesParams, PointFormat
from .point.record import PackedPointRecord
from .utils import read_string, write_string
from .vlrs import VLR
from .vlrs.geotiff import create_geotiff_projection_vlrs
from .vlrs.known import (
    ExtraBytesStruct,
    ExtraBytesVlr,
    GeoAsciiParamsVlr,
    GeoKeyDirectoryVlr,
    WktCoordinateSystemVlr,
)
from .vlrs.vlrlist import VLRList

logger = logging.getLogger(__name__)

GENERATING_SOFTWARE_LEN = 32
SYSTEM_IDENTIFIER_LEN = 32

LAS_FILE_SIGNATURE = b"LASF"
DEFAULT_GENERATING_SOFTWARE = f"laspy {__version__}"
assert len(DEFAULT_GENERATING_SOFTWARE) < GENERATING_SOFTWARE_LEN


class Version(NamedTuple):
    major: int
    minor: int

    @classmethod
    def from_str(cls, string: str) -> "Version":
        major, minor = tuple(map(int, string.split(".")))
        return cls(major, minor)

    def __eq__(self, other):
        if isinstance(other, str):
            return str(self) == other
        else:
            return other.major == self.major and other.minor == self.minor

    def __str__(self):
        return f"{self.major}.{self.minor}"


class GpsTimeType(enum.IntEnum):
    WEEK_TIME = 0
    STANDARD = 1


class GlobalEncoding:
    GPS_TIME_TYPE_MASK = 0b0000_0000_0000_0001
    WAVEFORM_INTERNAL_MASK = 0b0000_0000_0000_0010  # 1.3
    WAVEFORM_EXTERNAL_MASK = 0b0000_0000_0000_0100  # 1.3
    SYNTHETIC_RETURN_NUMBERS_MASK = 0b0000_0000_0000_1000  # 1.3
    WKT_MASK = 0b0000_0000_0001_0000  # 1.4

    def __init__(self, value=0):
        self.value = value

    def _set_bit(self, mask):
        self.value |= mask

    def _unset_bit(self, mask):
        self.value ^= mask

    def _set_if_true(self, mask, value):
        if bool(value) is True:
            self._set_bit(mask)
        else:
            self._unset_bit(mask)

    @property
    def gps_time_type(self) -> GpsTimeType:
        return GpsTimeType(self.value & self.GPS_TIME_TYPE_MASK)

    @gps_time_type.setter
    def gps_time_type(self, value: GpsTimeType):
        self.value ^= self.GPS_TIME_TYPE_MASK
        self.value |= int(value) & self.GPS_TIME_TYPE_MASK

    @property
    def waveform_data_packets_internal(self) -> bool:
        return bool(self.value & self.WAVEFORM_INTERNAL_MASK)

    @waveform_data_packets_internal.setter
    def waveform_data_packets_internal(self, value):
        self._set_if_true(self.WAVEFORM_INTERNAL_MASK, value)

    @property
    def waveform_data_packets_external(self) -> bool:
        return bool(self.value & self.WAVEFORM_EXTERNAL_MASK)

    @waveform_data_packets_external.setter
    def waveform_data_packets_external(self, value):
        self._set_if_true(self.WAVEFORM_EXTERNAL_MASK, value)

    @property
    def synthetic_return_numbers(self) -> bool:
        return bool(self.value & self.SYNTHETIC_RETURN_NUMBERS_MASK)

    @synthetic_return_numbers.setter
    def synthetic_return_numbers(self, value):
        self._set_if_true(self.SYNTHETIC_RETURN_NUMBERS_MASK, value)

    @property
    def wkt(self) -> bool:
        return bool(self.value & self.WKT_MASK)

    @wkt.setter
    def wkt(self, value):
        self._set_if_true(self.WKT_MASK, value)

    @classmethod
    def read_from(cls, stream: BinaryIO) -> "GlobalEncoding":
        return cls(int.from_bytes(stream.read(2), byteorder="little", signed=False))

    def write_to(self, stream: BinaryIO) -> None:
        stream.write(self.value.to_bytes(2, byteorder="little", signed=False))


class LasHeader:
    """Contains the information from the header of as LAS file
    with 'implementation' field left out and 'users' field
    stored in more ergonomic classes.

    This header also contains the VLRs

    Examples
    --------

    Creating a default header:

    >>> header = LasHeader()
    >>> header
    <LasHeader(1.2, <PointFormat(3, 0 bytes of extra dims)>)>

    Creating a header with the wanted version and point format:

    >>> header = LasHeader(version=Version(1, 4), point_format=PointFormat(6))
    >>> header
    <LasHeader(1.4, <PointFormat(6, 0 bytes of extra dims)>)>

    >>> header = LasHeader(version="1.4", point_format=6)
    >>> header
    <LasHeader(1.4, <PointFormat(6, 0 bytes of extra dims)>)>
    """

    #: The default version used when None is given to init
    DEFAULT_VERSION = Version(1, 2)
    #: The default point format Used when None is given to init
    DEFAULT_POINT_FORMAT = PointFormat(3)

    _OLD_LASPY_NAMES = {
        "max": "maxs",
        "min": "mins",
        "scale": "scales",
        "offset": "offsets",
        "filesource_id": "file_source_id",
        "system_id": "system_identifier",
        "date": "creation_date",
        "point_return_count": "number_of_points_by_return",
        "software_id": "generating_software",
        "point_records_count": "point_count",
    }

    def __init__(
        self,
        *,
        version: Optional[Union[Version, str]] = None,
        point_format: Optional[Union[PointFormat, int]] = None,
    ) -> None:
        if isinstance(point_format, int):
            point_format = PointFormat(point_format)
        if isinstance(version, str):
            version = Version.from_str(version)

        if version is None and point_format is None:
            version = LasHeader.DEFAULT_VERSION
            point_format = LasHeader.DEFAULT_POINT_FORMAT
        elif version is not None and point_format is None:
            point_format = PointFormat(dims.min_point_format_for_version(str(version)))
        elif version is None and point_format is not None:
            version = Version.from_str(
                dims.preferred_file_version_for_point_format(point_format.id)
            )
        dims.raise_if_version_not_compatible_with_fmt(point_format.id, str(version))

        #: File source id
        self.file_source_id: int = 0
        self.global_encoding: GlobalEncoding = GlobalEncoding()
        #: Project ID
        #: Initialized to null UUID
        self.uuid: UUID = UUID(bytes_le=b"\0" * 16)
        self._version: Version = version
        #: System identifier
        #: Initialized to 'OTHER'
        self.system_identifier: Union[str, bytes] = "OTHER"
        #: The software which generated the file
        #: Initialized to 'laspy'
        self.generating_software: Union[str, bytes] = DEFAULT_GENERATING_SOFTWARE
        self._point_format: PointFormat = point_format
        #: Day the file was created, initialized to date.today
        self.creation_date: Optional[date] = date.today()
        #: The number of points in the file
        self.point_count: int = 0
        #: The numbers used to scale the x,y,z coordinates
        self.scales: np.ndarray = np.array([0.01, 0.01, 0.01], dtype=np.float64)
        #: The numbers used to offset the x,y,z coordinates
        self.offsets: np.ndarray = np.zeros(3, dtype=np.float64)
        # The max values for x,y,z
        self.maxs: np.ndarray = np.zeros(3, dtype=np.float64)
        # The min values for x,y,z
        self.mins: np.ndarray = np.zeros(3, dtype=np.float64)

        #: Number of points by return
        #: for las <= 1.2 only the first 5 elements matters
        self.number_of_points_by_return: np.ndarray = np.zeros(15, dtype=np.uint32)

        #: The VLRS
        self._vlrs: VLRList = VLRList()

        #: Extra bytes between end of header and first vlrs
        self.extra_header_bytes: bytes = b""
        #: Extra bytes between end of vlr end first point
        self.extra_vlr_bytes: bytes = b""

        #: Las >= 1.3
        self.start_of_waveform_data_packet_record: int = 0

        #: Las >= 1.4
        #: Offset to the first evlr in the file
        self.start_of_first_evlr: int = 0
        #: The number of evlrs in the file
        self.number_of_evlrs: int = 0

        #: EVLRs, even though they are not stored in the 'header'
        #: part of the file we keep them in this class
        #: as they contain same information as vlr.
        #: None when the file does not support EVLR
        self.evlrs: Optional[VLRList] = None

        # Info we keep because it's useful for us but not the user
        self.offset_to_point_data: int = 0
        self.are_points_compressed: bool = False

        self._sync_extra_bytes_vlr()

    @property
    def point_format(self) -> PointFormat:
        """The point format"""
        return self._point_format

    @point_format.setter
    def point_format(self, new_point_format: PointFormat) -> None:
        dims.raise_if_version_not_compatible_with_fmt(
            new_point_format.id, str(self.version)
        )
        self._point_format = new_point_format
        self._sync_extra_bytes_vlr()

    @property
    def version(self) -> Version:
        """The version"""
        return self._version

    @version.setter
    def version(self, version: Version) -> None:
        dims.raise_if_version_not_compatible_with_fmt(
            self.point_format.id, str(version)
        )
        self._version = version

    # scale properties
    @property
    def x_scale(self) -> float:
        return self.scales[0]

    @x_scale.setter
    def x_scale(self, value: float) -> None:
        self.scales[0] = value

    @property
    def y_scale(self) -> float:
        return self.scales[1]

    @y_scale.setter
    def y_scale(self, value: float) -> None:
        self.scales[1] = value

    @property
    def z_scale(self) -> float:
        return self.scales[2]

    @z_scale.setter
    def z_scale(self, value: float) -> None:
        self.scales[2] = value

    # offset properties
    @property
    def x_offset(self) -> float:
        return self.offsets[0]

    @x_offset.setter
    def x_offset(self, value: float) -> None:
        self.offsets[0] = value

    @property
    def y_offset(self) -> float:
        return self.offsets[1]

    @y_offset.setter
    def y_offset(self, value: float) -> None:
        self.offsets[1] = value

    @property
    def z_offset(self) -> float:
        return self.offsets[2]

    @z_offset.setter
    def z_offset(self, value: float) -> None:
        self.offsets[2] = value

    # max properties
    @property
    def x_max(self) -> float:
        return self.maxs[0]

    @x_max.setter
    def x_max(self, value: float) -> None:
        self.maxs[0] = value

    @property
    def y_max(self) -> float:
        return self.maxs[1]

    @y_max.setter
    def y_max(self, value: float) -> None:
        self.maxs[1] = value

    @property
    def z_max(self) -> float:
        return self.maxs[2]

    @z_max.setter
    def z_max(self, value: float) -> None:
        self.maxs[2] = value

    # min properties
    @property
    def x_min(self) -> float:
        return self.mins[0]

    @x_min.setter
    def x_min(self, value: float) -> None:
        self.mins[0] = value

    @property
    def y_min(self) -> float:
        return self.mins[1]

    @y_min.setter
    def y_min(self, value: float) -> None:
        self.mins[1] = value

    @property
    def z_min(self) -> float:
        return self.mins[2]

    @z_min.setter
    def z_min(self, value: float) -> None:
        self.mins[2] = value

    @property
    def vlrs(self) -> VLRList:
        return self._vlrs

    @vlrs.setter
    def vlrs(self, vlrs: typing.Iterable[VLR]) -> None:
        self._vlrs = VLRList(vlrs)

        try:
            self.vlrs.extract("LaszipVlr")
        except ValueError:
            pass

        self._sync_extra_bytes_vlr()

    def add_extra_dims(self, params: List[ExtraBytesParams]) -> None:
        for param in params:
            self.point_format.add_extra_dimension(param)
        self._sync_extra_bytes_vlr()

    def add_extra_dim(self, params: ExtraBytesParams):
        self.add_extra_dims([params])

    def add_crs(self, crs: "pyproj.CRS", keep_compatibility: bool = True) -> None:
        """Add a Coordinate Reference System VLR from a pyproj CRS object.

        The type of VLR created depends on the las version and point format
        version. Las version >= 1.4 use WKT string, las version < 1.4 and point
        format < 6 use GeoTiff tags.

        .. warning::
            This requires `pyproj`.

        .. warning::
            Not all CRS are supported when adding GeoTiff tags.
            For example, custom CRS.

            Typically, if the CRS has an EPSG code it will be supported.
        """
        import pyproj

        # check and remove any existing crs vlrs
        for crs_vlr_name in (
            "WktCoordinateSystemVlr",
            "GeoKeyDirectoryVlr",
            "GeoAsciiParamsVlr",
            "GeoDoubleParamsVlr",
        ):
            try:
                self._vlrs.extract(crs_vlr_name)
            except IndexError:
                pass

        new_ver = self._version >= Version(1, 4)
        new_pt = self.point_format.id >= 6

        if new_pt or (new_ver and not keep_compatibility):
            self._vlrs.append(WktCoordinateSystemVlr(crs.to_wkt()))
            self.global_encoding.wkt = True
        else:
            self._vlrs.extend(create_geotiff_projection_vlrs(crs))

    def remove_extra_dim(self, name: str) -> None:
        self.remove_extra_dims([name])

    def remove_extra_dims(self, names: Iterable[str]) -> None:
        for name in names:
            self.point_format.remove_extra_dimension(name)
        self._sync_extra_bytes_vlr()

    def set_version_and_point_format(
        self, version: Version, point_format: PointFormat
    ) -> None:
        dims.raise_if_version_not_compatible_with_fmt(point_format.id, str(version))
        self._version = version
        self.point_format = point_format

    def partial_reset(self) -> None:
        f64info = np.finfo(np.float64)
        self.maxs = np.ones(3, dtype=np.float64) * f64info.min
        self.mins = np.ones(3, dtype=np.float64) * f64info.max

        self.start_of_first_evlr = 0
        self.number_of_evlrs = 0
        self.point_count = 0
        self.number_of_points_by_return = np.zeros(15, dtype=np.uint32)

    def update(self, points: PackedPointRecord) -> None:
        self.partial_reset()
        if not points:
            self.maxs = [0.0, 0.0, 0.0]
            self.mins = [0.0, 0.0, 0.0]
        else:
            self.grow(points)

    def grow(self, points: PackedPointRecord) -> None:
        self.x_max = max(
            self.x_max,
            (points["X"].max() * self.x_scale) + self.x_offset,
        )
        self.y_max = max(
            self.y_max,
            (points["Y"].max() * self.y_scale) + self.y_offset,
        )
        self.z_max = max(
            self.z_max,
            (points["Z"].max() * self.z_scale) + self.z_offset,
        )
        self.x_min = min(
            self.x_min,
            (points["X"].min() * self.x_scale) + self.x_offset,
        )
        self.y_min = min(
            self.y_min,
            (points["Y"].min() * self.y_scale) + self.y_offset,
        )
        self.z_min = min(
            self.z_min,
            (points["Z"].min() * self.z_scale) + self.z_offset,
        )

        for return_number, count in zip(
            *np.unique(points.return_number, return_counts=True)
        ):
            if return_number == 0:
                continue
            if return_number > len(self.number_of_points_by_return):
                break  # np.unique sorts unique values
            self.number_of_points_by_return[return_number - 1] += count
        self.point_count += len(points)

    def set_compressed(self, state: bool) -> None:
        self.are_points_compressed = state

    def max_point_count(self) -> int:
        if self.version <= Version(1, 3):
            return np.iinfo(np.uint32).max
        else:
            return np.iinfo(np.uint64).max

    @classmethod
    def read_from(
        cls, original_stream: BinaryIO, read_evlrs: bool = False
    ) -> "LasHeader":
        """
        Reads the header from the stream

        read_evlrs: If true, evlrs will be read

        Leaves the stream pos right before the point starts
        (regardless of is read_evlrs was true)

        """
        little_endian = "little"
        header = cls()

        stream = io.BytesIO(cls._prefetch_header_data(original_stream))

        file_sig = stream.read(4)
        # This should not be possible as _prefetch already checks this
        assert file_sig == LAS_FILE_SIGNATURE

        header.file_source_id = int.from_bytes(
            stream.read(2), little_endian, signed=False
        )
        header.global_encoding = GlobalEncoding.read_from(stream)

        header.uuid = UUID(bytes_le=stream.read(16))
        header._version = Version(
            int.from_bytes(stream.read(1), little_endian, signed=False),
            int.from_bytes(stream.read(1), little_endian, signed=False),
        )

        header.system_identifier = read_string(stream, SYSTEM_IDENTIFIER_LEN)
        header.generating_software = read_string(stream, GENERATING_SOFTWARE_LEN)

        creation_day_of_year = int.from_bytes(
            stream.read(2), little_endian, signed=False
        )
        creation_year = int.from_bytes(stream.read(2), little_endian, signed=False)
        try:
            header.creation_date = date(creation_year, 1, 1) + timedelta(
                creation_day_of_year - 1
            )
        except ValueError:
            header.creation_date = None

        header_size = int.from_bytes(stream.read(2), little_endian, signed=False)
        header.offset_to_point_data = int.from_bytes(
            stream.read(4), little_endian, signed=False
        )
        number_of_vlrs = int.from_bytes(stream.read(4), little_endian, signed=False)

        point_format_id = int.from_bytes(stream.read(1), little_endian, signed=False)
        point_size = int.from_bytes(stream.read(2), little_endian, signed=False)

        header.point_count = int.from_bytes(stream.read(4), little_endian, signed=False)
        for i in range(5):
            header.number_of_points_by_return[i] = int.from_bytes(
                stream.read(4), little_endian, signed=False
            )

        for i in range(3):
            header.scales[i] = struct.unpack("<d", stream.read(8))[0]
        for i in range(3):
            header.offsets[i] = struct.unpack("<d", stream.read(8))[0]
        for i in range(3):
            header.maxs[i] = struct.unpack("<d", stream.read(8))[0]
            header.mins[i] = struct.unpack("<d", stream.read(8))[0]

        if header.version.minor >= 3:
            header.start_of_waveform_data_packet_record = int.from_bytes(
                stream.read(8), little_endian, signed=False
            )
        if header.version.minor >= 4:
            header.start_of_first_evlr = int.from_bytes(
                stream.read(8), little_endian, signed=False
            )
            header.number_of_evlrs = int.from_bytes(
                stream.read(4), little_endian, signed=False
            )
            header.point_count = int.from_bytes(
                stream.read(8), little_endian, signed=False
            )
            for i in range(15):
                header.number_of_points_by_return[i] = int.from_bytes(
                    stream.read(8), little_endian, signed=False
                )

        current_pos = stream.tell()
        if current_pos < header_size:
            header.extra_header_bytes = stream.read(header_size - current_pos)
        elif current_pos > header_size:
            raise LaspyException("Incoherent header size")

        header._vlrs = VLRList.read_from(stream, num_to_read=number_of_vlrs)

        current_pos = stream.tell()
        if current_pos < header.offset_to_point_data:
            header.extra_vlr_bytes = stream.read(
                header.offset_to_point_data - current_pos
            )
        elif current_pos > header.offset_to_point_data:
            raise LaspyException("Incoherent offset to point data")

        header.are_points_compressed = is_point_format_compressed(point_format_id)
        point_format_id = compressed_id_to_uncompressed(point_format_id)
        point_format = PointFormat(point_format_id)
        try:
            extra_bytes_vlr = typing.cast(
                ExtraBytesVlr, header._vlrs.get("ExtraBytesVlr")[0]
            )
        except IndexError:
            pass
        else:
            if point_size == point_format.size:
                logger.warning(
                    "There is an ExtraByteVlr but the header.point_size matches the "
                    "point size without extra bytes. The extra bytes vlr info will be ignored"
                )
                header._vlrs.extract("ExtraBytesVlr")
            else:
                for extra_dim_info in extra_bytes_vlr.type_of_extra_dims():
                    point_format.add_extra_dimension(extra_dim_info)
        header._point_format = point_format

        if point_size > point_format.size:
            # We have unregistered extra bytes
            num_extra_bytes = point_size - point_format.size
            point_format.dimensions.append(
                dims.DimensionInfo(
                    name="ExtraBytes",
                    kind=dims.DimensionKind.UnsignedInteger,
                    num_bits=8 * num_extra_bytes,
                    num_elements=num_extra_bytes,
                    is_standard=False,
                    description="Un-registered ExtraBytes",
                )
            )
        elif point_size < point_format.size:
            raise LaspyException(
                f"Incoherent point size, "
                f"header says {point_size} point_format created says {point_format.size}"
            )

        if read_evlrs:
            header.read_evlrs(original_stream)
            stream.seek(header.offset_to_point_data)

        return header

    def write_to(
        self,
        stream: BinaryIO,
        ensure_same_size: bool = False,
        encoding_errors: str = "strict",
    ) -> None:
        """
        ensure_same_size: if true this function will raise an internal error
        if the written header would change the offset to point data
        it originally had (meaning the file could become broken),
        Used when rewriting a header to update the file (new point count, mins, maxs, etc)
        """
        if self.point_count > self.max_point_count():
            raise LaspyException(
                f"Version {self.version} cannot save clouds with more than"
                f" {self.max_point_count()} points (current: {self.point_count})"
            )

        little_endian = "little"
        with io.BytesIO() as tmp:
            self._vlrs.write_to(tmp, encoding_errors=encoding_errors)
            vlr_bytes = tmp.getvalue()

        header_size = LAS_HEADERS_SIZE[str(self.version)]
        header_size += len(self.extra_header_bytes)
        new_offset_to_data = header_size + len(vlr_bytes) + len(self.extra_vlr_bytes)

        if ensure_same_size and new_offset_to_data != self.offset_to_point_data:
            raise LaspyException(
                "Internal error, writing header would change original offset to data"
                "and break the file"
            )
        self.offset_to_point_data = new_offset_to_data

        stream.write(LAS_FILE_SIGNATURE)
        stream.write(self.file_source_id.to_bytes(2, little_endian, signed=False))
        self.global_encoding.write_to(stream)
        stream.write(self.uuid.bytes_le)
        stream.write(self.version.major.to_bytes(1, little_endian, signed=False))
        stream.write(self.version.minor.to_bytes(1, little_endian, signed=False))

        was_truncated = write_string(
            stream,
            self.system_identifier,
            SYSTEM_IDENTIFIER_LEN,
            encoding_errors=encoding_errors,
        )
        if was_truncated:
            logger.warning(
                f"system identifier does not fit into the {SYSTEM_IDENTIFIER_LEN} maximum bytes,"
                f" it will be truncated"
            )

        was_truncated = write_string(
            stream,
            self.generating_software,
            GENERATING_SOFTWARE_LEN,
            encoding_errors=encoding_errors,
        )
        if was_truncated:
            logger.warning(
                f"generating software does not fit into the {GENERATING_SOFTWARE_LEN} maximum bytes,"
                f" it will be truncated"
            )

        if self.creation_date is None:
            self.creation_date = date.today()

        stream.write(
            self.creation_date.timetuple().tm_yday.to_bytes(
                2, little_endian, signed=False
            )
        )
        stream.write(self.creation_date.year.to_bytes(2, little_endian, signed=False))

        stream.write(header_size.to_bytes(2, little_endian, signed=False))
        stream.write(self.offset_to_point_data.to_bytes(4, little_endian, signed=False))
        stream.write(len(self._vlrs).to_bytes(4, little_endian, signed=False))

        point_format_id = self.point_format.id
        if self.are_points_compressed:
            point_format_id = uncompressed_id_to_compressed(point_format_id)
        stream.write(point_format_id.to_bytes(1, little_endian, signed=False))
        stream.write(self.point_format.size.to_bytes(2, little_endian, signed=False))

        # Point Count
        if self.version.minor >= 4:
            stream.write(int(0).to_bytes(4, little_endian, signed=False))
            for i in range(5):
                stream.write(int(0).to_bytes(4, little_endian, signed=False))
        else:
            stream.write(self.point_count.to_bytes(4, little_endian, signed=False))
            for i in range(5):
                stream.write(
                    int(self.number_of_points_by_return[i]).to_bytes(
                        4, little_endian, signed=False
                    )
                )

        for i in range(3):
            stream.write(struct.pack("<d", self.scales[i]))
        for i in range(3):
            stream.write(struct.pack("<d", self.offsets[i]))
        for i in range(3):
            stream.write(struct.pack("<d", self.maxs[i]))
            stream.write(struct.pack("<d", self.mins[i]))

        if self.version.minor >= 3:
            stream.write(
                self.start_of_waveform_data_packet_record.to_bytes(
                    8, little_endian, signed=False
                )
            )

        if self.version.minor >= 4:
            stream.write(
                self.start_of_first_evlr.to_bytes(8, little_endian, signed=False)
            )
            stream.write(self.number_of_evlrs.to_bytes(4, little_endian, signed=False))
            stream.write(self.point_count.to_bytes(8, little_endian, signed=False))
            for i in range(15):
                stream.write(
                    int(self.number_of_points_by_return[i]).to_bytes(
                        8, little_endian, signed=False
                    )
                )
        stream.write(self.extra_header_bytes)
        stream.write(vlr_bytes)
        stream.write(self.extra_vlr_bytes)

    def parse_crs(self, prefer_wkt=True) -> Optional["pyproj.CRS"]:
        """
        Method to parse OGC WKT or GeoTiff VLR keys into a pyproj CRS object

        Returns None if no CRS VLR is present, or if the CRS specified
        in the VLRS is not understood.


        Parameters
        ----------
        prefer_wkt: Optional, default True,
            If True the WKT VLR will be preferred in case
            both the WKT and Geotiff VLR are present

        .. warning::
            This requires `pyproj`.

        .. versionadded:: 2.5
            The ``prefer_wkt`` parameters.
        """

        geo_vlr = self._vlrs.get_by_id("LASF_Projection")

        if self.evlrs is not None:
            geo_vlr.extend(self.evlrs.get_by_id("LASF_Projection"))

        parsed_crs = {}
        for rec in geo_vlr:
            if isinstance(rec, (WktCoordinateSystemVlr, GeoKeyDirectoryVlr)):
                crs = rec.parse_crs()
                if crs is not None:
                    parsed_crs[type(rec)] = crs

        # Could not parse anything / there was nothing to parse
        if not parsed_crs:
            return None

        if prefer_wkt:
            preferred, other = WktCoordinateSystemVlr, GeoKeyDirectoryVlr
        else:
            preferred, other = GeoKeyDirectoryVlr, WktCoordinateSystemVlr

        try:
            return parsed_crs[preferred]
        except KeyError:
            return parsed_crs[other]

    def read_evlrs(self, stream):
        """
        Reads EVLRs from the stream and sets them in the
        data property.

        The evlrs are accessed from the `evlrs` property

        Does nothing if either of these is true:
            - The file does not support EVLRS (version < 1.4)
            - The file has no EVLRS
            - The stream does not support seeking

        Leaves/restores the stream position to where it was before the call
        """
        if self.version.minor >= 4:
            if self.number_of_evlrs > 0 and stream.seekable():
                saved_pos = stream.tell()
                stream.seek(self.start_of_first_evlr, io.SEEK_SET)
                self.evlrs = VLRList.read_from(
                    stream, self.number_of_evlrs, extended=True
                )
                stream.seek(saved_pos)
            elif self.number_of_evlrs > 0 and not stream.seekable():
                self.evlrs = None
            else:
                self.evlrs = VLRList()
        else:
            self.evlrs = None

    @staticmethod
    def _prefetch_header_data(source) -> bytes:
        """
        reads (and returns) from the source all the bytes that
        are between the beginning of the file and the start of point data
        (which corresponds to Header + VLRS).

        It is done in two calls to the source's `read` method

        This is done because `LasHeader.read_from`
        does a bunch of read to the source, so we prefer to
        prefetch data in memory in case the original source
        is not buffered (like a http source could be)
        """
        header_bytes = source.read(LAS_HEADERS_SIZE["1.1"])

        file_sig = header_bytes[: len(LAS_FILE_SIGNATURE)]
        if not file_sig:
            raise LaspyException(f"Source is empty")
        if file_sig != LAS_FILE_SIGNATURE:
            raise LaspyException(f'Invalid file signature "{file_sig}"')
        if len(header_bytes) < LAS_HEADERS_SIZE["1.1"]:
            raise LaspyException("File is to small to be a valid LAS")

        offset_to_data = int.from_bytes(
            header_bytes[96 : 96 + 4], byteorder="little", signed=False
        )

        rest = source.read(offset_to_data - len(header_bytes))

        return header_bytes + rest

    def _sync_extra_bytes_vlr(self) -> None:
        try:
            self._vlrs.extract("ExtraBytesVlr")
        except IndexError:
            pass

        extra_dimensions = list(self.point_format.extra_dimensions)
        if not extra_dimensions:
            return

        eb_vlr = ExtraBytesVlr()
        for extra_dimension in extra_dimensions:
            dtype = extra_dimension.dtype
            assert dtype is not None

            eb_struct = ExtraBytesStruct(
                name=extra_dimension.name.encode(),
                description=extra_dimension.description.encode(),
            )

            if extra_dimension.num_elements > 3 and dtype.base == np.uint8:
                type_id = 0
                eb_struct.options = extra_dimension.num_elements
            else:
                type_id = extradims.get_id_for_extra_dim_type(dtype)

            eb_struct.data_type = type_id
            eb_struct.scale = extra_dimension.scales
            eb_struct.offset = extra_dimension.offsets

            eb_vlr.extra_bytes_structs.append(eb_struct)

        self._vlrs.append(eb_vlr)

    # To keep some kind of backward compatibility
    @property
    def major_version(self) -> int:
        return self.version.major

    @property
    def minor_version(self) -> int:
        return self.version.minor

    def __getattr__(self, item):
        try:
            return getattr(self, self._OLD_LASPY_NAMES[item])
        except KeyError:
            raise AttributeError(f"No attribute {item} in LasHeader") from None

    def __setattr__(self, key, value):
        try:
            return setattr(self, self._OLD_LASPY_NAMES[key], value)
        except KeyError:
            super().__setattr__(key, value)

    def __repr__(self) -> str:
        return f"<LasHeader({self.version.major}.{self.version.minor}, {self.point_format})>"


LAS_HEADERS_SIZE = {
    "1.1": 227,
    "1.2": 227,
    "1.3": 235,
    "1.4": 375,
}