classColours['Agriculture'] = [255, 255, 166]
classColours['Grassland'] = [200, 255, 187]
classColours['Forest'] = [132, 200, 76]
classColours['Urban'] = [200, 200, 200]

# define input variables
variables = [
    'coastAvg', 'blueAvg', 'greenAvg', 'redAvg', 'NIRAvg', 'SWIR1Avg',
    'SWIR2Avg', 'VVStd', 'wbiAvg', 'ndviAvg'
]

# run the classification
classratutils.classifyWithinRAT(clumps,
                                classesIntCol,
                                classesNameCol,
                                variables,
                                classifier=classifier,
                                classColours=classColours)

# export rat column to image
outimage = '/Users/Andy/Documents/Tanzania/WB_Mapping/land_cover/Landsat8_composite_clumps_classified.tif'
gdalformat = 'GTiff'
datatype = rsgislib.TYPE_8INT
fields = ['OutClass']
rastergis.exportCols2GDALImage(clumps, outimage, gdalformat, datatype, fields)

# export as shapefile
inputImg = outimage
outShp = '/Users/Andy/Documents/Tanzania/WB_Mapping/land_cover/Landsat8_composite_clumps_classified_v2.shp'
# rsgislib.vectorutils.polygoniseRaster(inputImg, outShp, imgBandNo=1, maskImg=None, imgMaskBandNo=1)
Ejemplo n.º 2
0
rios.rat.writeColumn(outputClumps,
                     'OutClass_mode_cert',
                     mode_cert,
                     colType=gdal.GFT_Integer)
rios.rat.writeColumn(outputClumps,
                     'OutClass_mode_cert_names',
                     names,
                     colType=gdal.GFT_String)

###########################################################################################
# export rat column: mode with certainty to image
gdalformat = 'KEA'
datatype = rsgislib.TYPE_8INT
fields = ['OutClass_mode_cert']
outCert = inputImage.split('.')[0] + '_cert.kea'
rastergis.exportCols2GDALImage(outputClumps, outCert, gdalformat, datatype,
                               fields)

####################################################################
# snap permanent water
inProcessImg = permWater
inRefImg = inputImage
outSnap = permWater.replace(
    '.kea', '_' + inputImage.split('/')[-1].split('_')[4] + '_snap.kea')
gdalformat = 'KEA'
rsgislib.imageutils.resampleImage2Match(inRefImg,
                                        inProcessImg,
                                        outSnap,
                                        gdalformat,
                                        interpMethod='nearestneighbour',
                                        datatype=rsgislib.TYPE_8UINT)
Ejemplo n.º 3
0
parser = argparse.ArgumentParser()
parser.add_argument("--rat", type=str, required=True, help="Raster attribute table to copy classification to")
parser.add_argument("--txt", type=str, required=True, help="Text file containing classification")
parser.add_argument("--outraster", type=str, required=False, default=None, help="Output Raster classification")
parser.add_argument("--name", type=str, required=False, default='Classification', help="Outname for column in RAT. Defaults to 'Classification'")

args = parser.parse_args()    
 
print('Reading in Classification')
classification = np.genfromtxt(args.txt,dtype=int, skip_header=1)

print('Opening RAT')
inRatFile = args.rat
ratDataset = gdal.Open(inRatFile, gdal.GA_Update)

# Read in column names
colnames = rat.getColumnNames(inRatFile)

# Read in one column
# (just need to check same number as rows as classification)
testCol = rat.readColumn(ratDataset, colnames[0]) 

if testCol.shape[0] != classification.shape[0]:
    raise Exception("The classification file doesn't contain the same number of rows as the RAT")

rat.writeColumn(ratDataset, args.name, classification)
ratDataset = None

if args.outraster is not None:
    rastergis.exportCols2GDALImage(args.rat, args.outraster, 'KEA', rsgislib.TYPE_8INT, [args.name])