Skip to content

Parsing

parsing

Functions

make_4x4_transform(rotation_str, translation_str, scale_str='1')

Convenience function to make a 4x4 matrix from the string format used by Metashape

Parameters:

Name Type Description Default
rotation_str str

Row major with 9 entries

required
translation_str str

3 entries

required
scale_str str

single value. Defaults to "1".

'1'

Returns:

Type Description

np.ndarray: (4, 4) A homogenous transform mapping from cam to world

Source code in geograypher/utils/parsing.py
def make_4x4_transform(rotation_str: str, translation_str: str, scale_str: str = "1"):
    """Convenience function to make a 4x4 matrix from the string format used by Metashape

    Args:
        rotation_str (str): Row major with 9 entries
        translation_str (str): 3 entries
        scale_str (str, optional): single value. Defaults to "1".

    Returns:
        np.ndarray: (4, 4) A homogenous transform mapping from cam to world
    """
    rotation_np = np.fromstring(rotation_str, sep=" ")
    rotation_np = np.reshape(rotation_np, (3, 3))

    if not np.isclose(np.linalg.det(rotation_np), 1.0, atol=1e-8, rtol=0):
        raise ValueError(
            f"Inproper rotation matrix with determinant {np.linalg.det(rotation_np)}"
        )

    translation_np = np.fromstring(translation_str, sep=" ")
    scale = float(scale_str)
    transform = np.eye(4)
    transform[:3, :3] = rotation_np * scale
    transform[:3, 3] = translation_np
    return transform

parse_metashape_mesh_metadata(mesh_metadata_file)

Parse the metadata file which is produced by Metashape when exporting a mesh to determine the coordinate reference frame and origin shift to use when interpreting the mesh.

Parameters:

Name Type Description Default
mesh_metadata_file Union[str, Path]

Path to the metadata XML file.

required

Returns:

Type Description
Union[CRS, None]

Union[pyproj.CRS, None]: The CRS to interpret the vertices (after the shift). If not present, None is returned.

Union[ndarray, None]

Union[np.ndarray, None]: The shift to be added to the mesh vertices. If not present, None is returned.

Source code in geograypher/utils/parsing.py
def parse_metashape_mesh_metadata(
    mesh_metadata_file: typing.Union[str, Path],
) -> typing.Tuple[typing.Union[pyproj.CRS, None], typing.Union[np.ndarray, None]]:
    """
    Parse the metadata file which is produced by Metashape when exporting a mesh to determine the
    coordinate reference frame and origin shift to use when interpreting the mesh.


    Args:
        mesh_metadata_file (typing.Union[str, Path]): Path to the metadata XML file.

    Returns:
        Union[pyproj.CRS, None]:
            The CRS to interpret the vertices (after the shift). If not present, None is returned.
        Union[np.ndarray, None]:
            The shift to be added to the mesh vertices. If not present, None is returned.
    """
    tree = ET.parse(mesh_metadata_file)
    root = tree.getroot()

    # Parse CRS and shift
    CRS = root.find("SRS")
    shift = root.find("SRSOrigin")

    # If CRS is present, convert it to a pyproj type
    if CRS is not None:
        CRS = pyproj.CRS(CRS.text)

    # If the shift is present, convert to a numpy array
    if shift is not None:
        shift = np.array(shift.text.split(","), dtype=float)
    return CRS, shift