Beispiel #1
0
def stackBands(basename, b1, b2, b3):
        raster_1 = basename + '_B1.TIF'
        raster_2 = basename + '_B2.TIF'
        raster_3 = basename + '_B3.TIF'
        raster_4 = basename + '_B4.TIF'
        raster_5 = basename + '_B5.TIF'
        raster_6 = basename + '_B6.TIF'
        raster_7 = basename + '_B7.TIF'
        raster_8 = basename + '_B8.TIF'
        raster_9 = basename + '_B9.TIF'
        raster_10 = basename + '_B10.TIF'
        raster_11 = basename + '_B11.TIF'

        b_1 = int(b1) - 1
        b_2 = int(b2) - 1
        b_3 = int(b3) - 1

        fullBandNamesList = ['Coastal', 'Blue', 'Green', 'Red', 'NIR', 'SWIR1', 'SWIR2', 'Panchromatic', 'Cirrus', 'TIRS1',
                         'TIRS2']
        bandNamesList = [fullBandNamesList[b_1], fullBandNamesList[b_2], fullBandNamesList[b_3]]

        fullImageList = [raster_1, raster_2, raster_3, raster_4, raster_5, raster_6, raster_7, raster_8, raster_9,
                     raster_10, raster_11]
        imageList = [fullImageList[b_1], fullImageList[b_2], fullImageList[b_3]]

        outputName = basename + '_B' + b1 + b2 + b3 + '.TIF'
        gdalFormat = 'GTiff'
        dataType = rsgislib.TYPE_16UINT

        imageutils.stackImageBands(imageList, bandNamesList, outputName, None, 0, gdalFormat, dataType)
Beispiel #2
0
def npStackRast(band_stack, directory, rast_fn, band_nm, ref_img):
    """
    :param band_stack: Numpy array of raster stack
    :param directory: String of file location to put temp and output file in
    :param ref_img: gdal dataset of a reference image, used for driverTiff creation
    :param rast_fn: String of directory and file name of image stack file to be made
    :param band_nm: list of strings containing band names
    :return: Tif file which is a raster stack of standardised input bands and band ratios
    """
    bands = []
    for j in range(band_stack.shape[2]):
        bands.append(str(directory + 'tempband_' + str(j) +
                         '.kea'))  # add file name to list
        rasteriseOutput(ref_img,
                        str(directory + 'tempband_' + str(j) + '.kea'),
                        band_stack[:, :, j], 'KEA')
        # print(str(directory + 'stan_band_' + str(j) + '.tif'))
    # print(bands)
    # band_names = []
    band_names = band_nm.copy()
    band_names.append('NDVI')
    band_names.append('NDWI')
    band_names.append('NDWIveg')
    band_names.append('SWIRratio')
    band_names.append('RVI')
    band_names.append('NIRGreen')
    # print(band_names)
    imageutils.stackImageBands(bands, band_names, rast_fn, None,
                               float(-9999.0), 'KEA', rsgislib.TYPE_32FLOAT)

    # remove temp files
    for k in range(len(bands)):
        os.remove(str(directory + 'tempband_' + str(k) + '.kea'))
Beispiel #3
0
def main(argv):
   inputlist = ''
   outstack = ''
   try:
      opts, args = getopt.getopt(argv,"hi:o:",["inputlist=","outstack="])
   except getopt.GetoptError:
      print 'test.py -i <input list text file> -o <output stack>'
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print 'test.py -l <list of images> -o <output stack>' 
         sys.exit()
      elif opt in ("-i", "--inputlist"):
         inputlist = arg
      elif opt in ("-o", "--outstack"):
         outstack = arg
   print 'Input list is:', 		inputlist
   print 'Output file is:', 		outstack

   # create image stack
   with open(inputlist, 'r') as f:
   	bands_list = [line.strip() for line in f]
   with open(inputlist, 'r') as f:
      band_names = [line.strip() for line in f]
   
	#band_names = ['HH', 'HV', 'HH_HV', 'Mean_HH', 'Mean_HV', 'Var_HH', 'Var_HV','DEM_H', 'DEM_S', 'DEM_A']    #, 'dem', 'aspect', 'slope']
   gdaltype = rsgislib.TYPE_32FLOAT
   imageutils.stackImageBands(bands_list, band_names, outstack, -9999, 0, 'KEA', rsgislib.TYPE_32FLOAT)  
