Skip to content

Commit

Permalink
Merge pull request #8 from cogeotiff/fixCLIandAddValue
Browse files Browse the repository at this point in the history
fix CLI and value
  • Loading branch information
vincentsarago committed Apr 13, 2023
2 parents 1854b5b + 575daa0 commit 6d2ba94
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 32 deletions.
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

32 changes: 17 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
repos:
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.12.1
hooks:
- id: validate-pyproject

- repo: https://github.com/psf/black
rev: 22.3.0
rev: 22.12.0
hooks:
- id: black
language_version: python

- repo: https://github.com/PyCQA/isort
rev: 5.4.2
rev: 5.12.0
hooks:
- id: isort
language_version: python

- repo: https://github.com/PyCQA/flake8
rev: 3.8.3
hooks:
- id: flake8
language_version: python

- repo: https://github.com/PyCQA/pydocstyle
rev: 6.1.1
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.238
hooks:
- id: pydocstyle
language_version: python
additional_dependencies:
- toml
- id: ruff
args: ["--fix"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.812
rev: v0.991
hooks:
- id: mypy
language_version: python
# No reason to run if only tests have changed. They intentionally break typing.
exclude: tests/.*
additional_dependencies:
- types-attrs
- types-cachetools
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## 0.2.1 (2023-04-13)

* Fix issue with CLI path
* add Alpha band value forwarding
* add `--value` option to set a custom value in the data

## 0.2.0 (2022-10-25)

* remove python 3.7 support
Expand Down
32 changes: 26 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Source = "https://github.com/cogeotiff/rio-faux"
Documentation = "https://cogeotiff.github.io/rio-faux/"

[project.entry-points."rasterio.rio_plugins"]
cogeo = "rio_faux.cli:faux"
faux = "rio_faux.cli:faux"

[build-system]
requires = ["flit>=3.2,<4"]
Expand All @@ -62,16 +62,36 @@ exclude = [
"mkdocs.yml",
]

[tool.coverage.run]
branch = true
parallel = true

[tool.coverage.report]
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]

[tool.isort]
profile = "black"
known_first_party = ["rio_fake"]
known_third_party = ["rasterio", "click"]
default_section = "THIRDPARTY"

[tool.mypy]
no_strict_optional = "True"
ignore_missing_imports = "True"
no_strict_optional = true

[tool.pydocstyle]
select = "D1"
match = "(?!test).*.py"
[tool.ruff]
select = [
"D1", # pydocstyle errors
"E", # pycodestyle errors
"W", # pycodestyle warnings
"C", # flake8-comprehensions
"B", # flake8-bugbear
]
ignore = [
"E501", # line too long, handled by black
"B008", # do not perform function calls in argument defaults
"B905", # ignore zip() without an explicit strict= parameter, only support with python >3.10
]
39 changes: 33 additions & 6 deletions rio_faux/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings

