Skip to content

Commit 6f32cad

Browse files
authored
Merge pull request #26 from mapbox/upgrayedd
[WIP] updates for rasterio 1.0 and python 3
2 parents 73e4c91 + a42a210 commit 6f32cad

File tree

8 files changed

+173
-146
lines changed

8 files changed

+173
-146
lines changed

circle.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
machine:
2-
python:
3-
version: 2.7.11
4-
51
dependencies:
62
pre:
73
- sudo add-apt-repository -y ppa:ubuntugis/ppa
84
- sudo apt-get update
95
- sudo apt-get -y --no-install-recommends --force-yes install python-numpy libgdal1h gdal-bin libgdal-dev
106
override:
7+
- pip install tox tox-pyenv
8+
- pyenv local 2.7.10 3.4.3 3.5.0
119
- pip install -U pip
1210
- pip install numpy
1311
- pip install -r requirements.txt
1412
- pip install -e ".[test]"
1513

1614
test:
17-
override:
18-
- py.test --cov raster_tester --cov-report term-missing --ignore=venv
1915
post:
2016
- codecov -t afb96efd-4a17-4272-a1bf-33317cad5462
2117
- codecov

raster_tester/aligned.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def tiled(sources, equal_blocks=True):
66
optionally assert that their block sizes are equal (default: True)
77
"""
88
blocksize = None
9-
with rasterio.drivers():
9+
with rasterio.Env():
1010
for source in sources:
1111
with rasterio.open(source) as src:
1212

@@ -32,7 +32,7 @@ def aligned(sources):
3232
"""
3333
atransform = None
3434
shape = None
35-
with rasterio.drivers():
35+
with rasterio.Env():
3636
for source in sources:
3737
with rasterio.open(source) as src:
3838

raster_tester/compare.py

Lines changed: 93 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import click
55
import numpy as np
66
import rasterio
7-
from rasterio.warp import reproject, RESAMPLING
7+
from rasterio.warp import reproject
8+
try:
9+
from rasterio.warp import RESAMPLING
10+
except ImportError:
11+
from rasterio.enums import Resampling as RESAMPLING
812
from rasterio.coords import BoundingBox
913

1014
from .utils import exception_raiser
@@ -58,8 +62,8 @@ def make_fill_array(height, width, downsample, dtype):
5862
def compare_properties(src1, src2, properties, tol=1e-6):
5963
noMatch = []
6064
for prop in properties:
61-
a = src1.__getattribute__(prop)
62-
b = src2.__getattribute__(prop)
65+
a = getattr(src1, prop)
66+
b = getattr(src2, prop)
6367
equal = True
6468
if isinstance(a, BoundingBox) and isinstance(b, BoundingBox):
6569
for coord_a, coord_b in zip(tuple(a), tuple(b)):
@@ -89,91 +93,92 @@ def compare_properties(src1, src2, properties, tol=1e-6):
8993