Beispiel #4
0
def main(argv):
    inputlist = ''
    outstack = ''
    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["inputlist=", "outstack="])
    except getopt.GetoptError:
        print 'test.py -i <input list text file> -o <output stack>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'test.py -l <list of images> -o <output stack>'
            sys.exit()
        elif opt in ("-i", "--inputlist"):
            inputlist = arg
        elif opt in ("-o", "--outstack"):
            outstack = arg
    print 'Input list is:', inputlist
    print 'Output file is:', outstack

    # create image stack
    with open(inputlist, 'r') as f:
        bands_list = [line.strip() for line in f]
    with open(inputlist, 'r') as f:
        band_names = [line.strip() for line in f]

#band_names = ['HH', 'HV', 'HH_HV', 'Mean_HH', 'Mean_HV', 'Var_HH', 'Var_HV','DEM_H', 'DEM_S', 'DEM_A']    #, 'dem', 'aspect', 'slope']
    gdaltype = rsgislib.TYPE_32FLOAT
    imageutils.stackImageBands(bands_list, band_names, outstack, -9999, 0,
                               'KEA', rsgislib.TYPE_32FLOAT)
Beispiel #5
0
def fullStack(basename):
        raster_1 = basename + '_B1.TIF'
        raster_2 = basename + '_B2.TIF'
        raster_3 = basename + '_B3.TIF'
        raster_4 = basename + '_B4.TIF'
        raster_5 = basename + '_B5.TIF'
        raster_6 = basename + '_B6.TIF'
        raster_7 = basename + '_B7.TIF'
        raster_8 = basename + '_B8.TIF'
        raster_9 = basename + '_B9.TIF'
        raster_10 = basename + '_B10.TIF'
        raster_11 = basename + '_B11.TIF'

        bandNamesList = ['Coastal', 'Blue', 'Green', 'Red', 'NIR', 'SWIR1', 'SWIR2', 'Cirrus']
        imageList = [raster_1, raster_2, raster_3, raster_4, raster_5, raster_6, raster_7, raster_9,]
        outputName = basename + '_fullstacked.TIF'
        gdalFormat = 'GTiff'
        dataType = rsgislib.TYPE_16UINT
        imageutils.stackImageBands(imageList, bandNamesList, outputName, None, 0, gdalFormat, dataType)
import rsgislib
from rsgislib import imageutils
from rsgislib import imagecalibration

imageList = ['./RAW/B02.jp2', './RAW/B03.jp2', './RAW/B04.jp2']
bandNamesList = ['Blue', 'Green', 'Red']
outputImageBGR = './Sen2_BGR.kea'
imageutils.stackImageBands(imageList, bandNamesList, outputImageBGR, None, 0,
                           'KEA', rsgislib.TYPE_16UINT)

imageList = ['./RAW/B08.jp2']
bandNamesList = ['NIR']
outputImageNIR = './Sen2_NIR.kea'
imageutils.stackImageBands(imageList, bandNamesList, outputImageNIR, None, 0,
                           'KEA', rsgislib.TYPE_16UINT)

imageList = [
    './RAW/B05.jp2', './RAW/B06.jp2', './RAW/B07.jp2', './RAW/B8A.jp2'
]
bandNamesList = ['RedEdge_B5', 'RedEdge_B6', 'RedEdge_B7', 'RedEdge_B8A']
outputImageRedEdge = './Sen2_RedEdge.kea'
imageutils.stackImageBands(imageList, bandNamesList, outputImageRedEdge, None,
                           0, 'KEA', rsgislib.TYPE_16UINT)

imageList = ['./RAW/B11.jp2', './RAW/B12.jp2']
bandNamesList = ['SWIR_B11', 'SWIR_B12']
outputImageSWIR = './Sen2_SWIR.kea'
imageutils.stackImageBands(imageList, bandNamesList, outputImageSWIR, None, 0,
                           'KEA', rsgislib.TYPE_16UINT)

