forked from perrygeo/python-rasterstats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_io.py
More file actions
386 lines (277 loc) · 10.6 KB
/
test_io.py
File metadata and controls
386 lines (277 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import json
from pathlib import Path
import fiona
import numpy as np
import pytest
import rasterio
from shapely.geometry import shape
from rasterstats.io import ( # todo parse_feature
fiona_generator,
Raster,
boundless_array,
bounds_window,
read_featurecollection,
read_features,
rowcol,
window_bounds,
)
data_dir = Path(__file__).parent / "data"
polygons = data_dir / "polygons.shp"
raster = data_dir / "slope.tif"
arr = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
arr3d = np.array([[[1, 1, 1], [1, 1, 1], [1, 1, 1]]])
eps = 1e-6
target_features = [f for f in fiona_generator(polygons)]
target_geoms = [shape(f["geometry"]) for f in target_features]
def _compare_geomlists(aa, bb):
for a, b in zip(aa, bb):
assert a.equals_exact(b, eps)
def _test_read_features(indata):
features = list(read_features(indata))
# multi
geoms = [shape(f["geometry"]) for f in features]
_compare_geomlists(geoms, target_geoms)
def _test_read_features_single(indata):
# single (first target geom)
geom = shape(list(read_features(indata))[0]["geometry"])
assert geom.equals_exact(target_geoms[0], eps)
def test_fiona_path():
assert list(read_features(polygons)) == target_features
def test_layer_index():
layer = fiona.listlayers(data_dir).index("polygons")
assert list(read_features(data_dir, layer=layer)) == target_features
def test_layer_name():
assert list(read_features(data_dir, layer="polygons")) == target_features
def test_path_unicode():
try:
upolygons = unicode(polygons)
except NameError:
# python3, it's already unicode
upolygons = polygons
assert list(read_features(upolygons)) == target_features
def test_featurecollection():
assert (
read_featurecollection(polygons)["features"]
== list(read_features(polygons))
== target_features
)
def test_shapely():
indata = [shape(f["geometry"]) for f in fiona_generator(polygons)]
_test_read_features(indata)
_test_read_features_single(indata[0])
def test_wkt():
indata = [shape(f["geometry"]).wkt for f in fiona_generator(polygons)]
_test_read_features(indata)
_test_read_features_single(indata[0])
def test_wkb():
indata = [shape(f["geometry"]).wkb for f in fiona_generator(polygons)]
_test_read_features(indata)
_test_read_features_single(indata[0])
def test_mapping_features():
# list of Features
indata = [f for f in fiona_generator(polygons)]
_test_read_features(indata)
def test_mapping_feature():
# list of Features
indata = [f for f in fiona_generator(polygons)]
_test_read_features(indata[0])
def test_mapping_geoms():
indata = [f for f in fiona_generator(polygons)]
_test_read_features(indata[0]["geometry"])
def test_mapping_collection():
indata = {"type": "FeatureCollection"}
indata["features"] = [f for f in fiona_generator(polygons)]
_test_read_features(indata)
def test_jsonstr():
# Feature str
indata = [f for f in fiona_generator(polygons)]
indata = json.dumps(indata[0])
_test_read_features(indata)
def test_jsonstr_geom():
# geojson geom str
indata = [f for f in fiona_generator(polygons)]
indata = json.dumps(indata[0]["geometry"])
_test_read_features(indata)
def test_jsonstr_collection():
indata = {"type": "FeatureCollection"}
indata["features"] = [f for f in fiona_generator(polygons)]
indata = json.dumps(indata)
_test_read_features(indata)
def test_jsonstr_collection_without_features():
indata = {"type": "FeatureCollection", "features": []}
indata = json.dumps(indata)
with pytest.raises(ValueError):
_test_read_features(indata)
def test_invalid_jsonstr():
indata = {"type": "InvalidGeometry", "coordinates": [30, 10]}
indata = json.dumps(indata)
with pytest.raises(ValueError):
_test_read_features(indata)
class MockGeoInterface:
def __init__(self, f):
self.__geo_interface__ = f
def test_geo_interface():
indata = [MockGeoInterface(f) for f in fiona_generator(polygons)]
_test_read_features(indata)
def test_geo_interface_geom():
indata = [MockGeoInterface(f["geometry"]) for f in fiona_generator(polygons)]
_test_read_features(indata)
def test_geo_interface_collection():
# geointerface for featurecollection?
indata = {"type": "FeatureCollection"}
indata["features"] = [f for f in fiona_generator(polygons)]
indata = MockGeoInterface(indata)
_test_read_features(indata)
def test_notafeature():
with pytest.raises(ValueError):
list(read_features(["foo", "POINT(-122 42)"]))
with pytest.raises(ValueError):
list(read_features(Exception()))
# Raster tests
def test_boundless():
# Exact
assert boundless_array(arr, window=((0, 3), (0, 3)), nodata=0).sum() == 9
# Intersects
assert boundless_array(arr, window=((-1, 2), (-1, 2)), nodata=0).sum() == 4
assert boundless_array(arr, window=((1, 4), (-1, 2)), nodata=0).sum() == 4
assert boundless_array(arr, window=((1, 4), (1, 4)), nodata=0).sum() == 4
assert boundless_array(arr, window=((-1, 2), (1, 4)), nodata=0).sum() == 4
# No overlap
assert boundless_array(arr, window=((-4, -1), (-4, -1)), nodata=0).sum() == 0
assert boundless_array(arr, window=((-4, -1), (4, 7)), nodata=0).sum() == 0
assert boundless_array(arr, window=((4, 7), (4, 7)), nodata=0).sum() == 0
assert boundless_array(arr, window=((4, 7), (-4, -1)), nodata=0).sum() == 0
assert boundless_array(arr, window=((-3, 0), (-3, 0)), nodata=0).sum() == 0
# Covers
assert boundless_array(arr, window=((-1, 4), (-1, 4)), nodata=0).sum() == 9
# 3D
assert boundless_array(arr3d, window=((0, 3), (0, 3)), nodata=0).sum() == 9
assert boundless_array(arr3d, window=((-1, 2), (-1, 2)), nodata=0).sum() == 4
assert boundless_array(arr3d, window=((-3, 0), (-3, 0)), nodata=0).sum() == 0
# 1D
with pytest.raises(ValueError):
boundless_array(np.array([1, 1, 1]), window=((0, 3),), nodata=0)
def test_boundless_masked():
a = boundless_array(arr, window=((-4, -1), (-4, -1)), nodata=0, masked=True)
assert a.mask.all()
b = boundless_array(arr, window=((0, 3), (0, 3)), nodata=0, masked=True)
assert not b.mask.any()
c = boundless_array(arr, window=((-1, 2), (-1, 2)), nodata=0, masked=True)
assert c.mask.any() and not c.mask.all()
def test_window_bounds():
with rasterio.open(raster) as src:
win = ((0, src.shape[0]), (0, src.shape[1]))
assert src.bounds == window_bounds(win, src.transform)
win = ((5, 10), (5, 10))
assert src.window_bounds(win) == window_bounds(win, src.transform)
def test_bounds_window():
with rasterio.open(raster) as src:
assert bounds_window(src.bounds, src.transform) == (
(0, src.shape[0]),
(0, src.shape[1]),
)
def test_rowcol():
import math
with rasterio.open(raster) as src:
x, _, _, y = src.bounds
x += 1.0
y -= 1.0
assert rowcol(x, y, src.transform, op=math.floor) == (0, 0)
assert rowcol(x, y, src.transform, op=math.ceil) == (1, 1)
def test_Raster_index():
x, y = 245114, 1000968
with rasterio.open(raster) as src:
c1, r1 = src.index(x, y)
with Raster(raster) as rast:
c2, r2 = rast.index(x, y)
assert c1 == c2
assert r1 == r2
def test_Raster():
import numpy as np
bounds = (244156, 1000258, 245114, 1000968)
r1 = Raster(raster, band=1).read(bounds)
with rasterio.open(raster) as src:
arr = src.read(1)
affine = src.transform
nodata = src.nodata
r2 = Raster(arr, affine, nodata, band=1).read(bounds)
with pytest.raises(ValueError):
r3 = Raster(arr, affine, nodata, band=1).read()
with pytest.raises(ValueError):
r4 = Raster(arr, affine, nodata, band=1).read(bounds=1, window=1)
# If the abstraction is correct, the arrays are equal
assert np.array_equal(r1.array, r2.array)
def test_Raster_boundless_disabled():
import numpy as np
bounds = (
244300.61494985913,
998877.8262535353,
246444.72726211764,
1000868.7876863468,
)
outside_bounds = (244156, 1000258, 245114, 1000968)
# rasterio src fails outside extent
with pytest.raises(ValueError):
r1 = Raster(raster, band=1).read(outside_bounds, boundless=False)
# rasterio src works inside extent
r2 = Raster(raster, band=1).read(bounds, boundless=False)
with rasterio.open(raster) as src:
arr = src.read(1)
affine = src.transform
nodata = src.nodata
# ndarray works inside extent
r3 = Raster(arr, affine, nodata, band=1).read(bounds, boundless=False)
# ndarray src fails outside extent
with pytest.raises(ValueError):
r4 = Raster(arr, affine, nodata, band=1).read(outside_bounds, boundless=False)
# If the abstraction is correct, the arrays are equal
assert np.array_equal(r2.array, r3.array)
def test_Raster_context():
# Assigned a regular name, stays open
r1 = Raster(raster, band=1)
assert not r1.src.closed
r1.src.close()
# Used as a context manager, closes itself
with Raster(raster, band=1) as r2:
pass
assert r2.src.closed
def test_geointerface():
class MockGeo:
def __init__(self, features):
self.__geo_interface__ = {"type": "FeatureCollection", "features": features}
# Make it iterable just to ensure that geo interface
# takes precendence over iterability
def __iter__(self):
pass
def __next__(self):
pass
def next(self):
pass
features = [
{
"type": "Feature",
"properties": {},
"geometry": {"type": "Point", "coordinates": [0, 0]},
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[-50, -10], [-40, 10], [-30, -10], [-50, -10]]],
},
},
]
geothing = MockGeo(features)
assert list(read_features(geothing)) == features
# Optional tests
def test_geodataframe():
gpd = pytest.importorskip("geopandas")
df = gpd.read_file(polygons)
if not hasattr(df, "__geo_interface__"):
pytest.skip("This version of geopandas doesn't support df.__geo_interface__")
assert list(read_features(df))
# TODO # io.parse_features on a feature-only geo_interface
# TODO # io.parse_features on a feature-only geojson-like object
# TODO # io.read_features on a feature-only
# TODO # io.Raster.read() on an open rasterio dataset