9094
def compare(srcpath1, srcpath2, max_px_diff=0, upsample=1, downsample=1,
9195
compare_masked=True, no_stderr=False, debug=False, flex_mode=False):
92-
with rasterio.open(srcpath1) as src1:
93-
with rasterio.open(srcpath2) as src2:
94-
95-
count1 = src1.count
96-
count2 = src2.count
97-
compareAlpha = 1
98-
99-
if flex_mode and [count1, count2].count(3) != 0:
100-
props = ['crs', 'driver', 'bounds', 'height', 'width', 'shape']
101-
if src1.count * src2.count != 12 or src1.count + src2.count != 7:
102-
exception_raiser(
103-
"In flex mode, %s and %s must 3 and 4, or 4 and 3 bands "
104-
"respectively (received %s and %s)" % (
105-
srcpath1, srcpath2, src1.count, src2.count), no_stderr)
106-
else:
107-
props = ['count', 'crs', 'dtypes', 'driver', 'bounds',
108-
'height', 'width', 'shape', 'nodatavals']
109-
propCompare = compare_properties(src1, src2, props)
110-
111-
if propCompare:
112-
exception_raiser(propCompare, no_stderr)
113-
114-
if compare_masked and src1.count == 4 and not flex_mode:
115-
# create arrays for decimated reading
116-
masked_1 = make_fill_array(
117-
src1.height, src1.width, downsample, src1.meta['dtype'])
118-
masked_2 = make_fill_array(
119-
src2.height, src2.width, downsample, src2.meta['dtype'])
120-
121-
src1.read(4, out=masked_1, masked=False)
122-
src2.read(4, out=masked_2, masked=False)
123-
compareAlpha = 0
124-
difference, aboveThreshold = array_compare(
125-
masked_1, masked_2, 16, max_px_diff, debug)
126-
127-
if aboveThreshold:
128-
exception_raiser(
129-
'Mask has %s pixels that vary by more than 16' % (difference), no_stderr)
130-
131-
elif compare_masked and flex_mode:
132-
masked_1 = make_fill_array(
133-
src1.height, src1.width, downsample, src1.meta['dtype'])
134-
masked_2 = make_fill_array(
135-
src2.height, src2.width, downsample, src2.meta['dtype'])
136-
137-
src1.read_masks(1, out=masked_1)
138-
src2.read_masks(1, out=masked_2)
139-
compareAlpha = 0
140-
141-
difference, aboveThreshold = array_compare(
142-
masked_1, masked_2, 16, max_px_diff, debug)
143-
144-
if aboveThreshold:
145-
exception_raiser(
146-
'Mask has %s pixels that vary by more than 16' % (difference),
147-
no_stderr)
148-
149-
for bidx in range(1, count1 + compareAlpha):
150-
# create arrays for decimated reading
151-
band1 = make_fill_array(
152-
src1.height, src1.width, downsample, src1.meta['dtype'])
153-
band2 = make_fill_array(
154-
src2.height, src2.width, downsample, src2.meta['dtype'])
155-
156-
src1.read(bidx, out=band1, masked=False)
157-
band1 = band1.astype(np.int16)
158-
159-
src2.read(bidx, out=band2, masked=False)
160-
band2 = band2.astype(np.int16)
161-
162-
if compare_masked and src1.count == 4:
163-
band1[masked_1 != 255] = 0
164-
band2[masked_1 != 255] = 0
165-
166-
if upsample > 1:
167-
toAff, frAff = affaux(upsample)
168-
band1 = upsample_array(band1, upsample, frAff, toAff)
169-
band2 = upsample_array(band2, upsample, frAff, toAff)
170-
171-
difference, aboveThreshold = array_compare(
172-
band1, band2, 16, max_px_diff, debug)
173-
174-
if aboveThreshold:
175-
exception_raiser('Band %s has %s pixels that vary by more than 16' % (
176-
bidx, difference), no_stderr)
96+
with rasterio.Env(GTIFF_IMPLICIT_JPEG_OVR=False):
97+
with rasterio.open(srcpath1) as src1:
98+
with rasterio.open(srcpath2) as src2:
99+
100+
count1 = src1.count
101+
count2 = src2.count
102+
compareAlpha = 1
103+
104+
if flex_mode and [count1, count2].count(3) != 0:
105+
props = ['crs', 'driver', 'bounds', 'height', 'width', 'shape']
106+
if src1.count * src2.count != 12 or src1.count + src2.count != 7:
107+
exception_raiser(
108+
"In flex mode, %s and %s must 3 and 4, or 4 and 3 bands "
109+
"respectively (received %s and %s)" % (
110+
srcpath1, srcpath2, src1.count, src2.count), no_stderr)
111+
else:
112+
props = ['count', 'crs', 'dtypes', 'driver', 'bounds',
113+
'height', 'width', 'shape', 'nodatavals']
114+
propCompare = compare_properties(src1, src2, props)
115+
116+
if propCompare:
117+
exception_raiser(propCompare, no_stderr)
118+
119+
if compare_masked and src1.count == 4 and not flex_mode:
120+
# create arrays for decimated reading
121+
masked_1 = make_fill_array(
122+
src1.height, src1.width, downsample, src1.meta['dtype'])
123+
masked_2 = make_fill_array(
124+
src2.height, src2.width, downsample, src2.meta['dtype'])
125+
126+
src1.read(4, out=masked_1, masked=False)
127+
src2.read(4, out=masked_2, masked=False)
128+
compareAlpha = 0
129+
difference, aboveThreshold = array_compare(
130+
masked_1, masked_2, 16, max_px_diff, debug)
131+
132+
if aboveThreshold:
133+
exception_raiser(
134+
'Mask has %s pixels that vary by more than 16' % (difference), no_stderr)
135+
136+
elif compare_masked and flex_mode:
137+
masked_1 = make_fill_array(
138+
src1.height, src1.width, downsample, src1.meta['dtype'])
139+
masked_2 = make_fill_array(
140+
src2.height, src2.width, downsample, src2.meta['dtype'])
141+
142+
src1.read_masks(1, out=masked_1)
143+
src2.read_masks(1, out=masked_2)
144+
compareAlpha = 0
145+
146+
difference, aboveThreshold = array_compare(
147+
masked_1, masked_2, 16, max_px_diff, debug)
148+
149+
if aboveThreshold:
150+
exception_raiser(
151+
'Mask has %s pixels that vary by more than 16' % (difference),
152+
no_stderr)
153+
154+
for bidx in range(1, count1 + compareAlpha):
155+
# create arrays for decimated reading
156+
band1 = make_fill_array(
157+
src1.height, src1.width, downsample, src1.meta['dtype'])
158+
band2 = make_fill_array(
159+
src2.height, src2.width, downsample, src2.meta['dtype'])
160+
161+
src1.read(bidx, out=band1, masked=False)
162+
band1 = band1.astype(np.int16)
163+
164+
src2.read(bidx, out=band2, masked=False)
165+
band2 = band2.astype(np.int16)
166+
167+
if compare_masked and src1.count == 4:
168+
band1[masked_1 != 255] = 0
169+
band2[masked_1 != 255] = 0
170+
171+
if upsample > 1:
172+
toAff, frAff = affaux(upsample)
173+
band1 = upsample_array(band1, upsample, frAff, toAff)
174+
band2 = upsample_array(band2, upsample, frAff, toAff)
175+
176+
difference, aboveThreshold = array_compare(
177+
band1, band2, 16, max_px_diff, debug)
178+
179+
if aboveThreshold:
180+
exception_raiser('Band %s has %s pixels that vary by more than 16' % (
181+
bidx, difference), no_stderr)
177182

