Package madrona :: Package raster_stats :: Module tests
[hide private]

Source Code for Module madrona.raster_stats.tests

  1  from madrona.common.test_settings_manager import SettingsTestCase as TestCase 
  2  from madrona.raster_stats.models import ZonalStatsCache, RasterDataset, zonal_stats, clear_cache 
  3  from django.contrib.gis.gdal.datasource import DataSource 
  4  from django.core import serializers 
  5  import os 
  6  import sys 
  7   
8 -def test_data():
9 rastpath = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data/impact.tif') 10 rast, created = RasterDataset.objects.get_or_create(name="test_impact",filepath=rastpath,type='continuous') 11 12 polygons = [] 13 14 shp = os.path.join(os.path.dirname(__file__), 'test_data/shapes.shp') 15 ds = DataSource(shp) 16 lyr = ds[0] 17 assert len(lyr) == 4 18 for feat in lyr: 19 polygons.append(feat.geom.geos) 20 21 del(lyr) 22 del(ds) 23 return rast, polygons
24
25 -def test_categorical_data():
26 rastpath = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data/landuse.tif') 27 rast, created = RasterDataset.objects.get_or_create(name="test_landuse",filepath=rastpath,type='categorical') 28 29 polygons = [] 30 31 shp = os.path.join(os.path.dirname(__file__), 'test_data/poly1.shp') 32 ds = DataSource(shp) 33 lyr = ds[0] 34 for feat in lyr: 35 polygons.append(feat.geom.geos) 36 37 del(lyr) 38 del(ds) 39 return rast, polygons
40
41 -class ZonalTest(TestCase):
42
43 - def setUp(self):
44 clear_cache() 45 self.rast, self.polygons = test_data()
46
47 - def test_zonal_util(self):
48 """ 49 Tests that starspan works and stuff 50 """ 51 # shouldnt have any nulls 52 zonal = zonal_stats(self.polygons[0], self.rast) 53 self.assertEqual(zonal.nulls,0) 54 55 # doesnt even touch the raster, all should be null 56 zonal = zonal_stats(self.polygons[1], self.rast) 57 self.assertEqual(zonal.pixels,None) 58 59 # Partly on and partly off the raster 60 # no nulls but pixel count should be low 61 zonal = zonal_stats(self.polygons[2], self.rast) 62 self.assertEqual(zonal.nulls,0) 63 self.assertEqual(zonal.pixels,225) 64 65 # All on the raster but should have nulls 66 zonal = zonal_stats(self.polygons[3], self.rast) 67 self.assertEqual(zonal.nulls,279)
68
69 - def test_caching(self):
70 """ 71 Test that the caching mechanism works and we can turn it on/off 72 """ 73 clear_cache() 74 self.assertEqual(len(ZonalStatsCache.objects.all()), 0) 75 76 zonal = zonal_stats(self.polygons[0], self.rast) 77 self.assertEqual(zonal.from_cache, False) 78 self.assertEqual(len(ZonalStatsCache.objects.all()), 1) 79 80 zonal = zonal_stats(self.polygons[0], self.rast) 81 self.assertEqual(zonal.from_cache, True) 82 self.assertEqual(len(ZonalStatsCache.objects.all()), 1) 83 84 zonal = zonal_stats(self.polygons[0], self.rast, read_cache=False) 85 self.assertEqual(zonal.from_cache, False) 86 self.assertEqual(len(ZonalStatsCache.objects.all()), 1) 87 88 zonal = zonal_stats(self.polygons[3], self.rast) 89 self.assertEqual(zonal.from_cache, False) 90 self.assertEqual(len(ZonalStatsCache.objects.all()), 2) 91 92 zonal = zonal_stats(self.polygons[3], self.rast) 93 self.assertEqual(zonal.from_cache, True) 94 self.assertEqual(len(ZonalStatsCache.objects.all()), 2) 95 96 clear_cache() 97 self.assertEqual(len(ZonalStatsCache.objects.all()), 0)
98
99 -class ZonalWebServiceTest(TestCase):
100 urls = 'madrona.raster_stats.urls' 101
102 - def setUp(self):
103 clear_cache() 104 self.rast, self.polygons = test_data()
105
106 - def test_webservice(self):
107 data = {'geom_txt': self.polygons[0].wkt} 108 #self.settings_manager.set(ROOT_URLCONF = 'madrona.raster_stats.urls') 109 response = self.client.get('/test_impact/', data) 110 self.failUnlessEqual(response.status_code, 200) 111 112 for obj in serializers.deserialize("json", response.content): 113 web_zonal = obj.object 114 util_zonal = zonal_stats(self.polygons[0], self.rast, read_cache=False) 115 self.failUnlessEqual(web_zonal.avg, util_zonal.avg)
116
117 -class ZonalCategoriesTest(TestCase):
118 - def setUp(self):
119 clear_cache() 120 self.rast, self.polygons = test_categorical_data()
121
122 - def test_categories(self):
123 zonal = zonal_stats(self.polygons[0], self.rast) 124 sumpix = 0 125 for zc in zonal.categories.all(): 126 sumpix += zc.count 127 self.assertEqual(zonal.pixels, sumpix)
128