Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

from django.db import models 

from django.conf import settings 

from django.core import serializers 

from django.db.utils import DatabaseError 

from madrona.common.utils import get_logger 

import tempfile 

import time 

import os 

 

logger = get_logger() 

 

try: 

    RASTDIR = settings.RASTER_DIR 

except: 

    RASTDIR = os.path.join(os.path.dirname(__file__), 'test_data') 

 

RASTER_TYPES = ( 

                ("continuous", "continuous"), 

                ("categorical", "catgorical"), 

               ) 

 

STARSPAN_BIN = settings.STARSPAN_BIN 

 

class RasterDataset(models.Model): 

    name = models.CharField(max_length=30, unique=True) 

    full_name = models.CharField(max_length=255, default="") 

    filepath = models.FilePathField(max_length=255, path=RASTDIR, recursive=True) 

    type = models.CharField(max_length=30, choices=RASTER_TYPES) 

 

    def __unicode__(self): 

        return unicode(self.name + " raster at " + self.filepath) 

 

    def save(self, *args, **kwargs): 

        super(RasterDataset, self).save(*args, **kwargs) 

        # invalidate the zonal stats cache 

        caches = ZonalStatsCache.objects.filter(raster=self) 

        caches.delete() 

 

    @property 

    def is_valid(self): 

        # TODO is there a CHEAP way to check for GDAL open? 

        if not os.path.exists(self.filepath): 

            return False 

        return True 

 

class ZonalCategory(models.Model): 

    category = models.IntegerField() 

    count = models.IntegerField() 

 

    def __unicode__(self): 

        return unicode("Category %s (count = %s)" % (self.category, self.count)) 

 

class ZonalStatsCache(models.Model): 

    geom_hash = models.CharField(max_length=255) 

    raster = models.ForeignKey('RasterDataset') 

    sum = models.FloatField(null=True, blank=True) 

    avg = models.FloatField(null=True, blank=True) 

    min = models.FloatField(null=True, blank=True) 

    max = models.FloatField(null=True, blank=True) 

    mode = models.FloatField(null=True, blank=True) 

    median = models.FloatField(null=True, blank=True) 

    stdev = models.FloatField(null=True, blank=True) 

    nulls = models.FloatField(null=True, blank=True) 

    pixels = models.FloatField(null=True, blank=True) 

    categories = models.ManyToManyField(ZonalCategory) 

    date_modified = models.DateTimeField(auto_now=True) 

 

    @property 

    def json(self): 

        return serializers.serialize("json", self) 

 

    def __unicode__(self): 

        return unicode("Zonal Stats for %s - avg:%s , pixels:%s, nulls:%s" % (self.raster.name, self.avg, self.pixels, self.nulls)) 

 

    class Meta: 

        unique_together = ('geom_hash', 'raster') 

 

def geom_to_file(geom, filepath): 

    json = """{ 

"type": "FeatureCollection", 

"features": [ 

{ "type": "Feature", "properties": { "id": 1 }, "geometry": %s } 

] 

}""" % geom.json 

    fh = open(filepath,'w') 

    fh.write(json) 

    fh.close() 

    assert os.path.exists(filepath) 

 