178183
click.echo("ok - %s is similar to within %s pixels of %s" %
179184
(srcpath1, max_px_diff, srcpath2))

raster_tester/crosses_dateline.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
from rasterio import warp
33
import numpy as np
44

5+
def _wrap_x_coord(xcoords):
6+
'''
7+
Wraps longitude coords beyond -180 / 180
8+
9+
Parameters
10+
-----------
11+
xcoords: ndarray or value
12+
coordinate or coordinates to wrap.
13+
14+
Returns
15+
--------
16+
wrapped: ndarray or value
17+
wrapped coordinate(s)
18+
'''
19+
return ((xcoords + 180) % 360) - 180
520

621
def winding_order(boundsArr):
722
'''
@@ -48,11 +63,16 @@ def transform_bounds(boundsArr, crs):
4863
if not crs:
4964
raise ValueError('Input raster must have a CRS')
5065

51-
return np.dstack(warp.transform(
52-
crs,
53-
{'init': 'epsg:4326'},
54-
boundsArr[:, 0],
55-
boundsArr[:, 1]))[0]
66+
if 'init' in crs and crs['init'] == 'epsg:4326':
67+
boundsArr[:, 0] = _wrap_x_coord(boundsArr[:, 0])
68+
else:
69+
boundsArr = np.dstack(warp.transform(
70+
crs,
71+
{'init': 'epsg:4326'},
72+
boundsArr[:, 0],
73+
boundsArr[:, 1]))[0]
74+
75+
return boundsArr
5676

5777

5878
def crosses_dateline(fpath):

0 commit comments

Comments
 (0)