Skip to content

xyc module#

XYC file model.#

dfastio.xyc.models #

XYC file reader and writer.

XYCModel #

Class for reading and writing XYC files.

Source code in src/dfastio/xyc/models.py
 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
class XYCModel:
    """Class for reading and writing XYC files."""

    @staticmethod
    def read(
        file_name: str,
        num_columns: int = 2,
        delimiter: str = None,
        has_header: bool = False,
    ) -> linestring.LineString:
        """
        Read lines from a file.

        Args:
            file_name (str):
                Name of the file to be read.
            num_columns (int):
                Number of columns to be read (2 or 3)
            delimiter (str):
                delimiter used in the file.
            has_header (bool):
                whether the file has a header.

        Returns:
            L : shapely.geometry.linestring.LineString
                Line strings.

        Examples:
            ```python
            >>> from dfastio.xyc.models import XYCModel
            >>> path = "examples/data/simple-xyc-file.xyc"
            >>> line = XYCModel.read(path, num_columns=3, delimiter=" ", has_header=True)
            >>> print(line)
            LINESTRING Z (5 6 4, 8 9 7, 11 12 10)

            ```
        """
        ext = Path(file_name).suffix
        if ext.lower() == ".xyc":
            if num_columns == 3:
                column_names = ["Val", "X", "Y"]
            else:
                column_names = ["X", "Y"]
            header = 0 if has_header else None
            if delimiter is not None:
                data = pd.read_csv(
                    file_name,
                    names=column_names,
                    skipinitialspace=True,
                    header=header,
                    delimiter=delimiter,
                )
            else:
                data = pd.read_csv(
                    file_name,
                    names=column_names,
                    skipinitialspace=True,
                    header=header,
                    sep=r"\s+",
                )

            num_points = len(data.X)
            x = data.X.to_numpy().reshape((num_points, 1))
            y = data.Y.to_numpy().reshape((num_points, 1))
            if num_columns == 3:
                z = data.Val.to_numpy().reshape((num_points, 1))
                coords = np.concatenate((x, y, z), axis=1)
            else:
                coords = np.concatenate((x, y), axis=1)
            coords_line_strig = LineString(coords)

        else:
            data_geometry = gpd.read_file(file_name)["geometry"]
            coords_line_strig = data_geometry[0]

        return coords_line_strig

    @staticmethod
    def write(xy: np.ndarray, val: np.ndarray, file_name: str) -> None:
        r"""Write a text file with x, y, and values.

        Args:
            xy (np.ndarray):
                An N x 2 array containing x and y coordinates.
            val (np.ndarray):
                An N x k array containing values.
            file_name (str):
                The name of the file to be written.

        Returns:
            None

        Example:
            ```python
            >>> from dfastio.xyc.models import XYCModel
            >>> import numpy as np
            >>> import tempfile
            >>> xy = np.array([[1, 2], [4, 5], [7, 8], [10, 11]])
            >>> val = np.array([1, 2, 3, 4])
            >>> with tempfile.TemporaryDirectory() as tempdir:
            ...     file_name = f"{tempdir}/output.xyc"
            ...     XYCModel.write(xy, val, file_name)
            ...     with open(file_name, "r") as f:
            ...         f.read()
            '1.00\t2.00\t1.00\n4.00\t5.00\t2.00\n7.00\t8.00\t3.00\n10.00\t11.00\t4.00\n'

            ```
        """
        with open(file_name, "w") as xyc:
            if val.ndim == 1:
                for i, value in enumerate(val):
                    xyc.write(f"{xy[i, 0]:.2f}\t{xy[i, 1]:.2f}\t{value:.2f}\n")
            else:
                for i in range(len(val)):
                    val_str = "\t".join(f"{x:.2f}" for x in val[i, :])
                    xyc.write(f"{xy[i, 0]:.2f}\t{xy[i, 1]:.2f}\t{val_str}\n")

read(file_name: str, num_columns: int = 2, delimiter: str = None, has_header: bool = False) -> linestring.LineString staticmethod #

Read lines from a file.

Parameters:

Name Type Description Default
file_name str

Name of the file to be read.

required
num_columns int

Number of columns to be read (2 or 3)

2
delimiter str

delimiter used in the file.

None
has_header bool

whether the file has a header.

False

Returns:

Name Type Description
L LineString

shapely.geometry.linestring.LineString Line strings.

Examples:

