Skip to content

Commit

Permalink
Merge pull request #39 from mapbox/rasterio1b
Browse files Browse the repository at this point in the history
Adapt to rasterio 1.0b1
  • Loading branch information
vincentsarago authored Jun 15, 2018
2 parents bb9aca2 + 54b8fba commit 273d2b7
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ common: &common
working_directory: ~/raster-tester
steps:
- checkout
- run: sudo apt-get -y --no-install-recommends --force-yes install gdal-bin libgdal-dev
- run: sudo pip install virtualenv
- run: virtualenv venv
- run:
Expand All @@ -20,6 +19,7 @@ common: &common
~/.local/bin/coverage xml
~/.local/bin/codecov
fi
when: always

jobs:
"python-2.7":
Expand Down
8 changes: 4 additions & 4 deletions raster_tester/aligned.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
import rasterio


def tiled(sources, equal_blocks=True):
""" Tests if raster sources have the same tiling
optionally assert that their block sizes are equal (default: True)
Expand All @@ -10,7 +10,7 @@ def tiled(sources, equal_blocks=True):
for source in sources:
with rasterio.open(source) as src:

if not src.profile['tiled']:
if not src.is_tiled:
return (False, "Source(s) are not internally tiled")

if equal_blocks:
Expand All @@ -37,10 +37,10 @@ def aligned(sources):
with rasterio.open(source) as src:

if atransform:
if src.affine != atransform:
if src.transform != atransform:
return (False, "Affine transforms are not aligned")
else:
atransform = src.affine
atransform = src.transform

if shape:
if src.shape != shape:
Expand Down
23 changes: 12 additions & 11 deletions raster_tester/compare.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#!/usr/bin/env python

from affine import Affine
import click
import numpy as np

from affine import Affine

import rasterio
from rasterio.crs import CRS
from rasterio.warp import reproject
try:
from rasterio.crs import CRS
except:
CRS = None
try:
from rasterio.warp import RESAMPLING
except ImportError:
from rasterio.enums import Resampling as RESAMPLING

from rasterio.enums import Resampling as RESAMPLING
from rasterio.coords import BoundingBox

from .utils import exception_raiser
Expand Down Expand Up @@ -116,13 +112,15 @@ def compare(srcpath1, srcpath2, max_px_diff=0, upsample=1, downsample=1,
"In flex mode, %s and %s must 3 and 4, or 4 and 3 bands "
"respectively (received %s and %s)" % (
srcpath1, srcpath2, src1.count, src2.count), no_stderr)
return
else:
props = ['count', 'crs', 'dtypes', 'driver', 'bounds',
'height', 'width', 'shape', 'nodatavals']
'height', 'width', 'shape', 'nodatavals']
propCompare = compare_properties(src1, src2, props)

if propCompare:
exception_raiser(propCompare, no_stderr)
return

if compare_masked and src1.count == 4 and not flex_mode:
# create arrays for decimated reading
Expand All @@ -140,6 +138,7 @@ def compare(srcpath1, srcpath2, max_px_diff=0, upsample=1, downsample=1,
if aboveThreshold:
exception_raiser(
'Mask has %s pixels that vary by more than 16' % (difference), no_stderr)
return

elif compare_masked and flex_mode:
masked_1 = make_fill_array(
Expand All @@ -158,6 +157,7 @@ def compare(srcpath1, srcpath2, max_px_diff=0, upsample=1, downsample=1,
exception_raiser(
'Mask has %s pixels that vary by more than 16' % (difference),
no_stderr)
return

for bidx in range(1, count1 + compareAlpha):
# create arrays for decimated reading
Expand Down Expand Up @@ -187,6 +187,7 @@ def compare(srcpath1, srcpath2, max_px_diff=0, upsample=1, downsample=1,
if aboveThreshold:
exception_raiser('Band %s has %s pixels that vary by more than 16' % (
bidx, difference), no_stderr)
return

click.echo("ok - %s is similar to within %s pixels of %s" %
(srcpath1, max_px_diff, srcpath2))
5 changes: 4 additions & 1 deletion raster_tester/crosses_dateline.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import numpy as np

import rasterio as rio
from rasterio import warp
import numpy as np

def _wrap_x_coord(xcoords):
'''
Expand All @@ -18,6 +20,7 @@ def _wrap_x_coord(xcoords):
'''
return ((xcoords + 180) % 360) - 180


def winding_order(boundsArr):
'''
returns False if CCW; True is CW
Expand Down
3 changes: 2 additions & 1 deletion raster_tester/empty.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import click, sys

import numpy as np
import rasterio as rio


def array_hasdata(arr):
return np.any(arr)


def is_empty(input_path, randomize, bidx):
with rio.open(input_path) as src:
windows = [window for ij, window in src.block_windows()]
Expand Down
53 changes: 15 additions & 38 deletions raster_tester/scripts/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click

import sys

import click
import raster_tester


Expand All @@ -9,7 +10,7 @@ def cli():
pass


@click.command("compare")
@cli.command("compare")
@click.argument("input_1", type=click.Path(exists=True))
@click.argument("input_2", type=click.Path(exists=True))
@click.option("--pixel-threshold", "-p", type=int, default=0,
Expand All @@ -33,71 +34,47 @@ def compare(input_1, input_2, pixel_threshold, upsample, downsample,
compare_masked, no_error, debug, flex_mode)


cli.add_command(compare)


@click.command("isempty")
@cli.command("isempty")
@click.argument("input_1", type=click.Path(exists=True))
@click.option('--bidx', '-b', default=4,
help="Bands to blob [default = 4]")
@click.option("--randomize", is_flag=True,
help='iterate through windows in a psuedorandom fashion')
def isempty(input_1, randomize, bidx):
empty = raster_tester.is_empty(input_1, randomize, bidx, )
exits = {
True: ("is empty", 0),
False: ("is not empty", 1)
}

message, eCode = exits[empty]

click.echo("%s %s" % (input_1, message))
sys.exit(eCode)

cli.add_command(isempty)
if empty:
click.echo('{} is empty'.format(input_1))
else:
raise click.ClickException("{} is not empty".format(input_1))


@click.command("isaligned")
@cli.command("isaligned")
@click.argument('sources', required=True, nargs=-1)
def isaligned(sources):
aligned, msg = raster_tester.aligned(sources)
if aligned:
click.echo("ok: {} are aligned ({})".format(', '.join(sources), msg))
sys.exit(0)
else:
click.echo("not ok: {} are not aligned ({})".format(', '.join(sources), msg))
sys.exit(1)
raise click.ClickException("not ok: {} are not aligned ({})".format(', '.join(sources), msg))

cli.add_command(isaligned)


@click.command("istiled")
@cli.command("istiled")
@click.argument('sources', required=True, nargs=-1)
@click.option('--blocksize/--no-blocksize', is_flag=True, default=True,
help="assert that sources are internally tiled")
def istiled(sources, blocksize):
result, msg = raster_tester.tiled(sources, blocksize)
if result:
click.echo("ok: {} are tiled ({})".format(', '.join(sources), msg))
sys.exit(0)
else:
click.echo("not ok: {} are not all tiled ({})".format(', '.join(sources), msg))
sys.exit(1)
raise click.ClickException("not ok: {} are not all tiled ({})".format(', '.join(sources), msg))

cli.add_command(istiled)

@click.command('crossesdateline')
@cli.command('crossesdateline')
@click.argument('input', type=click.Path(exists=True))
def crossesdateline(input):
result = raster_tester.crosses_dateline(input)
if result:
click.echo('%s crosses dateline; exit 1' % (input))
sys.exit(1)
raise click.ClickException('{} crosses dateline; exit 1'.format(input))
else:
click.echo('%s does not cross dateline; exit 0' % (input))
sys.exit(0)

cli.add_command(crossesdateline)

if __name__ == "__main__":
cli()
click.echo('{} does not cross dateline; exit 0'.format(input))
6 changes: 2 additions & 4 deletions raster_tester/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env python
import sys

import click


def exception_raiser(message, no_stderr):
if no_stderr:
click.echo("not ok - %s" % (message))
sys.exit(0)
else:
raise ValueError(message)
raise click.ClickException("not ok - {}".format(message))
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
click
rasterio
rasterio>=1.0b1
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@


setup(name='raster-tester',
version='0.10.0',
version='0.11.0',
description=u"Tools for testing rasters",
long_description=long_description,
classifiers=[],
classifiers=[
'Topic :: Scientific/Engineering :: GIS',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6'
],
keywords='',
author=u"Camilla Mahon",
author_email='[email protected]',
Expand Down
2 changes: 2 additions & 0 deletions tests/test_aligned.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from click.testing import CliRunner

from raster_tester.scripts.cli import cli
import raster_tester

Expand Down
Loading

0 comments on commit 273d2b7

Please sign in to comment.