<s> import os <EOL> import datetime <EOL> from django . contrib . gis . db import models <EOL> from django . utils . deconstruct import deconstructible <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> import greenwich <EOL> from greenwich . io import MemFileIO <EOL> import numpy as np <EOL> from spillway . query import RasterQuerySet <EOL> @ deconstructible <EOL> class UploadDir ( object ) : <EOL> def __init__ ( self , path ) : <EOL> self . path = path <EOL> def __call__ ( self , instance , filename ) : <EOL> return os . path . join ( self . path , filename ) <EOL> upload_to = UploadDir ( '<STR_LIT:data>' ) <EOL> class AbstractRasterStore ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> image = models . FileField ( _ ( '<STR_LIT>' ) , upload_to = upload_to ) <EOL> width = models . IntegerField ( _ ( '<STR_LIT>' ) ) <EOL> height = models . IntegerField ( _ ( '<STR_LIT>' ) ) <EOL> geom = models . PolygonField ( _ ( '<STR_LIT>' ) ) <EOL> event = models . DateField ( ) <EOL> srs = models . TextField ( _ ( '<STR_LIT>' ) ) <EOL> minval = models . FloatField ( _ ( '<STR_LIT>' ) ) <EOL> maxval = models . FloatField ( _ ( '<STR_LIT>' ) ) <EOL> nodata = models . FloatField ( _ ( '<STR_LIT>' ) , blank = True , null = True ) <EOL> xpixsize = models . FloatField ( _ ( '<STR_LIT>' ) ) <EOL> ypixsize = models . FloatField ( _ ( '<STR_LIT>' ) ) <EOL> objects = RasterQuerySet ( ) <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT:image>' , '<STR_LIT>' ) <EOL> ordering = [ '<STR_LIT:image>' ] <EOL> get_latest_by = '<STR_LIT>' <EOL> abstract = True <EOL> def __unicode__ ( self ) : <EOL> return self . image . name <EOL> def clean_fields ( self , * args , ** kwargs ) : <EOL> if not self . image . storage . exists ( self . image ) : <EOL> self . image . save ( self . image . name , self . image , save = False ) <EOL> with greenwich . Raster ( self . image . path ) as r : <EOL> band = r [ - <NUM_LIT:1> ] <EOL> bmin , bmax = band . GetMinimum ( ) , band . GetMaximum ( ) <EOL> if bmin is None or bmax is None : <EOL> bmin , bmax = band . ComputeRasterMinMax ( ) <EOL> self . geom = buffer ( r . envelope . polygon . ExportToWkb ( ) ) <EOL> if r . sref . srid : <EOL> self . geom . srid = r . sref . srid <EOL> self . xpixsize , self . ypixsize = r . affine . scale <EOL> self . width , self . height = r . size <EOL> self . minval = bmin <EOL> self . maxval = bmax <EOL> self . nodata = r . nodata <EOL> self . srs = r . sref . wkt <EOL> if self . event is None : <EOL> self . event = datetime . date . today ( ) <EOL> super ( AbstractRasterStore , self ) . clean_fields ( * args , ** kwargs ) <EOL> def linear ( self , limits = None , k = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> start , stop = limits or ( self . minval , self . maxval ) <EOL> return np . linspace ( start , stop , k ) <EOL> def quantiles ( self , k = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> with greenwich . Raster ( self . image . path ) as rast : <EOL> arr = rast . masked_array ( ) <EOL> q = list ( np . linspace ( <NUM_LIT:0> , <NUM_LIT:100> , k ) ) <EOL> return np . percentile ( arr . compressed ( ) , q ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> self . full_clean ( ) <EOL> super ( AbstractRasterStore , self ) . save ( * args , ** kwargs ) <EOL> def array ( self , geom = None , stat = None ) : <EOL> with greenwich . Raster ( self . image . path ) as r : <EOL> if geom : <EOL> if geom . num_coords > <NUM_LIT:1> : <EOL> with r . clip ( geom ) as clipped : <EOL> arr = clipped . masked_array ( ) <EOL> else : <EOL> coord_px = r . affine . transform ( ( geom . coords , ) ) . pop ( ) <EOL> arr = r . ReadAsArray ( * ( coord_px + ( <NUM_LIT:1> , <NUM_LIT:1> ) ) ) <EOL> else : <EOL> arr = r . masked_array ( ) <EOL> if arr is not None : <EOL> if stat : <EOL> arr = getattr ( np . ma , stat ) ( arr ) <EOL> if arr . size == <NUM_LIT:1> : <EOL> arr = arr . item ( ) <EOL> return arr <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def raster ( self ) : <EOL> return greenwich . Raster ( self . image . path ) <EOL> def convert ( self , format = None , geom = None ) : <EOL> imgpath = self . image . path <EOL> ext = format or os . path . splitext ( imgpath ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> ext = os . path . splitext ( ext ) [ <NUM_LIT:0> ] <EOL> driver = greenwich . driver_for_path ( '<STR_LIT>' % ext ) <EOL> if not geom and imgpath . endswith ( ext ) : <EOL> return <EOL> memio = MemFileIO ( ) <EOL> if geom : <EOL> with greenwich . Raster ( imgpath ) as r : <EOL> with r . clip ( geom ) as clipped : <EOL> clipped . save ( memio , driver ) <EOL> else : <EOL> driver . copy ( imgpath , memio . name ) <EOL> self . image . file = memio </s>
