def raster(self): imfield = self.image # Check _file attr to avoid opening a file handle. if isinstance(getattr(imfield, '_file', None), MemFileIO): path = imfield.file.name else: path = self.image.path return greenwich.Raster(path)
def raster(self): imfield = self.image # Check _file attr to avoid opening a file handle. fileobj = getattr(imfield, '_file', None) if isinstance(fileobj, MemFileIO): path = imfield.file.name elif fileobj and fileobj.name.startswith(tempfile.gettempdir()): path = fileobj.name else: path = self.image.path return greenwich.Raster(path)
def array(self, geom=None, stat=None): with greenwich.Raster(self.image.path) as r: if geom: if geom.num_coords > 1: with r.clip(geom) as clipped: arr = clipped.masked_array() else: coord_px = r.affine.transform((geom.coords,)).pop() arr = r.ReadAsArray(*(coord_px + (1, 1))) else: arr = r.masked_array() if arr is not None: if stat: arr = getattr(np.ma, stat)(arr) if arr.size == 1: arr = arr.item() return arr raise ValueError('Failure reading array values')
def convert(self, format=None, geom=None): imgpath = self.image.path # Handle format as .tif, tif, or tif.zip ext = format or os.path.splitext(imgpath)[-1][1:] ext = os.path.splitext(ext)[0] driver = greenwich.driver_for_path('base.%s' % ext) # No conversion is needed if the original format without clipping # is requested. if not geom and imgpath.endswith(ext): return memio = MemFileIO() if geom: with greenwich.Raster(imgpath) as r: with r.clip(geom) as clipped: clipped.save(memio, driver) else: driver.copy(imgpath, memio.name) self.image.file = memio
def clean_fields(self, *args, **kwargs): # Override this instead of save() so that fields are populated on # save() *or* manager methods like RasterStore.objects.create(). if not self.image.storage.exists(self.image): self.image.save(self.image.name, self.image, save=False) with greenwich.Raster(self.image.path) as r: band = r[-1] bmin, bmax = band.GetMinimum(), band.GetMaximum() if bmin is None or bmax is None: bmin, bmax = band.ComputeRasterMinMax() self.geom = buffer(r.envelope.polygon.ExportToWkb()) if r.sref.srid: self.geom.srid = r.sref.srid self.xpixsize, self.ypixsize = r.affine.scale self.width, self.height = r.size self.minval = bmin self.maxval = bmax self.nodata = r.nodata self.srs = r.sref.wkt if self.event is None: self.event = datetime.date.today() super(AbstractRasterStore, self).clean_fields(*args, **kwargs)
def warp(self, format=None, srid=None, geom=None): """Returns a new RasterQuerySet with possibly warped/converted rasters. Keyword args: format -- raster file extension format as str geom -- geometry for masking or spatial subsetting srid -- spatial reference identifier as int for warping to """ clone = self._clone() for obj in clone: obj.convert(format, geom) if srid: f = obj.image.file if isinstance(f, MemFileIO): r = greenwich.Raster(f.name) else: r = obj.raster() memio = MemFileIO(delete=False) dswarp = r.warp(srid, memio) obj.image.file = memio dswarp.close() r.close() return clone
def convert(self, format=None, geom=None): imgpath = self.image.path # Handle format as .tif, tif, or tif.zip ext = format or os.path.splitext(imgpath)[-1][1:] ext = os.path.splitext(ext)[0] # No conversion is needed if the original format without clipping # is requested. if not geom and imgpath.endswith(ext): return driver = greenwich.driver_for_path(ext, _imgdrivers) # Allow overriding of default driver settings. settings = self.driver_settings.get(ext) if settings: driver.settings = settings memio = MemFileIO() if geom: with greenwich.Raster(imgpath) as r, r.clip(geom) as clipped: clipped.save(memio, driver) else: driver.copy(imgpath, memio.name) self.image.name = os.extsep.join( (os.path.splitext(os.path.basename(imgpath))[0], ext)) self.image.file = memio
def quantiles(self, k=5): """Returns an ndarray of quantile breaks.""" with greenwich.Raster(self.image.path) as rast: arr = rast.masked_array() q = list(np.linspace(0, 100, k)) return np.percentile(arr.compressed(), q)
def raster(self): return greenwich.Raster(self.image.path)