Skip to content

Commit 8087892

Browse files
committed
Merge pull request #3 from mapbox/test-setup
resampling w/ rasterio
2 parents 953bfac + ea2d01e commit 8087892

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
# raster-tester
22

3+
```
4+
_______________ _______________
5+
|_|_|_|_|_|_|_|_| |_|_|_|_|_|_|_|_|
6+
|_|_|_|_|_|_|_|_| HIRU |_|_|_|_|_|_|_|_|
7+
|_|_|_|_|_|_|_|_| DIFF |_|_|_|_|_|_|_|_|
8+
|_|_|_|_|_|_|_|_| FROM |_|_|_|_|_|_|_|_|
9+
|_|_|_|_|_|_|_|_| ===> |_|_|_|_|_|_|_|_|
10+
|_|_|_|_|_|_|_|_| |_|_|_|_|_|_|_|_|
11+
12+
```
13+
14+
## compare
15+
316
```
417
Usage: raster-tester compare [OPTIONS] INPUT_1 INPUT_2
518
619
Options:
720
-p, --pixel-threshold INTEGER threshold for pixel diffs
8-
-r, --resample FLOAT If the image is lossy, resample to handle
21+
-r, --resample INTEGER If the image is lossy, resample to handle
922
variation in compression artifacts
1023
--help Show this message and exit.
1124
```

README.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
raster-tester
2+
=============
3+
4+
::
5+
6+
_______________ _______________
7+
|_|_|_|_|_|_|_|_| |_|_|_|_|_|_|_|_|
8+
|_|_|_|_|_|_|_|_| HIRU |_|_|_|_|_|_|_|_|
9+
|_|_|_|_|_|_|_|_| DIFF |_|_|_|_|_|_|_|_|
10+
|_|_|_|_|_|_|_|_| FROM |_|_|_|_|_|_|_|_|
11+
|_|_|_|_|_|_|_|_| ===> |_|_|_|_|_|_|_|_|
12+
|_|_|_|_|_|_|_|_| |_|_|_|_|_|_|_|_|
13+
14+
compare
15+
-------
16+
17+
::
18+
19+
Usage: raster-tester compare [OPTIONS] INPUT_1 INPUT_2
20+
21+
Options:
22+
-p, --pixel-threshold INTEGER threshold for pixel diffs
23+
-r, --resample INTEGER If the image is lossy, resample to handle
24+
variation in compression artifacts
25+
--help Show this message and exit.

raster_tester/__init__.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
#!/usr/bin/env python
22

3+
import click
4+
35
import numpy as np
46
import rasterio as rio
5-
import scipy.ndimage
7+
from rasterio.warp import reproject, RESAMPLING
8+
from rasterio import Affine
9+
10+
def affaux(up):
11+
return Affine(1, 0, 0, 0, -1, 0), Affine(up, 0, 0, 0, -up, 0)
12+
13+
def upsample(bidx, up, fr, to):
14+
upBidx = np.empty((bidx.shape[0] * up, bidx.shape[1] * up), dtype=bidx.dtype)
15+
16+
reproject(
17+
bidx, upBidx,
18+
src_transform=fr,
19+
dst_transform=to,
20+
src_crs="EPSG:3857",
21+
dst_crs="EPSG:3857",
22+
resampling=RESAMPLING.bilinear)
23+
24+
return upBidx
625

726
def compare(srcpath1, srcpath2, max_px_diff=0, resample=1):
827
with rio.drivers():
@@ -21,15 +40,18 @@ def compare(srcpath1, srcpath2, max_px_diff=0, resample=1):
2140

2241
for bidx in range(1, count1 + 1):
2342
band1 = src1.read(bidx, masked=False).astype(np.int16)
24-
band1 = scipy.ndimage.zoom(band1, float(resample), order=1)
2543
band2 = src2.read(bidx, masked=False).astype(np.int16)
26-
band2 = scipy.ndimage.zoom(band2, float(resample), order=1)
44+
45+
if resample > 1:
46+
toAff, frAff = affaux(resample)
47+
band1 = upsample(band1, resample, frAff, toAff)
48+
band2 = upsample(band2, resample, frAff, toAff)
2749

2850
diff = np.absolute(band1 - band2)
2951
threshold = np.zeros(band1.shape)
3052
outliers = np.where(diff > 16)
3153
if outliers[0].size > max_px_diff:
32-
print outliers[0]
54+
click.echo(outliers[0], err=True)
3355
assert outliers[0].size <= max_px_diff, "band %s has %d pixels which differ by > 16" % (bidx, outliers[0].size)
3456

3557
src1.close()

raster_tester/scripts/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def cli():
1111
@click.argument("input_2", type=click.Path(exists=True))
1212
@click.option("--pixel-threshold", "-p", type=int, default=0,
1313
help='threshold for pixel diffs')
14-
@click.option("--resample", "-r", type=float, default=1.0,
14+
@click.option("--resample", "-r", type=int, default=1,
1515
help='If the image is lossy, resample to handle variation in compression artifacts')
1616
def compare(input_1, input_2, pixel_threshold, resample):
1717
raster_tester.compare(input_1, input_2, pixel_threshold, resample)

0 commit comments

Comments
 (0)