-
Notifications
You must be signed in to change notification settings - Fork 38
/
cli.py
141 lines (124 loc) · 3.64 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""rio_rgbify CLI."""
import click
import rasterio as rio
import numpy as np
from riomucho import RioMucho
import json
from rasterio.rio.options import creation_options
from rio_rgbify.encoders import data_to_rgb
from rio_rgbify.mbtiler import RGBTiler
def _rgb_worker(data, window, ij, g_args):
return data_to_rgb(
data[0][g_args["bidx"] - 1], g_args["base_val"], g_args["interval"], g_args["round_digits"]
)
@click.command("rgbify")
@click.argument("src_path", type=click.Path(exists=True))
@click.argument("dst_path", type=click.Path(exists=False))
@click.option(
"--base-val",
"-b",
type=float,
default=0,
help="The base value of which to base the output encoding on [DEFAULT=0]",
)
@click.option(
"--interval",
"-i",
type=float,
default=1,
help="Describes the precision of the output, by incrementing interval [DEFAULT=1]",
)
@click.option(
"--round-digits",
"-r",
type=int,
default=0,
help="Less significants encoded bits to be set to 0. Round the values, but have better images compression [DEFAULT=0]",
)
@click.option("--bidx", type=int, default=1, help="Band to encode [DEFAULT=1]")
@click.option(
"--max-z",
type=int,
default=None,
help="Maximum zoom to tile (.mbtiles output only)",
)
@click.option(
"--bounding-tile",
type=str,
default=None,
help="Bounding tile '[{x}, {y}, {z}]' to limit output tiles (.mbtiles output only)",
)
@click.option(
"--min-z",
type=int,
default=None,
help="Minimum zoom to tile (.mbtiles output only)",
)
@click.option(
"--format",
type=click.Choice(["png", "webp"]),
default="png",
help="Output tile format (.mbtiles output only)",
)
@click.option("--workers", "-j", type=int, default=4, help="Workers to run [DEFAULT=4]")
@click.option("--verbose", "-v", is_flag=True, default=False)
@click.pass_context
@creation_options
def rgbify(
ctx,
src_path,
dst_path,
base_val,
interval,
round_digits,
bidx,
max_z,
min_z,
bounding_tile,
format,
workers,
verbose,
creation_options,
):
"""rio-rgbify cli."""
if dst_path.split(".")[-1].lower() == "tif":
with rio.open(src_path) as src:
meta = src.profile.copy()
meta.update(count=3, dtype=np.uint8)
for c in creation_options:
meta[c] = creation_options[c]
gargs = {"interval": interval, "base_val": base_val, "round_digits": round_digits, "bidx": bidx}
with RioMucho(
[src_path], dst_path, _rgb_worker, options=meta, global_args=gargs
) as rm:
rm.run(workers)
elif dst_path.split(".")[-1].lower() == "mbtiles":
if min_z is None or max_z is None:
raise ValueError("Zoom range must be provided for mbtile output")
if max_z < min_z:
raise ValueError(
"Max zoom {0} must be greater than min zoom {1}".format(max_z, min_z)
)
if bounding_tile is not None:
try:
bounding_tile = json.loads(bounding_tile)
except Exception:
raise TypeError(
"Bounding tile of {0} is not valid".format(bounding_tile)
)
with RGBTiler(
src_path,
dst_path,
interval=interval,
base_val=base_val,
round_digits=round_digits,
format=format,
bounding_tile=bounding_tile,
max_z=max_z,
min_z=min_z,
) as tiler:
tiler.run(workers)
else:
raise ValueError(
"{} output filetype not supported".format(dst_path.split(".")[-1])
)