Skip to content

crs

get_affine_sign(crs)

Return (x_sign, y_sign) for an Affine scale, given a CRS.

Some coordinate systems may use unconventional axis directions, in which case the correct direction may not be possible to infer correctly. In these cases, the assumption is that x increases to the right, and y increases upwards.

Source code in src/rastr/gis/crs.py
 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
def get_affine_sign(crs: CRS | str) -> tuple[Literal[+1, -1], Literal[+1, -1]]:
    """Return (x_sign, y_sign) for an Affine scale, given a CRS.

    Some coordinate systems may use unconventional axis directions, in which case
    the correct direction may not be possible to infer correctly. In these cases,
    the assumption is that x increases to the right, and y increases upwards.
    """
    crs = CRS.from_user_input(crs)

    # Try to detect horizontal axis directions from CRS metadata
    dir_x, dir_y, *_ = [(a.direction or "").lower() for a in crs.axis_info]

    try:
        if _is_conventional_direction(dir_x):
            x_sign = +1
        else:
            x_sign = -1
    except NotImplementedError:
        msg = (
            f"Could not determine x-axis direction from CRS axis info '{dir_x}'. "
            "Falling back to +1 (increasing to the right)."
        )
        warnings.warn(msg, stacklevel=2)
        x_sign = +1

    try:
        if _is_conventional_direction(dir_y):
            y_sign = -1
        else:
            y_sign = +1
    except NotImplementedError:
        msg = (
            f"Could not determine y-axis direction from CRS axis info '{dir_y}'. "
            "Falling back to -1 (increasing upwards)."
        )
        warnings.warn(msg, stacklevel=2)
        y_sign = -1

    return x_sign, y_sign