outputImageRedEdge10m = './Sen2_RedEdge_10m.kea'
Beispiel #7
0
                    default='Float32',
                    help="Data type")
args = parser.parse_args()

# Get output extension from file
outFormat = getGDALFormat(args.outstack)

imageList = []
bandNamesList = []

fileList = os.listdir(args.indir)

replaceFileStrList = args.search.split('*')

for fName in fileList:
    if fnmatch.fnmatch(fName, args.search):  # Match search string
        imageList.append(os.path.join(args.indir, fName))
        for replaceFileStr in replaceFileStrList:
            bandName = fName.replace(replaceFileStr, '')
        bandNamesList.append(bandName)

gdalformat = 'KEA'
dataType = rsgislib.TYPE_32FLOAT

# Stack bands
imageutils.stackImageBands(imageList, bandNamesList, args.outstack, None, 0,
                           'KEA', getRSGISLibDataType(args.datatype))

# Calculate stats
imageutils.popImageStats(args.outstack, True, 0., True)
def processSingleFile(inputFile, outputDIR, tmpath, calcExtraBands, palsar2=False):
    inputFile = os.path.abspath(inputFile)
    outputDIR = os.path.abspath(outputDIR)
    tmpath = os.path.abspath(tmpath)
    print("Processing: " + inputFile)
    baseName = os.path.basename(inputFile).split(".")[0]
    print("\t" + baseName)
    rsgisUtils = rsgislib.RSGISPyUtils()
    uidStr = "_"+rsgisUtils.uidGenerator()
    createdTmp = False
    if not os.path.exists(tmpath):
        os.makedirs(tmpath)
        createdTmp = True
    
    extract2DIR = os.path.join(tmpath, baseName+uidStr)
    if not os.path.exists(extract2DIR):
        os.makedirs(extract2DIR)
    os.chdir(extract2DIR)
    
    cmd = 'tar -xzf ' + inputFile
    print(cmd)
    subprocess.call(cmd, shell=True)
    
    try:
        if palsar2:
            hhFiles = glob.glob(os.path.join(extract2DIR, HH_P2_FILE_PATTERN))
            hvFiles = glob.glob(os.path.join(extract2DIR, HV_P2_FILE_PATTERN))
            
            if len(hhFiles) == 0:
                hhFiles = glob.glob(os.path.join(extract2DIR, HH_P2_FP_FILE_PATTERN))
            if len(hvFiles) == 0:
                hvFiles = glob.glob(os.path.join(extract2DIR, HV_P2_FP_FILE_PATTERN))
            
            in_hh_file = hhFiles[0]
            in_hv_file = hvFiles[0]
        else:
            in_hh_file = glob.glob(os.path.join(extract2DIR, HH_P1_FILE_PATTERN))[0]
            in_hv_file = glob.glob(os.path.join(extract2DIR, HV_P1_FILE_PATTERN))[0]
    except IndexError:
        raise Exception('Could not find data - check filenames')
    
    bands_list = [in_hh_file, in_hv_file]
    band_names = ['HH','HV']
    
    # Create extra image bands
    for calcBand in calcExtraBands:    
        if calcBand == 'COVARHH':
            extraBandFile = os.path.join(extract2DIR, baseName + '_covhh.kea')
            imagefilter.applyCoeffOfVarFilter(in_hh_file, extraBandFile, 5, 'KEA', rsgislib.TYPE_32FLOAT)
            bandName = 'CoVHH'
            bands_list.append(extraBandFile)
            band_names.append(bandName)
        if calcBand == 'COVARHV':
            extraBandFile = os.path.join(extract2DIR, baseName + '_covhv.kea')
            imagefilter.applyCoeffOfVarFilter(in_hv_file, extraBandFile, 5, 'KEA', rsgislib.TYPE_32FLOAT)
            bandName = 'CoVHV'
            bands_list.append(extraBandFile)
            band_names.append(bandName)
        if calcBand == 'HHHV':
            extraBandFile = os.path.join(extract2DIR, baseName + '_hhhv.kea')
            bandDefns = [imagecalc.BandDefn('hh', in_hh_file, 1),
                         imagecalc.BandDefn('hv', in_hv_file, 1)]
            imagecalc.bandMath(extraBandFile, 'hv==0?0:hh/hv', 'KEA', rsgislib.TYPE_32FLOAT, bandDefns) 
            bandName = 'HH/HV'
            bands_list.append(extraBandFile)
            band_names.append(bandName)
    
    # Create stack
    stackFile = os.path.join(outputDIR, baseName + '_stack.kea')
    imageutils.stackImageBands(bands_list, band_names, stackFile, None, 0, 'KEA', rsgislib.TYPE_32FLOAT) 
    imageutils.popImageStats(stackFile, usenodataval=True, nodataval=0, calcpyramids=True)
    
    try:
        in_mask_file = glob.glob(os.path.join(extract2DIR, MASK_FILE_PATTERN))[0]
        out_mask_file = os.path.join(outputDIR, baseName + '_mask.kea')
        cmd = 'gdal_translate -of KEA ' + in_mask_file + ' ' + out_mask_file
        subprocess.call(cmd, shell=True)
        rastergis.populateStats(out_mask_file, True, True)
    except IndexError:
        print("WARNING: Could not find the mask file... Ignoring.")
    
    try:
        in_date_file = glob.glob(os.path.join(extract2DIR, DATE_FILE_PATTERN))[0]
        out_date_file = os.path.join(outputDIR, baseName + '_date.kea')
        cmd = 'gdal_translate -of KEA ' + in_date_file + ' ' + out_date_file
        subprocess.call(cmd, shell=True)
        rastergis.populateStats(out_date_file, True, True)
    except IndexError:
        print("WARNING: Could not find the date file... Ignoring.")
        
    try:
        in_linci_file = glob.glob(os.path.join(extract2DIR, LINCI_FILE_PATTERN))[0]
        out_linci_file = os.path.join(outputDIR, baseName + '_linci.kea')
        cmd = 'gdal_translate -of KEA ' + in_linci_file + ' ' + out_linci_file
        subprocess.call(cmd, shell=True)
        rastergis.populateStats(out_linci_file, True, True)
    except IndexError:
        print("WARNING: Could not find the linci file... Ignoring.")
    
    shutil.rmtree(extract2DIR)
    if createdTmp:
        shutil.rmtree(tmpath)
