Skip to content

Commit

Permalink
fix CLI and value
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Apr 13, 2023
1 parent 1854b5b commit 05ec5f6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 37 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
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
]
47 changes: 36 additions & 11 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 All @@ -103,11 +130,9 @@ def faux(

indexes = src_dst.indexes

if forward_band_tags:
for i, b in enumerate(indexes):
tmp_dst.set_band_description(
i + 1, src_dst.descriptions[b - 1]
)
for i, b in enumerate(indexes):
tmp_dst.set_band_description(i + 1, src_dst.descriptions[b - 1])
if forward_band_tags:
tmp_dst.update_tags(i + 1, **src_dst.tags(b))

if forward_dataset_tags:
Expand All @@ -118,7 +143,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

0 comments on commit 05ec5f6

Please sign in to comment.