Beispiel #1
0
def classify(outputClumps):
    # define output class column
    # rebalance the training data
    classesIntCol = 'ClassInt'
    rsgislib.classification.classratutils.balanceSampleTrainingRandom(
        outputClumps, classesIntCol, 'classesIntColBal', 50, 5000)
    classesIntCol = 'classesIntColBal'
    # run the classifier
    classratutils.classifyWithinRAT(outputClumps,
                                    classesIntCol,
                                    classesNameCol,
                                    variables,
                                    classifier=classifier,
                                    classColours=None,
                                    preProcessor=MaxAbsScaler(),
                                    outColInt=outColInt)
Beispiel #2
0
    # define the output colours
    classColours = dict()
    classColours['Other'] = [212, 125, 83]
    classColours['Water'] = [157, 212, 255]
    classColours['VegWater'] = [191, 255, 0]

    # define variables for the classification
    variables = [
        'VVMin', 'VHMin', 'VVdivVHMin', 'VVMax', 'VHMax', 'VVdivVHMax',
        'VVAvg', 'VHAvg', 'VVdivVHAvg', 'VVStd', 'VHStd', 'VVdivVHStd'
    ]
    # run the classification
    classratutils.classifyWithinRAT(outputClumps,
                                    classesIntCol,
                                    classesNameCol,
                                    variables,
                                    classifier=classifier,
                                    classColours=classColours,
                                    preProcessor=MaxAbsScaler())

    ############################################################################################ # Random forests ranks parameters in terms of importance to the classifier
    # there is a lot which can be learnt here...
    print('Feature importance: ' + inputImg)
    featImportances = classifier.feature_importances_
    featIndices = numpy.argsort(featImportances)[::-1]
    # Print the feature ranking
    print("Feature ranking:")
    with open('./Importance/importance_' + inputImg + '.txt', 'w') as txt:
        for f in range(len(variables)):
            print("\t{0}. {1} ({2})".format(f + 1, variables[featIndices[f]],
                                            featImportances[featIndices[f]]))
classColours['Water'] = [157, 212, 255]
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)