import click
import numpy
import rasterio
from rasterio.enums import ColorInterp, MaskFlags
from rasterio.enums import Resampling as ResamplingEnums
Expand All @@ -27,6 +28,12 @@ def has_mask_band(src_dst):
@click.command()
@options.file_in_arg
@options.file_out_arg
@click.option(
"--value",
default=None,
type=float,
help="Set a custom value in the data.",
)
@click.option(
"--forward-band-tags",
default=False,
Expand All @@ -51,6 +58,7 @@ def has_mask_band(src_dst):
def faux(
input,
output,
value,
forward_band_tags,
forward_dataset_tags,
creation_options,
Expand All @@ -68,17 +76,20 @@ def faux(
overview_blocksize = src_dst.profile.get("blockxsize", overview_blocksize)

config.update(
dict(
GDAL_NUM_THREADS="ALL_CPUS",
GDAL_TIFF_INTERNAL_MASK=os.environ.get("GDAL_TIFF_INTERNAL_MASK", True),
GDAL_TIFF_OVR_BLOCKSIZE=str(overview_blocksize),
)
{
"GDAL_NUM_THREADS": "ALL_CPUS",
"GDAL_TIFF_INTERNAL_MASK": os.environ.get("GDAL_TIFF_INTERNAL_MASK", True),
"GDAL_TIFF_OVR_BLOCKSIZE": str(overview_blocksize),
}
)

with rasterio.Env(**config):
with rasterio.open(input) as src_dst:
meta = src_dst.meta
with MemoryFile() as m:
with m.open(**meta) as tmp_dst:
tmp_dst.colorinterp = src_dst.colorinterp

if tmp_dst.colorinterp[0] is ColorInterp.palette:
try:
tmp_dst.write_colormap(1, src_dst.colormap(1))
Expand All @@ -91,6 +102,22 @@ def faux(
if has_mask_band(src_dst):
tmp_dst.write_mask(src_dst.dataset_mask())

if value:
tmp_dst.write(
numpy.full(
(tmp_dst.count, tmp_dst.height, tmp_dst.width),
value,
dtype=tmp_dst.dtypes[0],
),
)

if ColorInterp.alpha in tmp_dst.colorinterp:
alpha_bidx = src_dst.colorinterp.index(ColorInterp.alpha) + 1
tmp_dst.write(
src_dst.read(indexes=alpha_bidx),
indexes=alpha_bidx,
)

tags = src_dst.tags()

overview_resampling = tags.get(
Expand Down Expand Up @@ -118,7 +145,7 @@ def faux(

output_profile = src_dst.profile
output_profile.update(
dict(BIGTIFF=os.environ.get("BIGTIFF", "IF_SAFER"))
{"BIGTIFF": os.environ.get("BIGTIFF", "IF_SAFER")}
)
if creation_options:
output_profile.update(creation_options)
Expand Down
60 changes: 60 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import os

import numpy
import pytest
import rasterio
from click.testing import CliRunner
Expand Down Expand Up @@ -85,3 +86,62 @@ def test_tags():
assert src_dst.tags() == faux_dst.tags()
assert src_dst.descriptions == faux_dst.descriptions
assert src_dst.tags(0) == faux_dst.tags(0)


def test_alpha():
"""Check if alpha forwarded."""
runner = CliRunner()
src_path = os.path.join(os.path.dirname(__file__), "fixtures", "image_rgba.tif")
with runner.isolated_filesystem():
result = runner.invoke(faux, [src_path, "output.tif"])
assert not result.exception
assert result.exit_code == 0
with rasterio.open(src_path) as src_dst, rasterio.open(
"output.tif"
) as faux_dst:
assert src_dst.colorinterp == faux_dst.colorinterp
numpy.testing.assert_array_equal(
src_dst.read(indexes=4), faux_dst.read(indexes=4)
)


def test_mask():
"""Check if alpha forwarded."""
runner = CliRunner()
src_path = os.path.join(os.path.dirname(__file__), "fixtures", "image_rgb_mask.tif")
with runner.isolated_filesystem():
result = runner.invoke(faux, [src_path, "output.tif"])
assert not result.exception
assert result.exit_code == 0
with rasterio.open(src_path) as src_dst, rasterio.open(
"output.tif"
) as faux_dst:
assert src_dst.mask_flag_enums == faux_dst.mask_flag_enums
numpy.testing.assert_array_equal(
src_dst.dataset_mask(), faux_dst.dataset_mask()
)


def test_value():
"""Check value."""
runner = CliRunner()
src_path = os.path.join(os.path.dirname(__file__), "fixtures", "image_rgba.tif")
with runner.isolated_filesystem():
result = runner.invoke(faux, [src_path, "output.tif", "--value", 100])
assert not result.exception
assert result.exit_code == 0
with rasterio.open(src_path) as src_dst, rasterio.open(
"output.tif"
) as faux_dst:

assert numpy.unique(faux_dst.read(indexes=1)) == 100

# Check that we are casting values to the output data type
result = runner.invoke(faux, [src_path, "output.tif", "--value", 100.3])
assert not result.exception
assert result.exit_code == 0
with rasterio.open(src_path) as src_dst, rasterio.open(
"output.tif"
) as faux_dst:

assert numpy.unique(faux_dst.read(indexes=1)) == 100

0 comments on commit 6d2ba94

Please sign in to comment.