forked from perrygeo/python-rasterstats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
73 lines (52 loc) · 1.87 KB
/
test_utils.py
File metadata and controls
73 lines (52 loc) · 1.87 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
from pathlib import Path
import pytest
from shapely.geometry import LineString
from rasterstats import zonal_stats
from rasterstats.utils import (
VALID_STATS,
boxify_points,
get_percentile,
remap_categories,
stats_to_csv,
)
data_dir = Path(__file__).parent / "data"
raster = data_dir / "slope.tif"
def test_csv():
polygons = data_dir / "polygons.shp"
stats = zonal_stats(polygons, raster, stats="*")
csv = stats_to_csv(stats)
assert csv.split()[0] == ",".join(sorted(VALID_STATS))
def test_categorical_csv():
polygons = data_dir / "polygons.shp"
categorical_raster = data_dir / "slope_classes.tif"
stats = zonal_stats(polygons, categorical_raster, categorical=True)
csv = stats_to_csv(stats)
assert csv.split()[0] == "1.0,2.0,5.0"
def test_get_percentile():
assert get_percentile("percentile_0") == 0.0
assert get_percentile("percentile_100") == 100.0
assert get_percentile("percentile_13.2") == 13.2
def test_get_bad_percentile():
with pytest.raises(ValueError):
get_percentile("foo")
with pytest.raises(ValueError):
get_percentile("percentile_101")
with pytest.raises(ValueError):
get_percentile("percentile_101")
with pytest.raises(ValueError):
get_percentile("percentile_-1")
with pytest.raises(ValueError):
get_percentile("percentile_foobar")
def test_remap_categories():
feature_stats = {1: 22.343, 2: 54.34, 3: 987.5}
category_map = {1: "grassland", 2: "forest"}
new_stats = remap_categories(category_map, feature_stats)
assert 1 not in new_stats.keys()
assert "grassland" in new_stats.keys()
assert 3 in new_stats.keys()
def test_boxify_non_point():
line = LineString([(0, 0), (1, 1)])
with pytest.raises(ValueError):
boxify_points(line, None)
# TODO # def test_boxify_multi_point
# TODO # def test_boxify_point