>>> from dfastio.xyc.models import XYCModel
>>> path = "examples/data/simple-xyc-file.xyc"
>>> line = XYCModel.read(path, num_columns=3, delimiter=" ", has_header=True)
>>> print(line)
LINESTRING Z (5 6 4, 8 9 7, 11 12 10)
Source code in src/dfastio/xyc/models.py
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
@staticmethod
def read(
    file_name: str,
    num_columns: int = 2,
    delimiter: str = None,
    has_header: bool = False,
) -> linestring.LineString:
    """
    Read lines from a file.

    Args:
        file_name (str):
            Name of the file to be read.
        num_columns (int):
            Number of columns to be read (2 or 3)
        delimiter (str):
            delimiter used in the file.
        has_header (bool):
            whether the file has a header.

    Returns:
        L : shapely.geometry.linestring.LineString
            Line strings.

    Examples:
        ```python
        >>> from dfastio.xyc.models import XYCModel
        >>> path = "examples/data/simple-xyc-file.xyc"
        >>> line = XYCModel.read(path, num_columns=3, delimiter=" ", has_header=True)
        >>> print(line)
        LINESTRING Z (5 6 4, 8 9 7, 11 12 10)

        ```
    """
    ext = Path(file_name).suffix
    if ext.lower() == ".xyc":
        if num_columns == 3:
            column_names = ["Val", "X", "Y"]
        else:
            column_names = ["X", "Y"]
        header = 0 if has_header else None
        if delimiter is not None:
            data = pd.read_csv(
                file_name,
                names=column_names,
                skipinitialspace=True,
                header=header,
                delimiter=delimiter,
            )
        else:
            data = pd.read_csv(
                file_name,
                names=column_names,
                skipinitialspace=True,
                header=header,
                sep=r"\s+",
            )

        num_points = len(data.X)
        x = data.X.to_numpy().reshape((num_points, 1))
        y = data.Y.to_numpy().reshape((num_points, 1))
        if num_columns == 3:
            z = data.Val.to_numpy().reshape((num_points, 1))
            coords = np.concatenate((x, y, z), axis=1)
        else:
            coords = np.concatenate((x, y), axis=1)
        coords_line_strig = LineString(coords)

    else:
        data_geometry = gpd.read_file(file_name)["geometry"]
        coords_line_strig = data_geometry[0]

    return coords_line_strig

write(xy: np.ndarray, val: np.ndarray, file_name: str) -> None staticmethod #

Write a text file with x, y, and values.

Parameters:

Name Type Description Default
xy ndarray

An N x 2 array containing x and y coordinates.

required
val ndarray

An N x k array containing values.

required
file_name str

The name of the file to be written.

required

Returns:

Type Description
None

None

Example
>>> from dfastio.xyc.models import XYCModel
>>> import numpy as np
>>> import tempfile
>>> xy = np.array([[1, 2], [4, 5], [7, 8], [10, 11]])
>>> val = np.array([1, 2, 3, 4])
>>> with tempfile.TemporaryDirectory() as tempdir:
...     file_name = f"{tempdir}/output.xyc"
...     XYCModel.write(xy, val, file_name)
...     with open(file_name, "r") as f:
...         f.read()
'1.00\t2.00\t1.00\n4.00\t5.00\t2.00\n7.00\t8.00\t3.00\n10.00\t11.00\t4.00\n'
Source code in src/dfastio/xyc/models.py
 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
@staticmethod
def write(xy: np.ndarray, val: np.ndarray, file_name: str) -> None:
    r"""Write a text file with x, y, and values.

    Args:
        xy (np.ndarray):
            An N x 2 array containing x and y coordinates.
        val (np.ndarray):
            An N x k array containing values.
        file_name (str):
            The name of the file to be written.

    Returns:
        None

    Example:
        ```python
        >>> from dfastio.xyc.models import XYCModel
        >>> import numpy as np
        >>> import tempfile
        >>> xy = np.array([[1, 2], [4, 5], [7, 8], [10, 11]])
        >>> val = np.array([1, 2, 3, 4])
        >>> with tempfile.TemporaryDirectory() as tempdir:
        ...     file_name = f"{tempdir}/output.xyc"
        ...     XYCModel.write(xy, val, file_name)
        ...     with open(file_name, "r") as f:
        ...         f.read()
        '1.00\t2.00\t1.00\n4.00\t5.00\t2.00\n7.00\t8.00\t3.00\n10.00\t11.00\t4.00\n'

        ```
    """
    with open(file_name, "w") as xyc:
        if val.ndim == 1:
            for i, value in enumerate(val):
                xyc.write(f"{xy[i, 0]:.2f}\t{xy[i, 1]:.2f}\t{value:.2f}\n")
        else:
            for i in range(len(val)):
                val_str = "\t".join(f"{x:.2f}" for x in val[i, :])
                xyc.write(f"{xy[i, 0]:.2f}\t{xy[i, 1]:.2f}\t{val_str}\n")