|
4 | 4 | import click
|
5 | 5 | import numpy as np
|
6 | 6 | 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 |
8 | 12 | from rasterio.coords import BoundingBox
|
9 | 13 |
|
10 | 14 | from .utils import exception_raiser
|
@@ -58,8 +62,8 @@ def make_fill_array(height, width, downsample, dtype):
|
58 | 62 | def compare_properties(src1, src2, properties, tol=1e-6):
|
59 | 63 | noMatch = []
|
60 | 64 | for prop in properties:
|
61 |
| - a = src1.__getattribute__(prop) |
62 |
| - b = src2.__getattribute__(prop) |
| 65 | + a = getattr(src1, prop) |
| 66 | + b = getattr(src2, prop) |
63 | 67 | equal = True
|
64 | 68 | if isinstance(a, BoundingBox) and isinstance(b, BoundingBox):
|
65 | 69 | for coord_a, coord_b in zip(tuple(a), tuple(b)):
|
@@ -89,91 +93,92 @@ def compare_properties(src1, src2, properties, tol=1e-6):
|
89 | 93 |
|
90 | 94 | def compare(srcpath1, srcpath2, max_px_diff=0, upsample=1, downsample=1,
|
91 | 95 | 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) |
177 | 182 |
|
178 | 183 | click.echo("ok - %s is similar to within %s pixels of %s" %
|
179 | 184 | (srcpath1, max_px_diff, srcpath2))
|
0 commit comments