def classify(self, feature): tile = feature.GetFieldAsString(self.tileAttribute); print( "{0} Tile {1}".format(time.strftime("%d/%m/%Y"), tile) ); filterTile="{0} = '{1}'".format(self.tileAttribute, tile); gridFile = self.basepath('INPUT', 'GRID', 'CLASSIFICATION_GRID.shp'); mvcFile = self.getMvcFile(tile); trainningDataset = "DATASET_{0}".format(self.trainningDataset); trainningFilename = "{0}_TRAINNING.shp".format(tile); trainningFile = self.basepath('INPUT', 'TRAINNING', trainningDataset, trainningFilename); tempOutputFilename = "{0}_G{1}_{2}_MVC.tif".format(tile, self.gapfill.zfill(2), self.trainningDataset); tempOutputFile = self.temppath(tempOutputFilename); outputFilename = "{0}_CLASSIFICATION.tif".format(tile); outputFile = self.basepath('OUTPUT', self.runtimeDataset, 'CLASSIFICATION', outputFilename); outputStatsFilename = "{0}_STATS.csv".format(tile); outputStatsFile = self.basepath('OUTPUT', self.runtimeDataset, 'VALIDATION', outputStatsFilename); if not self.noClassify: print( " {0} Classify".format( time.strftime("%H:%M:%S") ) ); resultClassification = garsect2.run2(mvcFile, trainningFile, self.trainningAttribute, self.mvcNullValue, tempOutputFile, outputStatsFile, 1, self.nTreeRandomForest, [1]); command = 'gdalwarp -co COMPRESS=lzw -co INTERLEAVE=BAND -co TILED=YES -ot Byte -q -dstnodata "{0}" -cwhere "{1}" -crop_to_cutline -overwrite -multi -cutline {2} {3} {4}'.format(self.mvcNullValue, filterTile, gridFile, tempOutputFile, outputFile); os.system(command); os.remove(tempOutputFile); return outputFile;
def classifyWithRandTrainning(self, feature): tile = feature.GetFieldAsString(self.tileAttribute); mvcFile = self.getMvcFile(tile) boundsGeometry = feature.geometry(); nRandomPoints = self.nTreeRandomPoints; execNumber = int(self.trainningDataset[1:]); classificationResults = [ ]; print( "{0} Tile {1}".format(time.strftime("%d/%m/%Y"), tile) ); for i in range(execNumber): tmpFiles = []; rSuffix = "_R" + str(i) + '-' + str(execNumber); fileBaseName = tile + rSuffix; pctRandom = float(feature.GetFieldAsString(self.pctClassAttribute)); print( " Execution {0}".format( str(i + 1) ) ); filterTile="{0} = '{1}'".format(self.tileAttribute, tile); gridFile = self.basepath('INPUT', 'GRID', 'CLASSIFICATION_GRID.shp'); validationDataset = "DATASET_{0}".format(self.validationDataset); validationFile = self.basepath('INPUT', 'VALIDATION', validationDataset, 'POLYGON_VALIDATION.shp'); trainningFile = self.temppath(fileBaseName + ".shp"); tmpFiles.append(self.temppath(fileBaseName + ".shp")); tmpFiles.append(self.temppath(fileBaseName + ".dbf")); tmpFiles.append(self.temppath(fileBaseName + ".shx")); tmpFiles.append(self.temppath(fileBaseName + ".prj")); try: os.remove(trainningFile); except: pass srs = osr.SpatialReference() srs.ImportFromEPSG(4326) trainningDs = self.geoDB['drivers']['shp'].CreateDataSource(trainningFile) trainningLayer = trainningDs.CreateLayer("point_out", srs, ogr.wkbPoint ) trainningLayer.CreateField( ogr.FieldDefn(self.trainningAttribute, ogr.OFTString) ) fixedTotalPoints = int(math.ceil(nRandomPoints * 0.25)); pctTotalPoints = int(math.trunc(nRandomPoints * 0.75)); nRandPointIn = int(math.trunc(pctTotalPoints * pctRandom)) + (fixedTotalPoints / 2) print( " {0} {1} randomPoints".format( time.strftime("%H:%M:%S"), nRandPointIn ) ); inTrainningPoints = randompoints.randomPointsIn(validationFile, nRandPointIn, boundsGeometry); nRandPointOut = int(math.ceil(pctTotalPoints * (1 - pctRandom))) + (fixedTotalPoints / 2) print( " {0} {1} randomPointsOut".format( time.strftime("%H:%M:%S"), nRandPointOut ) ); notInTrainningPoints = randompoints.randomPointsOut(validationFile, nRandPointOut, boundsGeometry); for point in notInTrainningPoints: feat = ogr.Feature(trainningLayer.GetLayerDefn()) feat.SetGeometry(point['geometry']); feat.SetField(self.trainningAttribute, 1) trainningLayer.CreateFeature(feat) for point in inTrainningPoints: feat = ogr.Feature(trainningLayer.GetLayerDefn()) feat.SetGeometry(point['geometry']); feat.SetField(self.trainningAttribute, 2) trainningLayer.CreateFeature(feat) trainningLayer.SyncToDisk(); tempOutputFile = self.temppath(fileBaseName + "_noclip.tif"); tmpFiles.append(tempOutputFile); classificationResult = self.temppath(fileBaseName + '.tif'); classificationResults.append(classificationResult); print( " {0} Classify".format( time.strftime("%H:%M:%S") ) ); resultClassification = garsect2.run2(mvcFile, trainningFile, self.trainningAttribute, self.mvcNullValue, tempOutputFile, None, 1, self.nTreeRandomForest, [1]); command = 'gdalwarp -co COMPRESS=lzw -co INTERLEAVE=BAND -co TILED=YES -ot Int16 -q -dstnodata "{0}" -cwhere "{1}" -crop_to_cutline -overwrite -multi -cutline {2} {3} {4}'.format(self.mvcNullValue, filterTile, gridFile, tempOutputFile, classificationResult); os.system(command); print( " {0} Remove temp files".format( time.strftime("%H:%M:%S") ) ); for tmpFile in tmpFiles: os.remove(tmpFile); letters = []; params = ''; for i,classificationResult in enumerate(classificationResults): letter = ascii_uppercase[i]; params += " -{0} {1} ".format(letter, classificationResult); letters.append(letter); expr = "({0})/{1}".format( '+'.join(letters), len(letters) ); outputFilename = "{0}_CLASSIFICATION.tif".format(tile); outputFile = self.basepath('OUTPUT', self.runtimeDataset, 'CLASSIFICATION', outputFilename); print( " {0} AVG classification calcultion".format( time.strftime("%H:%M:%S") ) ); command = 'gdal_calc.py --co="COMPRESS=deflate" --co="INTERLEAVE=BAND" --co="TILED=YES" --NoDataValue=0 {0} --outfile={1} --calc="{2}"'.format(params, outputFile, expr) os.system(command); for classificationResult in classificationResults: print( " {0} Remove temp files".format( time.strftime("%H:%M:%S") ) ); os.remove(classificationResult); return '';