Beispiel #9
0
def creating_stacks(layersList, bandNamesList, outName):
    #set format of data: assuming kea
    gdalformat = 'KEA'
    dataType = rsgislib.TYPE_32FLOAT
    imageutils.stackImageBands(layersList, bandNamesList, outName, None, 0, gdalformat, dataType)                                         
    return(outName)
Beispiel #10
0
def exportCols2GDALImage(clumps,
                         outimage,
                         gdalformat,
                         datatype,
                         fields,
                         ratband=1,
                         tempDIR=None):
    """Exports columns of the raster attribute table as bands in a GDAL image. Utility function, exports each column individually then stacks them.

Where:

:param clumps: is a string containing the name of the input image file with RAT
:param outimage: is a string containing the name of the output gdal file
:param gdalformat: is a string containing the GDAL format for the output file - eg 'KEA'
:param datatype: is an int containing one of the values from rsgislib.TYPE_*
:param field: is a list of strings, providing the names of the column to be exported.
:param ratband: is an optional (default = 1) integer parameter specifying the image band to which the RAT is associated.

Example::

   clumps='./RATS/injune_p142_casi_sub_utm_clumps_elim_final_clumps_elim_final.kea'
   outimage='./TestOutputs/RasterGIS/injune_p142_casi_rgb_export.kea'
   gdalformat = 'KEA'
   datatype = rsgislib.TYPE_32FLOAT
   fields = ['RedAvg','GreenAvg','BlueAvg']
   rastergis.exportCols2GDALImage(clumps, outimage, gdalformat, datatype, fields)

"""
    import os
    import rsgislib
    from rsgislib import imageutils

    rsgisUtils = rsgislib.RSGISPyUtils()

    if tempDIR is None:
        tempDIR = os.path.split(outimage)[0]

    outExt = os.path.splitext(outimage)[-1]
    tempFileList = []

    # Export each field
    for field in fields:
        print('Exporting: ' + field)
        outTempFile = os.path.join(tempDIR, field + outExt)
        exportCol2GDALImage(clumps,
                            outTempFile,
                            gdalformat,
                            datatype,
                            field,
                            ratband=1)
        tempFileList.append(outTempFile)

    # Stack Bands
    print('Stacking Bands')
    imageutils.stackImageBands(tempFileList, fields, outimage, None, 0,
                               gdalformat, datatype)

    # Remove temp files
    print('Removing temp files')
    for tempFile in tempFileList:
        rsgisUtils.deleteFileWithBasename(tempFile)
