Skip to content

Commit

Permalink
Merge pull request #3 from mapbox/test-setup
Browse files Browse the repository at this point in the history
resampling w/ rasterio
  • Loading branch information
dnomadb committed Sep 23, 2015
2 parents 953bfac + ea2d01e commit 8087892
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
# raster-tester

```
_______________ _______________
|_|_|_|_|_|_|_|_| |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| HIRU |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| DIFF |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| FROM |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| ===> |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| |_|_|_|_|_|_|_|_|
```

## compare

```
Usage: raster-tester compare [OPTIONS] INPUT_1 INPUT_2
Options:
-p, --pixel-threshold INTEGER threshold for pixel diffs
-r, --resample FLOAT If the image is lossy, resample to handle
-r, --resample INTEGER If the image is lossy, resample to handle
variation in compression artifacts
--help Show this message and exit.
```
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
raster-tester
=============

::

_______________ _______________
|_|_|_|_|_|_|_|_| |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| HIRU |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| DIFF |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| FROM |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| ===> |_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_| |_|_|_|_|_|_|_|_|

compare
-------

::

Usage: raster-tester compare [OPTIONS] INPUT_1 INPUT_2

Options:
-p, --pixel-threshold INTEGER threshold for pixel diffs
-r, --resample INTEGER If the image is lossy, resample to handle
variation in compression artifacts
--help Show this message and exit.
30 changes: 26 additions & 4 deletions raster_tester/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
#!/usr/bin/env python

import click

import numpy as np
import rasterio as rio
import scipy.ndimage
from rasterio.warp import reproject, RESAMPLING
from rasterio import Affine

def affaux(up):
return Affine(1, 0, 0, 0, -1, 0), Affine(up, 0, 0, 0, -up, 0)

def upsample(bidx, up, fr, to):
upBidx = np.empty((bidx.shape[0] * up, bidx.shape[1] * up), dtype=bidx.dtype)

reproject(
bidx, upBidx,
src_transform=fr,
dst_transform=to,
src_crs="EPSG:3857",
dst_crs="EPSG:3857",
resampling=RESAMPLING.bilinear)

return upBidx

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

for bidx in range(1, count1 + 1):
band1 = src1.read(bidx, masked=False).astype(np.int16)
band1 = scipy.ndimage.zoom(band1, float(resample), order=1)
band2 = src2.read(bidx, masked=False).astype(np.int16)
band2 = scipy.ndimage.zoom(band2, float(resample), order=1)

if resample > 1:
toAff, frAff = affaux(resample)
band1 = upsample(band1, resample, frAff, toAff)
band2 = upsample(band2, resample, frAff, toAff)

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

src1.close()
Expand Down
2 changes: 1 addition & 1 deletion raster_tester/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def cli():
@click.argument("input_2", type=click.Path(exists=True))
@click.option("--pixel-threshold", "-p", type=int, default=0,
help='threshold for pixel diffs')
@click.option("--resample", "-r", type=float, default=1.0,
@click.option("--resample", "-r", type=int, default=1,
help='If the image is lossy, resample to handle variation in compression artifacts')
def compare(input_1, input_2, pixel_threshold, resample):
raster_tester.compare(input_1, input_2, pixel_threshold, resample)
Expand Down

0 comments on commit 8087892

Please sign in to comment.