Skip to content

Commit dcfc0e9

Browse files
committed
Add rgb and rgba webp and manually allow defining colorinterp
1 parent fb4b469 commit dcfc0e9

8 files changed

+194
-9
lines changed

rasterio_generated/fixtures/uint8_rgb_deflate_block64_cog.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44

55
import numpy as np
6+
from rasterio.enums import ColorInterp
67

78
from rasterio_generated.write_utils import write_cog
89

@@ -27,4 +28,5 @@ def generate(output_path: Path) -> None:
2728
data,
2829
blocksize=64,
2930
compress="DEFLATE",
31+
colorinterp=[ColorInterp.red, ColorInterp.green, ColorInterp.blue],
3032
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Generate an RGB GeoTIFF with DEFLATE compression."""
2+
3+
from pathlib import Path
4+
5+
import numpy as np
6+
from rasterio.enums import ColorInterp
7+
8+
from rasterio_generated.write_utils import write_cog
9+
10+
11+
def generate(output_path: Path) -> None:
12+
"""Generate a 256x256 RGB GeoTIFF with DEFLATE compression."""
13+
# Create RGB gradient pattern
14+
r = np.linspace(0, 127, 128, dtype=np.uint8)
15+
g = np.linspace(127, 0, 128, dtype=np.uint8)
16+
b = np.full(128, 128, dtype=np.uint8)
17+
18+
data = np.stack(
19+
[
20+
np.tile(r, (128, 1)),
21+
np.tile(g.reshape(-1, 1), (1, 128)),
22+
np.tile(b, (128, 1)),
23+
]
24+
)
25+
26+
write_cog(
27+
output_path,
28+
data,
29+
blocksize=64,
30+
compress="WEBP",
31+
colorinterp=[ColorInterp.red, ColorInterp.green, ColorInterp.blue],
32+
)
1.74 KB
Binary file not shown.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
```
2+
Driver: GTiff
3+
File: rasterio_generated/fixtures/uint8_rgb_webp_block64_cog.tif
4+
COG: True
5+
Compression: WEBP
6+
ColorSpace: None
7+
8+
Profile
9+
Width: 128
10+
Height: 128
11+
Bands: 3
12+
Tiled: True
13+
Dtype: uint8
14+
NoData: 0.0
15+
Alpha Band: False
16+
Internal Mask: False
17+
Interleave: PIXEL
18+
ColorMap: False
19+
ColorInterp: ('red', 'green', 'blue')
20+
Scales: (1.0, 1.0, 1.0)
21+
Offsets: (0.0, 0.0, 0.0)
22+
23+
Geo
24+
Crs: EPSG:4326
25+
Origin: (0.0, 0.0)
26+
Resolution: (0.01, -0.01)
27+
BoundingBox: (0.0, -1.28, 1.28, 0.0)
28+
MinZoom: 7
29+
MaxZoom: 7
30+
31+
Image Metadata
32+
AREA_OR_POINT: Area
33+
34+
Image Structure
35+
LAYOUT: COG
36+
COMPRESSION: WEBP
37+
INTERLEAVE: PIXEL
38+
OVERVIEW_RESAMPLING: BILINEAR
39+
COMPRESSION_REVERSIBILITY: LOSSY
40+
WEBP_LEVEL: 75
41+
42+
Band 1
43+
ColorInterp: red
44+
45+
Band 2
46+
ColorInterp: green
47+
48+
Band 3
49+
ColorInterp: blue
50+
51+
IFD
52+
Id Size BlockSize Decimation
53+
0 128x128 64x64 0
54+
1 64x64 64x64 2
55+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Generate an RGB GeoTIFF with DEFLATE compression."""
2+
3+
from pathlib import Path
4+
5+
import numpy as np
6+
from rasterio.enums import ColorInterp
7+
8+
from rasterio_generated.write_utils import write_cog
9+
10+
11+
def generate(output_path: Path) -> None:
12+
"""Generate a 256x256 RGB GeoTIFF with DEFLATE compression."""
13+
# Create RGB gradient pattern
14+
r = np.linspace(0, 127, 128, dtype=np.uint8)
15+
g = np.linspace(127, 0, 128, dtype=np.uint8)
16+
b = np.full(128, 128, dtype=np.uint8)
17+
a = np.full(128, 255, dtype=np.uint8)
18+
19+
data = np.stack(
20+
[
21+
np.tile(r, (128, 1)),
22+
np.tile(g.reshape(-1, 1), (1, 128)),
23+
np.tile(b, (128, 1)),
24+
np.tile(a.reshape(-1, 1), (1, 128)),
25+
]
26+
)
27+
28+
write_cog(
29+
output_path,
30+
data,
31+
blocksize=64,
32+
compress="WEBP",
33+
colorinterp=[
34+
ColorInterp.red,
35+
ColorInterp.green,
36+
ColorInterp.blue,
37+
ColorInterp.alpha,
38+
],
39+
)
1.78 KB
Binary file not shown.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
```
2+
Driver: GTiff
3+
File: rasterio_generated/fixtures/uint8_rgba_webp_block64_cog.tif
4+
COG: True
5+
Compression: WEBP
6+
ColorSpace: None
7+
8+
Profile
9+
Width: 128
10+
Height: 128
11+
Bands: 4
12+
Tiled: True
13+
Dtype: uint8
14+
NoData: 0.0
15+
Alpha Band: True
16+
Internal Mask: False
17+
Interleave: PIXEL
18+
ColorMap: False
19+
ColorInterp: ('red', 'green', 'blue', 'alpha')
20+
Scales: (1.0, 1.0, 1.0, 1.0)
21+
Offsets: (0.0, 0.0, 0.0, 0.0)
22+
23+
Geo
24+
Crs: EPSG:4326
25+
Origin: (0.0, 0.0)
26+
Resolution: (0.01, -0.01)
27+
BoundingBox: (0.0, -1.28, 1.28, 0.0)
28+
MinZoom: 7
29+
MaxZoom: 7
30+
31+
Image Metadata
32+
AREA_OR_POINT: Area
33+
34+
Image Structure
35+
LAYOUT: COG
36+
COMPRESSION: WEBP
37+
INTERLEAVE: PIXEL
38+
OVERVIEW_RESAMPLING: BILINEAR
39+
COMPRESSION_REVERSIBILITY: LOSSY
40+
WEBP_LEVEL: 75
41+
42+
Band 1
43+
ColorInterp: red
44+
45+
Band 2
46+
ColorInterp: green
47+
48+
Band 3
49+
ColorInterp: blue
50+
51+
Band 4
52+
ColorInterp: alpha
53+
54+
IFD
55+
Id Size BlockSize Decimation
56+
0 128x128 64x64 0
57+
1 64x64 64x64 2
58+
```