def _run_starspan_zonal(geom, rasterds, pixprop=0.5): 

    """ 

    Consider this a 'private' method .. dont call directly, use zonal_stats() instead 

    Runs starspan and returns a ZonalStatsCache object 

    """ 

    # Create tempdir and cd in  

    tmpdir_base = tempfile.gettempdir() 

    geom_hash = geom.wkt.__hash__() 

    timestamp = str(time.time()) 

    tmpdir = os.path.join(tmpdir_base, 'madrona.raster_stats', str(geom_hash), timestamp, rasterds.name) 

    os.makedirs(tmpdir) 

    old_dir = os.getcwd() 

    os.chdir(tmpdir) 

 

    # Output geom to temp dataset 

    out_json = os.path.join(tmpdir, 'geom_%s.json' % timestamp) 

    geom_to_file(geom, out_json) 

 

    # Run starspan 

    out_csv = os.path.join(tmpdir, 'output_%s_stats.csv' % timestamp) 

    out_categories = os.path.join(tmpdir, 'output_%s_categories.csv' % timestamp) 

    if os.path.exists(out_csv): 

        os.remove(out_csv) 

    if os.path.exists(out_categories): 

        os.remove(out_categories) 

    cmd = '%s --vector %s --where "id=1" --out-prefix %s/output_%s --out-type table --summary-suffix _stats.csv --raster %s --stats avg mode median min max sum stdev nulls --pixprop %s ' % (STARSPAN_BIN,out_json,tmpdir, timestamp, rasterds.filepath, pixprop) 

    if rasterds.type == 'categorical': 

        cmd += "--class-summary-suffix _categories.csv" 

 

    starspan_out = os.popen(cmd).read() 

 

    if not os.path.exists(out_csv): 

        raise Exception("Starspan failed to produce output file: %s" % starspan_out) 

    if rasterds.type == 'categorical' and not os.path.exists(out_categories): 

        raise Exception("Starspan failed to produce output file: %s" % starspan_out) 

 

    res = open(out_csv,'r').readlines() 

 

    # Create zonal model 

    zonal, created = ZonalStatsCache.objects.get_or_create(raster=rasterds, geom_hash=geom_hash) 

 

    # Make sure we have valid results output by starspan 

    if len(res) == 2 and "Intersecting features: 0" not in starspan_out: 

        headers = [x.strip() for x in res[0].split(',')] 

        vals = [x.strip() for x in res[1].split(',')] 

        assert len(headers) == len(vals) 

 

        # loop and populate model 

        for i in range(len(headers)): 

            if "_Band1" in headers[i]: 

                stat_type = headers[i].replace("_Band1",'') 

                zonal.__dict__[stat_type] = float(vals[i]) 

            elif headers[i] == 'numPixels': 

                zonal.pixels = float(vals[i]) 

 

    # Handle categories 

    if rasterds.type == 'categorical' and zonal.pixels: 

        zonal.save() 

        res = open(out_categories).readlines() 

        headers = [x.strip() for x in res[0].split(',')] 

        assert headers == ['FID', 'class', 'count'] 

        for row in res[1:]: 

            vals = [int(x.strip()) for x in row.split(',')] 

            cat = vals[1] 

            count = vals[2] 

            zc = ZonalCategory.objects.create(category=cat, count=count) 

            zc.save() 

            zonal.categories.add(zc) 

 

    try: 

        if zonal.pixels: 

            zonal.save() 

    except: 

        # Most likely another zonal stats cache for this geom/raster 

        # was saved to the cache before this one completed. 

        pass 

 

    if settings.STARSPAN_REMOVE_TMP: 

        os.chdir(old_dir) 

        import shutil 

        shutil.rmtree(tmpdir) 

 

    return zonal 

 

def clear_cache(): 

    objs = ZonalStatsCache.objects.all() 

    objs.delete() 

 

def zonal_stats(geom, rasterds, read_cache=True, cache_only=False, pixprop=0.5): 

    """ 

    Given a GEOSGeometry and a RasterDataset, 

    compute the zonal stats and return json like 

     { 'raster': 'elevation', 'stats': {'sum': 10234.2, 'mean': 12.4}} 

    result will be stored in cache 

     and cache value is returned if read_cache 

    """ 

    if not geom.valid: 

        return None 

 

    geom_hash = geom.wkt.__hash__() 

 

    cached = None 

    if read_cache: 

        try: 

            cached = ZonalStatsCache.objects.get(geom_hash=geom_hash, raster=rasterds) 

        except ZonalStatsCache.DoesNotExist: 

            cached = None 

        except DatabaseError: 

            cached = None 

 

    if cached: 

        result = cached 

        result.from_cache = True 

    else: 

        if cache_only: 

            # Return an empty result 

            result = ZonalStatsCache(geom_hash=geom_hash, raster=rasterds) 

        else: 

            result = _run_starspan_zonal(geom, rasterds, pixprop=pixprop) 

        result.from_cache = False 

 

    return result