Source code for py3dtiles.tileset.content.tile_content
from __future__ import annotations
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Literal
import numpy as np
import numpy.typing as npt
from typing_extensions import Self
from py3dtiles.tileset.bounding_volume_box import BoundingVolumeBox
from .batch_table import BatchTable
from .feature_table import FeatureTable
[docs]
class TileContent(ABC):
[docs]
@abstractmethod
def to_array(self) -> npt.NDArray[np.uint8]:
"""
Return a uint8 view of the bytes composing this tile content.
This is mainly a legacy method used to load from binary files.
"""
[docs]
@abstractmethod
def save_as(self, path: Path) -> None:
"""
Save the tile content to a file.
"""
[docs]
@classmethod
@abstractmethod
def from_bytes(cls, data: bytes) -> Self:
"""
Load a tile_content from bytes. This method can be directly supplied the bytes in the file.
"""
[docs]
@classmethod
def from_file(cls, tile_path: Path) -> Self:
with tile_path.open("rb") as f:
data = f.read()
return cls.from_bytes(data)
[docs]
@abstractmethod
def get_vertex_count(self) -> int: ...
[docs]
@abstractmethod
def get_vertices(self) -> npt.NDArray[np.float32 | np.uint16] | None: ...
[docs]
@abstractmethod
def get_colors(self) -> npt.NDArray[np.uint8 | np.uint16 | np.float32] | None: ...
[docs]
def get_bounding_volume_box(self) -> BoundingVolumeBox | None:
vertices = self.get_vertices()
if vertices is None:
return None
return BoundingVolumeBox.from_points(vertices)
[docs]
class LegacyTileContent(TileContent):
header: TileContentHeader
body: TileContentBody
[docs]
@abstractmethod
def sync(self) -> None:
"""
Allow to synchronize headers with contents.
"""
[docs]
def to_array(self) -> npt.NDArray[np.uint8]:
self.sync()
header_arr = self.header.to_array()
body_arr = self.body.to_array()
return np.concatenate((header_arr, body_arr))
[docs]
@classmethod
@abstractmethod
def from_array(cls, array: npt.NDArray[np.uint8]) -> Self: ...
[docs]
@classmethod
def from_bytes(cls, data: bytes) -> Self:
arr = np.frombuffer(data, dtype=np.uint8)
return cls.from_array(arr)
[docs]
def save_as(self, path: Path) -> None:
tile_arr = self.to_array()
with path.open("bw") as f:
f.write(bytes(tile_arr))
def __str__(self) -> str:
return (
"------ Tile header ------\n"
+ (str(self.header) if self.header is not None else "")
+ "\n"
+ "------ Tile body ------\n"
+ (str(self.body) if self.body is not None else "")
)
[docs]
def get_batch_table_binary_property(self, name: str) -> npt.NDArray[Any]:
"""
Get a binary property from a batch table. Internally forward to `self.body.batch_table.get_binary_property(name)`.
"""
return self.body.batch_table.get_binary_property(name)
[docs]
class TileContentBody(ABC):
batch_table: BatchTable
feature_table: FeatureTable[Any, Any]
@abstractmethod
def __str__(self) -> str: ...
[docs]
@abstractmethod
def to_array(self) -> npt.NDArray[np.uint8]: ...