Beispiel #11
0

# Image File stacking - create image stack of sentinel MT images
# Iterate through dates and create temp_stack of them in 4_Stacked file location
print("Creating image stack of clipped sentinel images...")

sentBandNames = [
    'Band02', 'Band03', 'Band04', 'Band05', 'Band06', 'Band07', 'Band08',
    'Band8A', 'Band11', 'Band12'
]

for img_date_dir in os.listdir(os.path.join(fil_dir, '3_Clipped', site_dir)):
    # print (img_date_dir)
    imageutils.stackImageBands(
        fileStackBands(fil_dir, site_dir, img_date_dir), sentBandNames,
        os.path.join(fil_dir, '4_Stacked', site_dir,
                     img_date_dir + '_stack_temp.kea'), None, 0, 'KEA',
        rsgislib.TYPE_32FLOAT)

# Iterate through stacked images
for stack_temp_dir in os.listdir(os.path.join(fil_dir, '4_Stacked', site_dir)):
    if stack_temp_dir.endswith('_stack_temp.kea'):
        stack_temp_ds = gdal.Open(
            os.path.join(fil_dir, '4_Stacked', site_dir, stack_temp_dir),
            gdal.GA_Update)  # as gdal array

        # Output Files to be created
        stack_dir_fn = os.path.join(fil_dir, '4_Stacked', site_dir,
                                    stack_temp_dir[:-15])
        stack_fn = os.path.join(stack_dir_fn + '_stack.kea')
        stan_rast_fn = os.path.join(stack_dir_fn + '_stack_stan.kea')
bandDefns.append(BandDefn('b1', b1, 1))
imagecalc.bandMath(b1_nan, '(b1==0.0)?50:b1', gdalformat,
                   rsgislib.TYPE_32FLOAT, bandDefns)
bandDefns = []
bandDefns.append(BandDefn('b2', b2, 1))
imagecalc.bandMath(b2_nan, '(b2==0.0)?50:b2', gdalformat,
                   rsgislib.TYPE_32FLOAT, bandDefns)
bandDefns = []
bandDefns.append(BandDefn('b3', b3, 1))
imagecalc.bandMath(b3_nan, '(b3==0.0)?50:b3', gdalformat,
                   rsgislib.TYPE_32FLOAT, bandDefns)

# stack the nonan bands together
gdalformat = 'GTiff'
gdaltype = rsgislib.TYPE_32FLOAT
imageutils.stackImageBands([b1_nan, b2_nan, b3_nan], ['VV', 'VH', 'VVdivVH'],
                           nanOut, None, 50, gdalformat, gdaltype)

inputImage = nanOut

# remove intermediate  images
try:
    os.remove(b1)
    os.remove(b2)
    os.remove(b3)
    os.remove(b1_nan)
    os.remove(b2_nan)
    os.remove(b3_nan)
except Exception:
    pass

###########################################################################
Beispiel #13
0
imageList=glob.glob('*sel_lt15dB.kea')


imageList =sorted(imageList)

