File: extradims.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 (66 lines) | stat: -rw-r--r-- 1,449 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
import numpy as np

from . import errors

_extra_dims_base = (
    "u1",
    "i1",
    "u2",
    "i2",
    "u4",
    "i4",
    "u8",
    "i8",
    "f4",
    "f8",
)

_allowed_extra_dims_types = []
_allowed_extra_dims_types.extend(np.dtype(base) for base in _extra_dims_base)
for i in (2, 3):
    _allowed_extra_dims_types.extend(
        np.dtype(f"{i}{base}") for base in _extra_dims_base
    )
_allowed_extra_dims_types = tuple(_allowed_extra_dims_types)


def get_dtype_for_extra_dim(type_index: int) -> np.dtype:
    """Returns the dtype for the given type index

    Note that 0 is a special case not handled by this

    Parameters
    ----------
    type_index: int
        index of the type as defined in the LAS Specification

    Returns
    -------
    str,
        a string representing the type, can be understood by numpy

    """
    assert type_index != 0, "Can't get np.dtype for type_index 0"
    try:
        return _allowed_extra_dims_types[type_index - 1]
    except IndexError:
        raise errors.UnknownExtraType(type_index) from None


def get_id_for_extra_dim_type(dtype: np.dtype) -> int:
    """Returns the index of the type as defined in the LAS Specification

    Parameters
    ----------
    dtype: str

    Returns
    -------
    int
        index of the type

    """
    try:
        return _allowed_extra_dims_types.index(dtype) + 1
    except ValueError:
        raise errors.UnknownExtraType(dtype) from None