/
test_web.py
222 lines (188 loc) · 8.06 KB
/
test_web.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""tests rio_cogeo web."""
import os
import struct
import sys
import mercantile
import numpy
import pytest
import rasterio
from click.testing import CliRunner
from rasterio.warp import transform_bounds
from rio_tiler import reader as COGreader
from rio_cogeo.cogeo import cog_translate
from rio_cogeo.profiles import cog_profiles
from rio_cogeo.utils import get_zooms
raster_path_web = os.path.join(os.path.dirname(__file__), "fixtures", "image_web.tif")
raster_path_north = os.path.join(
os.path.dirname(__file__), "fixtures", "image_north.tif"
)
def test_cog_translate_webZooms():
"""
Test Web-Optimized COG.
- Test COG size is a multiple of 256 (mercator tile size)
- Test COG bounds are aligned with mercator grid at max zoom
- Test high resolution internal tiles are equal to mercator tile using
cogdumper and rio-tiler
- Test overview internal tiles are equal to mercator tile using
cogdumper and rio-tiler
"""
runner = CliRunner()
with runner.isolated_filesystem():
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = dict(GDAL_TIFF_OVR_BLOCKSIZE="128")
cog_translate(
raster_path_north,
"cogeo.tif",
web_profile,
quiet=True,
web_optimized=True,
config=config,
)
with rasterio.open("cogeo.tif") as out_dst:
_, maxzoom = get_zooms(out_dst)
assert maxzoom == 8
cog_translate(
raster_path_north,
"cogeo.tif",
web_profile,
quiet=True,
web_optimized=True,
latitude_adjustment=False,
config=config,
)
with rasterio.open("cogeo.tif") as out_dst:
_, maxzoom = get_zooms(out_dst)
assert maxzoom == 10
def test_cog_translate_web():
"""
Test Web-Optimized COG.
- Test COG size is a multiple of 256 (mercator tile size)
- Test COG bounds are aligned with mercator grid at max zoom
"""
runner = CliRunner()
with runner.isolated_filesystem():
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = dict(GDAL_TIFF_OVR_BLOCKSIZE="128")
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
web_optimized=True,
config=config,
)
with rasterio.open(raster_path_web) as src_dst:
with rasterio.open("cogeo.tif") as out_dst:
blocks = list(set(out_dst.block_shapes))
assert len(blocks) == 1
ts = blocks[0][0]
assert not out_dst.width % ts
assert not out_dst.height % ts
_, max_zoom = get_zooms(out_dst)
bounds = list(
transform_bounds(
src_dst.crs, "epsg:4326", *src_dst.bounds, densify_pts=21
)
)
ulTile = mercantile.xy_bounds(
mercantile.tile(bounds[0], bounds[3], max_zoom)
)
assert out_dst.bounds.left == ulTile.left
assert out_dst.bounds.top == ulTile.top
lrTile = mercantile.xy_bounds(
mercantile.tile(bounds[2], bounds[1], max_zoom)
)
assert out_dst.bounds.right == lrTile.right
assert round(out_dst.bounds.bottom, 6) == round(lrTile.bottom, 6)
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
def test_cog_translate_Internal():
"""
Test Web-Optimized COG.
- Test COG size is a multiple of 256 (mercator tile size)
- Test COG bounds are aligned with mercator grid at max zoom
- Test high resolution internal tiles are equal to mercator tile using
cogdumper and rio-tiler
- Test overview internal tiles are equal to mercator tile using
cogdumper and rio-tiler
"""
from cogdumper.cog_tiles import COGTiff
from cogdumper.filedumper import Reader as FileReader
runner = CliRunner()
with runner.isolated_filesystem():
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = dict(GDAL_TIFF_OVR_BLOCKSIZE="128")
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
web_optimized=True,
config=config,
)
with rasterio.open(raster_path_web) as src_dst:
with rasterio.open("cogeo.tif") as out_dst:
blocks = list(set(out_dst.block_shapes))
assert len(blocks) == 1
ts = blocks[0][0]
assert not out_dst.width % ts
assert not out_dst.height % ts
_, max_zoom = get_zooms(out_dst)
bounds = list(
transform_bounds(
src_dst.crs, "epsg:4326", *src_dst.bounds, densify_pts=21
)
)
minimumTile = mercantile.tile(bounds[0], bounds[3], max_zoom)
maximumTile = mercantile.tile(bounds[2], bounds[1], max_zoom)
with open("cogeo.tif", "rb") as out_body:
reader = FileReader(out_body)
cog = COGTiff(reader.read)
# High resolution
# Top Left tile
_, tile = cog.get_tile(0, 0, 0)
tile_length = 256 * 256 * 3
t = struct.unpack_from("{}b".format(tile_length), tile)
arr = numpy.array(t).reshape(256, 256, 3).astype(numpy.uint8)
arr = numpy.transpose(arr, [2, 0, 1])
with rasterio.open("cogeo.tif") as src_dst:
data, _ = COGreader.tile(
src_dst, *minimumTile, resampling_method="nearest"
)
numpy.testing.assert_array_equal(data, arr)
# Bottom right tile
_, tile = cog.get_tile(4, 3, 0)
tile_length = 256 * 256 * 3
t = struct.unpack_from("{}b".format(tile_length), tile)
arr = numpy.array(t).reshape(256, 256, 3).astype(numpy.uint8)
arr = numpy.transpose(arr, [2, 0, 1])
with rasterio.open("cogeo.tif") as src_dst:
data, _ = COGreader.tile(
src_dst, *maximumTile, resampling_method="nearest"
)
numpy.testing.assert_array_equal(data, arr)
# Low resolution (overview 1)
# Top Left tile
# NOTE: overview internal tile size is 128px
# We need to stack two internal tiles to compare with
# the 256px mercator tile fetched by rio-tiler
# ref: https://github.com/cogeotiff/rio-cogeo/issues/60
_, tile = cog.get_tile(1, 0, 1)
tile_length = 128 * 128 * 3
t = struct.unpack_from("{}b".format(tile_length), tile)
arr1 = numpy.array(t).reshape(128, 128, 3).astype(numpy.uint8)
arr1 = numpy.transpose(arr1, [2, 0, 1])
_, tile = cog.get_tile(2, 0, 1)
tile_length = 128 * 128 * 3
t = struct.unpack_from("{}b".format(tile_length), tile)
arr2 = numpy.array(t).reshape(128, 128, 3).astype(numpy.uint8)
arr2 = numpy.transpose(arr2, [2, 0, 1])
arr = numpy.dstack((arr1, arr2))
with rasterio.open("cogeo.tif") as src_dst:
data, _ = COGreader.tile(
src_dst, 118594, 60034, 17, resampling_method="nearest"
)
data = data[:, 128:, :]
numpy.testing.assert_array_equal(data, arr)