forked from perrygeo/python-rasterstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
132 lines (115 loc) · 5.44 KB
/
test_cli.py
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
import os.path
import json
import warnings
# Some warnings must be ignored to parse output properly
# https://github.com/pallets/click/issues/371#issuecomment-223790894
from click.testing import CliRunner
from rasterstats.cli import zonalstats, pointquery
def test_cli_feature():
raster = os.path.join(os.path.dirname(__file__), 'data/slope.tif')
vector = os.path.join(os.path.dirname(__file__), 'data/feature.geojson')
runner = CliRunner()
warnings.simplefilter('ignore')
result = runner.invoke(zonalstats, [vector,
'--raster', raster,
'--stats', 'mean',
'--prefix', 'test_'])
assert result.exit_code == 0
outdata = json.loads(result.output)
assert len(outdata['features']) == 1
feature = outdata['features'][0]
assert 'test_mean' in feature['properties']
assert round(feature['properties']['test_mean'], 2) == 14.66
assert 'test_count' not in feature['properties']
def test_cli_feature_stdin():
raster = os.path.join(os.path.dirname(__file__), 'data/slope.tif')
vector = os.path.join(os.path.dirname(__file__), 'data/feature.geojson')
runner = CliRunner()
warnings.simplefilter('ignore')
result = runner.invoke(zonalstats,
['--raster', raster,
'--stats', 'all',
'--prefix', 'test_'],
input=open(vector, 'r').read())
assert result.exit_code == 0
outdata = json.loads(result.output)
assert len(outdata['features']) == 1
feature = outdata['features'][0]
assert 'test_mean' in feature['properties']
assert 'test_std' in feature['properties']
def test_cli_features_sequence():
raster = os.path.join(os.path.dirname(__file__), 'data/slope.tif')
vector = os.path.join(os.path.dirname(__file__), 'data/featurecollection.geojson')
runner = CliRunner()
result = runner.invoke(zonalstats, [vector,
'--raster', raster,
'--stats', 'mean',
'--prefix', 'test_',
'--sequence'])
assert result.exit_code == 0
results = result.output.splitlines()
for r in results:
outdata = json.loads(r)
assert outdata['type'] == 'Feature'
def test_cli_features_sequence_rs():
raster = os.path.join(os.path.dirname(__file__), 'data/slope.tif')
vector = os.path.join(os.path.dirname(__file__), 'data/featurecollection.geojson')
runner = CliRunner()
result = runner.invoke(zonalstats, [vector,
'--raster', raster,
'--stats', 'mean',
'--prefix', 'test_',
'--sequence', '--rs'])
assert result.exit_code == 0
# assert result.output.startswith(b'\x1e')
assert result.output[0] == '\x1e'
def test_cli_featurecollection():
raster = os.path.join(os.path.dirname(__file__), 'data/slope.tif')
vector = os.path.join(os.path.dirname(__file__), 'data/featurecollection.geojson')
runner = CliRunner()
result = runner.invoke(zonalstats, [vector,
'--raster', raster,
'--stats', 'mean',
'--prefix', 'test_'])
assert result.exit_code == 0
outdata = json.loads(result.output)
assert len(outdata['features']) == 2
feature = outdata['features'][0]
assert 'test_mean' in feature['properties']
assert round(feature['properties']['test_mean'], 2) == 14.66
assert 'test_count' not in feature['properties']
def test_cli_pointquery():
raster = os.path.join(os.path.dirname(__file__), 'data/slope.tif')
vector = os.path.join(os.path.dirname(__file__), 'data/featurecollection.geojson')
runner = CliRunner()
result = runner.invoke(pointquery, [vector,
'--raster', raster,
'--property-name', 'slope'])
assert result.exit_code == 0
outdata = json.loads(result.output)
assert len(outdata['features']) == 2
feature = outdata['features'][0]
assert 'slope' in feature['properties']
def test_cli_point_sequence():
raster = os.path.join(os.path.dirname(__file__), 'data/slope.tif')
vector = os.path.join(os.path.dirname(__file__), 'data/featurecollection.geojson')
runner = CliRunner()
result = runner.invoke(pointquery, [vector,
'--raster', raster,
'--property-name', 'slope',
'--sequence'])
assert result.exit_code == 0
results = result.output.splitlines()
for r in results:
outdata = json.loads(r)
assert outdata['type'] == 'Feature'
def test_cli_point_sequence_rs():
raster = os.path.join(os.path.dirname(__file__), 'data/slope.tif')
vector = os.path.join(os.path.dirname(__file__), 'data/featurecollection.geojson')
runner = CliRunner()
result = runner.invoke(pointquery, [vector,
'--raster', raster,
'--property-name', 'slope',
'--sequence', '--rs'])
assert result.exit_code == 0
assert result.output[0] == '\x1e'