Skip to content

Commit

Permalink
refactor: create test tiles on top of rio RGB.tiff
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau committed May 1, 2023
1 parent c3b799d commit 416b617
Show file tree
Hide file tree
Showing 29 changed files with 4,983 additions and 3,407 deletions.
1 change: 1 addition & 0 deletions rio_vrt/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

types = {
"byte": "Byte",
"uint8": "Byte",
"uint16": "UInt16",
"int16": "Int16",
"uint32": "UInt32",
Expand Down
21 changes: 16 additions & 5 deletions rio_vrt/vrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ def _add_source_content(
) -> None:
"""Add the content of a sourcefile in xml."""
width, height = str(src.width), str(src.height)
blockx, blocky = str(src.profile["blockxsize"]), str(src.profile["blockysize"])
blockx = str(src.profile.get("blockxsize", ""))
blocky = str(src.profile.get("blockysize", ""))

attr = {
"RasterXSize": width,
"RasterYSize": height,
"DataType": type,
"BlockXSize": blockx,
"BlockYSize": blocky,
}

# optional attributes
if blockx and blocky:
attr["BlockXSize"], attr["BlockySize"] = blockx, blocky

ET.SubElement(Source, "SourceProperties", attr)

attr = {"xOff": "0", "yOff": "0", "xSize": width, "ySize": height}
Expand Down Expand Up @@ -162,13 +166,20 @@ def build_vrt(
color = colorinterps[i - 1].name.capitalize()
ET.SubElement(VRTRasterBands_dict[i], "ColorInterp").text = color

if nodatavals[i - 1] is not None:
text = str(nodatavals[i - 1])
ET.SubElement(VRTRasterBands_dict[i], "NoDataValue").text = text

# add the files
for f in files:
relativeToVRT = "1" if relative is True else "0"
with rio.open(f) as src:
for i in indexes:
is_alpha = colorinterps[i - 1] == ColorInterp.alpha
source_type = "ComplexSource" if is_alpha else "SimpleSource"
has_nodata = nodatavals[i - 1] is not None
source_type = (
"ComplexSource" if is_alpha or has_nodata else "SimpleSource"
)
Source = ET.SubElement(VRTRasterBands_dict[i], source_type)

attr = {"relativeToVRT": relativeToVRT}
Expand All @@ -187,7 +198,7 @@ def build_vrt(

if nodatavals[i - 1] is not None:
text = str(nodatavals[i - 1])
ET.SubElement(Source, "NoDataValue").text = text
ET.SubElement(Source, "NODATA").text = text

if colorinterps[i - 1] == ColorInterp.alpha:
ET.SubElement(Source, "UseMaskBand").text = "true"
Expand Down
69 changes: 66 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
"""Pytest session configuration."""

from itertools import product
from pathlib import Path
from typing import List
from typing import Generator, List, Tuple

import pytest
import rasterio as rio
from natsort import natsorted
from rasterio import transform, windows


def get_tile_size(ds: rio.DatasetReader) -> int:
"""Return the size tile corresponding to the giveb file.
.. warning::
The file must be squared.
Args:
ds: the rasterio dataset
Returns:
the square size
"""
return int(ds.width / 4), int(ds.height / 4)


def get_tiles(
ds: rio.DatasetReader, size: Tuple[int, int]
) -> Generator[int, windows.Window, transform.Affine]:
"""Return the window and transformed associated to a tile from a given dataset.
Args:
ds: the rasterio dataset
size: the tile size
"""
xsize, ysize = size
nols, nrows = ds.width, ds.height
offsets = product(range(0, nols, xsize), range(0, nrows, ysize))
big_window = windows.Window(col_off=0, row_off=0, width=nols, height=nrows)
for col_off, row_off in offsets:
window = windows.Window(
col_off=col_off, row_off=row_off, width=xsize, height=ysize
).intersection(big_window)
transform = windows.transform(window, ds.transform)
yield window, transform


@pytest.fixture(scope="session")
Expand All @@ -16,5 +55,29 @@ def data_dir() -> Path:
@pytest.fixture(scope="session")
def tiles(data_dir: Path) -> List[Path]:
"""Returns the list of tiles from the data folder as relative path."""
tiles = [f for f in data_dir.glob("*.tiff")]
return natsorted(tiles)
# download the data from the rasterio repository
rio_data = "https://github.com/rasterio/rasterio/raw/main/tests/data/RGB.byte.tif"

# split the data in tiles in 2 resolutions
raw_dir = data_dir / "raw"
raw_dir.mkdir(exist_ok=True)

# create a list of files
with rio.open(rio_data) as src:
profile = src.profile.copy()
size = get_tile_size(src)

for i, tile in enumerate(get_tiles(src, size)):
window, affine = tile
profile.update(transform=affine, width=window.width, height=window.height)
file = raw_dir / f"tile{i}.tiff"
with rio.open(file, "w", **profile) as dst:
dst.write(src.read(window=window))

# yield the sorted list
tiles = [f for f in raw_dir.glob("*.tiff")]
yield natsorted(tiles)

# flush the folder
[f.unlink() for f in tiles]
raw_dir.rmdir()
75 changes: 0 additions & 75 deletions tests/data/cut_image.py

This file was deleted.

Binary file removed tests/data/tile_0.tiff
Binary file not shown.
Binary file removed tests/data/tile_1.tiff
Binary file not shown.
Binary file removed tests/data/tile_10.tiff
Binary file not shown.
Binary file removed tests/data/tile_11.tiff
Binary file not shown.
Binary file removed tests/data/tile_12.tiff
Binary file not shown.
Binary file removed tests/data/tile_13.tiff
Binary file not shown.
Binary file removed tests/data/tile_14.tiff
Binary file not shown.
Binary file removed tests/data/tile_15.tiff
Binary file not shown.
Binary file removed tests/data/tile_2.tiff
Binary file not shown.
Binary file removed tests/data/tile_3.tiff
Binary file not shown.
Binary file removed tests/data/tile_4.tiff
Binary file not shown.
Binary file removed tests/data/tile_5.tiff
Binary file not shown.
Binary file removed tests/data/tile_6.tiff
Binary file not shown.
Binary file removed tests/data/tile_7.tiff
Binary file not shown.
Binary file removed tests/data/tile_8.tiff
Binary file not shown.
Binary file removed tests/data/tile_9.tiff
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/test_rio_vrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_build_vrt_html_shema(tiles: List[Path], data_dir: Path) -> None:
with NamedTemporaryFile(suffix=".vrt", dir=data_dir) as vrt_path:
vrt_file = rio_vrt.build_vrt(vrt_path.name, tiles)
xml_schema = xmlschema.XMLSchema(urlopen(_xsd_file))
assert xml_schema.is_valid(vrt_file)
assert xml_schema.validate(vrt_file) is None


def test_build_vrt_stack_shema(tiles: List[Path], data_dir: Path) -> None:
Expand All @@ -39,7 +39,7 @@ def test_build_vrt_stack_shema(tiles: List[Path], data_dir: Path) -> None:
with NamedTemporaryFile(suffix=".vrt", dir=data_dir) as vrt_path:
vrt_file = rio_vrt.build_vrt(vrt_path.name, tiles, mosaic=False)
xml_schema = xmlschema.XMLSchema(urlopen(_xsd_file))
assert xml_schema.is_valid(vrt_file)
assert xml_schema.validate(vrt_file) is None


def test_build_vrt_complete(tiles: List[Path], data_dir: Path, file_regression) -> None:
Expand Down
Loading

0 comments on commit 416b617

Please sign in to comment.