rasterio_generated/write_utils.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def write_cog(
4141
predictor: Literal[2, 3] | None = None,
4242
nodata: int | float | None = None,
4343
rasterio_env: dict[str, str | bool | int] | None = None,
44+
colorinterp: list[ColorInterp] | None = None,
4445
):
4546
"""Write a COG to disk.
4647
@@ -106,19 +107,17 @@ def write_cog(
106107
with rasterio.Env(env):
107108
with MemoryFile() as memfile:
108109
with memfile.open(**src_profile) as mem:
109-
if nband == 3:
110-
ci = [ColorInterp.red, ColorInterp.green, ColorInterp.blue]
111-
112-
else:
113-
ci = [ColorInterp.gray]
114-
if nband > 1:
115-
ci += [ColorInterp.undefined] * (nband - 1)
110+
if colorinterp is not None:
111+
assert len(colorinterp) == mem.count
116112

117113
if nodata_type == "alpha" and mask is not None:
118114
data = np.concatenate([data, mask])
119-
ci += [ColorInterp.alpha]
115+
assert (
116+
colorinterp is not None and colorinterp[-1] == ColorInterp.alpha
117+
)
120118

121-
mem.colorinterp = ci
119+
if colorinterp is not None:
120+
mem.colorinterp = colorinterp
122121

123122
# Write Data
124123
if nband == 1:

0 commit comments

Comments
 (0)