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
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
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
42
46
48 """
49 Tests that starspan works and stuff
50 """
51
52 zonal = zonal_stats(self.polygons[0], self.rast)
53 self.assertEqual(zonal.nulls,0)
54
55
56 zonal = zonal_stats(self.polygons[1], self.rast)
57 self.assertEqual(zonal.pixels,None)
58
59
60
61 zonal = zonal_stats(self.polygons[2], self.rast)
62 self.assertEqual(zonal.nulls,0)
63 self.assertEqual(zonal.pixels,225)
64
65
66 zonal = zonal_stats(self.polygons[3], self.rast)
67 self.assertEqual(zonal.nulls,279)
68
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
100 urls = 'madrona.raster_stats.urls'
101
105
107 data = {'geom_txt': self.polygons[0].wkt}
108
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
121
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