def getWindow(imageFile,x,y,winsize,site): """ Extracts portion of raster defined by window as an array. """ inProj = osr.SpatialReference() inProj.ImportFromEPSG(int('4326')) outProj = osr.SpatialReference() outProj.ImportFromEPSG(int('2835%s' % qvf.utmzone(imageFile)[-1])) transform = osr.CoordinateTransformation(inProj, outProj) (x, y, z) = transform.TransformPoint(x, y) x = float(x) y = float(y) winsize = int(cmdargs.winsize) imgInfo = gdalcommon.info(imageFile) trans = imgInfo.transform bands = imgInfo.rasterCount (row,col) = gdalcommon.eastNorth2rowCol(trans,x,y) (row,col) = (int(row), int(col)) temp = '%s_%s_%spix.tif' % (imageFile.split('.')[0],site.strip(),winsize) cmd = 'gdal_translate -srcwin %s %s %s %s %s %s' % (col-int(.5*winsize), row-int(.5*winsize), winsize, winsize, imageFile, temp) os.system(cmd) return temp
def getStatVal(imageFile,longitude,latitude,winsize,statistic,site): """ Caculates the statistics on the pixels in the window array """ band1,band2,band3,band4,band5,band6,count = 'None','None','None','None','None','None','None' if imageFile != 'None' and imageFile != None: imageFile=qvf.changestage(imageFile,'tmp') temp = '%s_%s_%spix.tif' % (imageFile.split('.')[0],site.strip(),winsize) if not os.path.exists(temp): subsetRaster = getWindow(imageFile,longitude,latitude,winsize,site) else: subsetRaster = temp try: imgInfo = gdalcommon.info(subsetRaster) handle = gdal.Open(subsetRaster) for band in [1,2,3,4,5,6]: if handle != None: bandHandle = handle.GetRasterBand(band) bandArray = bandHandle.ReadAsArray() maskedBand = ma.masked_values(bandArray, 0) count = ma.count(maskedBand) if statistic == 'mean': statVal = maskedBand.mean() elif statistic == 'std': statVal = maskedBand.std() else: statVal = None if band == 1: band1 = statVal elif band == 2: band2 = statVal elif band == 3: band3 = statVal elif band == 4: band4 = statVal elif band == 5: band5 = statVal elif band == 6: band6 = statVal except: pass return band1, band2, band3, band4, band5, band6, count