# snap to common grid
for img in imageList[1:len(imageList)]:
	print('')
	print('Processing: ' + img)
	print('')
	imgOut=img.replace('.kea','_snapped.kea')
	inRefImg=imageList[0] # base raster to snap to
	gdalFormat = 'kea'
	rsgislib.imageutils.resampleImage2Match(inRefImg, img, imgOut, gdalFormat, interpMethod='nearestneighbour', datatype=rsgislib.TYPE_8INT) # perform resampling/snap

# list snapped images
imageList=glob.glob('*sel_lt15dB_snapped.kea')
imageList =sorted(imageList)

# stack images
bandNamesList = []

for img in imageList:
	date=img.split('_')[4].split('T')[0]
	bandNamesList.append('b'+date)

outputImage = 'sel_lt15dB_stack_2017.kea'
gdalformat = 'KEA'
gdaltype = rsgislib.TYPE_8INT
imageutils.stackImageBands(imageList, bandNamesList, outputImage, None, 0, gdalformat, gdaltype)
def create_palsar_rgb_composite(in_tile_dir, stretchtxt, calcCoVHH, calcCoVHV):
    """
    Create scaled 8-bit RGB composite
    of HH, HV and HH/HV PALSAR data in GeoTiff format

    Stores intermediate files in temp directory and removes
    after.

    """
    try:
        in_hh_file = glob.glob(os.path.join(in_tile_dir, HH_FILE_PATTERN))[0]
        in_hv_file = glob.glob(os.path.join(in_tile_dir, HV_FILE_PATTERN))[0]
    except IndexError:
        raise Exception('Could not find data - check filenames')

    tile_basename = os.path.basename(in_tile_dir)
    out_composite = os.path.join(in_tile_dir, tile_basename + '_composite.tif')

    # Make temp directory for intermediate files
    temp_dir = tempfile.mkdtemp(prefix='palsar_stack_')
    #print("Temp DIR: " + temp_dir)

    # Create HH/HV image
    temp_3rdBand_file = ''
    bandName = ''
    if calcCoVHH:
        temp_3rdBand_file = os.path.join(temp_dir,
                                         tile_basename + '_covhh.tif')
        imagefilter.applyCoeffOfVarFilter(in_hh_file, temp_3rdBand_file, 5,
                                          'GTiff', rsgislib.TYPE_32FLOAT)
        bandName = 'CoVHH'
    elif calcCoVHV:
        temp_3rdBand_file = os.path.join(temp_dir,
                                         tile_basename + '_covhv.tif')
        imagefilter.applyCoeffOfVarFilter(in_hv_file, temp_3rdBand_file, 5,
                                          'GTiff', rsgislib.TYPE_32FLOAT)
        bandName = 'CoVHV'
    else:
        temp_3rdBand_file = os.path.join(temp_dir, tile_basename + '_hhhv.tif')
        bandDefns = [
            imagecalc.BandDefn('hh', in_hh_file, 1),
            imagecalc.BandDefn('hv', in_hv_file, 1)
        ]
        imagecalc.bandMath(temp_3rdBand_file, 'hh/hv', 'GTiff',
                           rsgislib.TYPE_32FLOAT, bandDefns)
        bandName = 'HH/HV'

    # Create stack
    temp_stack = os.path.join(temp_dir, tile_basename + '_stack.tif')
    bands_list = [in_hh_file, in_hv_file, temp_3rdBand_file]
    band_names = ['HH', 'HV', bandName]
    imageutils.stackImageBands(bands_list, band_names, temp_stack, None, 0,
                               'GTiff', rsgislib.TYPE_32FLOAT)

    # Apply stretch
    if stretchtxt == None:
        imageutils.stretchImage(temp_stack, out_composite, False, '', True,
                                False, 'GTiff', rsgislib.TYPE_8INT,
                                imageutils.STRETCH_LINEARSTDDEV, STRETCH_STDEV)
    else:
        imageutils.stretchImageWithStats(temp_stack, out_composite, stretchtxt,
                                         'GTiff', rsgislib.TYPE_8INT,
                                         imageutils.STRETCH_LINEARMINMAX, 0)

    # Remove temp directory
    shutil.rmtree(temp_dir)