-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
92 lines (65 loc) · 2.74 KB
/
test.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
#!/usr/bin/env python
from os.path import join as pjoin, dirname
import unittest
import fiona
import numpy
import rasterio
from image_processing import segmentation
class TestSegmentation(unittest.TestCase):
"""
Test that the segmentation module, (rasterisation, segments)
works as expected.
"""
def setUp(self):
data_dir = pjoin(dirname(__file__), 'data')
self.raster_fname = pjoin(data_dir, 'LS5-2008-02-25.kea')
self.vector_3577 = pjoin(data_dir, 'sample_polygon_3577.shp')
self.vector_4326 = pjoin(data_dir, 'sample_polygon_4326.shp')
with fiona.open(self.vector_3577) as src:
self.crs_3577 = src.crs_wkt
with fiona.open(self.vector_4326) as src:
self.crs_4326 = src.crs_wkt
with rasterio.open(self.raster_fname) as src:
self.transform = src.affine
self.dims = (src.height, src.width)
def test_same_proj(self):
ras = segmentation.rasterise_vector(self.vector_4326,
raster_filename=self.raster_fname)
self.assertEqual(ras.sum(), 40000)
def test_different_proj(self):
ras = segmentation.rasterise_vector(self.vector_3577,
raster_filename=self.raster_fname)
self.assertEqual(ras.sum(), 40000)
def test_input_crs(self):
ras = segmentation.rasterise_vector(self.vector_3577, shape=self.dims,
transform=self.transform,
crs=self.crs_4326)
self.assertEqual(ras.sum(), 40000)
def test_single_segment(self):
data = numpy.ones((400, 400), dtype='int8')
seg = segmentation.Segments(data)
self.assertEqual(1, seg.n_segments)
def test_zero_segments(self):
data = numpy.zeros((400, 400), dtype='int8')
seg = segmentation.Segments(data)
self.assertEqual(0, seg.n_segments)
def test_include_zero(self):
data = numpy.zeros((400, 400), dtype='int8')
data[0:100, 0:100] = 1
seg = segmentation.Segments(data, include_zero=True)
self.assertEqual(2, seg.n_segments)
def test_multiple_segments(self):
data = numpy.arange(400*400).reshape((400, 400))
seg = segmentation.Segments(data)
self.assertEqual(159999, seg.n_segments)
def test_segment_lookup(self):
"""
This test is pretty much already covered by the
idl-functions package.
"""
data = numpy.arange(400*400).reshape((400, 400))
seg = segmentation.Segments(data)
idx = seg.locations(6666)
self.assertTrue((data[idx] == data[data == 6666]).all())
if __name__ == '__main__':
unittest.main()