def test_Basic_CropMix_classif(self): """ this test verify if features labels generated are similar to a reference produce thanks to a specific configuration file (cropMix 2 mode) """ from Sampling.DataExtraction import VectorSampler import tileEnvelope as env from Sampling import TileArea as area from Common.Tools import CreateRegionsByTiles as RT #expected output ref_path = os.path.join(self.iota2_directory, "data", "references", "iota2tests_features_labels_test_Basic.txt") #test inputs vector_file = os.path.join(self.iota2_directory, "data", "references", "sampler", "D0005H0002_polygons_To_Sample.shp") L8_rasters = os.path.join(self.iota2_directory, "data", "L8_50x50") classifications_path = os.path.join(self.iota2_directory, "data", "references", "sampler") #rename reference shape test_vector = os.path.join(self.test_working_directory_tmp, "D0005H0002_polygons_1_Sample.shp") fut.cpShapeFile(vector_file.replace(".shp", ""), test_vector.replace(".shp", ""), [".prj", ".shp", ".dbf", ".shx"]) #generate IOTA output directory IOTA2Directory.GenerateDirectories(self.test_working_directory) #fill up configuration file self.config.setParam('chain', 'outputPath', self.test_working_directory) self.config.setParam('chain', 'listTile', "D0005H0002") self.config.setParam('chain', 'featuresPath', self.test_working_directory_tmp) self.config.setParam('chain', 'L8Path', L8_rasters) self.config.setParam('chain', 'userFeatPath', 'None') self.config.setParam('argTrain', 'samplesOptions', '-sampler random -strategy all') self.config.setParam('argTrain', 'cropMix', True) self.config.setParam('argTrain', 'samplesClassifMix', True) self.config.setParam('argTrain', 'annualClassesExtractionSource', classifications_path) self.config.setParam('GlobChain', 'useAdditionalFeatures', False) #shapes genereation fut.getCommonMasks("D0005H0002", self.config, None) env.GenerateShapeTile(["D0005H0002"], self.test_working_directory_tmp, self.test_working_directory + "/envelope", None, self.config) shapeRegion = os.path.join(self.test_working_directory_tmp, "MyFakeRegion.shp") area.generateRegionShape("one_region", self.test_working_directory + "/envelope", "", shapeRegion, "region", self.config, None) RT.createRegionsByTiles(shapeRegion, "region", self.test_working_directory + "/envelope", self.test_working_directory + "/shapeRegion/", None) #Launch sampling VectorSampler.generateSamples(test_vector, None, self.config) test_vector = fut.fileSearchRegEx(self.test_working_directory + "/learningSamples/*sqlite")[0] test_field_list = fut.getAllFieldsInShape(test_vector, driver='SQLite') with open(ref_path, 'r') as f: ref_field_list = [line.rstrip() for line in f] #check outputs #condition : the difference between ref_field_list and test_field_list must be 'originfid' condition = len(set(ref_field_list) - set(test_field_list)) == 1 and list( set(ref_field_list) - set(test_field_list))[0] == "originfid" self.assertTrue(condition)
def RandomInSitu(vectorFile, field, nbdraws, opath, name, AllFields, ratio, pathWd): """ """ AllPath = [] crop = 0 classes = [] shapefile = vectorFile field = field dicoprop = {} allFID = [] nbtirage = nbdraws nameshp = shapefile.split('.') namefile = nameshp[0].split('/') driver = ogr.GetDriverByName("ESRI Shapefile") dataSource = driver.Open(shapefile, 0) layer = dataSource.GetLayer() # Count the total features of cropland if crop == 1: layer.SetAttributeFilter("CROP =1") count = float(layer.GetFeatureCount()) # Find the number of polygons by class for feature in layer: pid = feature.GetFID() allFID.append(pid) cl = feature.GetField(field) if cl not in classes: classes.append(cl) AllTrain = [] AllValid = [] for tirage in range(0, nbtirage): listallid, listValid = get_randomPoly(dataSource, field, classes, ratio) ch = "" listFid = [] for fid in listallid: listFid.append("FID=" + str(fid)) resultA = [] for e in listFid: resultA.append(e) resultA.append(' OR ') resultA.pop() chA = ''.join(resultA) layer.SetAttributeFilter(chA) learningShape = opath + "/" + name + "_seed" + str( tirage) + "_learn.shp" if pathWd is None: outShapefile = opath + "/" + name + "_seed" + str( tirage) + "_learn.shp" fu.CreateNewLayer(layer, outShapefile, AllFields) else: outShapefile = pathWd + "/" + name + "_seed" + str( tirage) + "_learn.shp" fu.CreateNewLayer(layer, outShapefile, AllFields) fu.cpShapeFile( outShapefile.replace(".shp", ""), opath + "/" + name + "_seed" + str(tirage) + "_learn", [".prj", ".shp", ".dbf", ".shx"]) for i in allFID: if i not in listallid: listValid.append(i) chV = "" listFidV = [] for fid in listValid: listFidV.append("FID=" + str(fid)) resultV = [] for e in listFidV: resultV.append(e) resultV.append(' OR ') resultV.pop() chV = ''.join(resultV) layer.SetAttributeFilter(chV) validationShape = opath + "/" + name + "_seed" + str( tirage) + "_val.shp" if pathWd is None: outShapefile2 = opath + "/" + name + "_seed" + str( tirage) + "_val.shp" fu.CreateNewLayer(layer, outShapefile2, AllFields) else: outShapefile2 = pathWd + "/" + name + "_seed" + str( tirage) + "_val.shp" fu.CreateNewLayer(layer, outShapefile2, AllFields) fu.cpShapeFile(outShapefile2.replace(".shp", ""), opath + "/" + name + "_seed" + str(tirage) + "_val", [".prj", ".shp", ".dbf", ".shx"]) AllTrain.append(learningShape) AllValid.append(validationShape) return AllTrain, AllValid