Ejemplo n.º 1
0
 def i_validate(self):
     """
     Interface to validate a classification. It going to rasterize the validation shapefile and the 
     classification shapefile with :func:`layer_rasterization`. Next, to compare pixel by pixel, the classification
     quality to built a confusion matrix in a csv file.
     
     """
     # Variable to convert the input classname to an individual interger
     # Only for the validate sample
     class_validate = 0
     complete_validate_shp = os.path.dirname(self.valid_shp[0][0]) + '/validate.shp'
     
     # TODO: Set this method in the Precision_moba class
     
     # Processing to rasterize the validate shapefile. 1) Merge sahpefiles 2) Rasterization
     for val in self.valid_shp:
         if class_validate != 2: 
             # Grassland to 1
             if (class_validate !=3 and len(self.out_fieldname_carto) != 4+2) or len(self.out_fieldname_carto) == 4+2:
                 # To the level 3 with woodeen to 4 and 5
                 #
                 # Self.valid_shp is a list of list. In this variable there is :
                 # [Shapefile path, fieldname classes, classnames]
                 opt = {}
                 opt['Remove'] = 1 # To overwrite 
     
                 # Create a raster to valide the classification
                 # First time, create a new shapefile with a new field integer
                 sample_val = Sample(val[0], self.path_area, 1, **opt)
                 opt['add_fieldname'] = 1 
                 opt['fieldname'] = 'CLASS_CODE'
                 opt['class'] = str(class_validate) # Add integer classes
                 # Set the new shapefile
                 val[0] = val[0][:-4] + '_.shp'
                 val[1] = opt['fieldname']
                 val[2] = opt['class']
                 # Complete the new shapefile
                 sample_val.fill_sample(val[0], 0, **opt)
                 # Second time, merge the validate shapefile
                 if class_validate == 0:
                     process_tocall_merge =  ['ogr2ogr', '-overwrite', complete_validate_shp, val[0]]
                 elif class_validate > 0:
                     process_tocall_merge =  ['ogr2ogr', '-update', '-append', complete_validate_shp, \
                                              val[0], '-nln', os.path.basename(complete_validate_shp[:-4])]
                 subprocess.call(process_tocall_merge)
         # Increrment variable
         class_validate = self.valid_shp.index(val) + 1
     
     # Compute precision of the classification
     valid = Precision_moba(self.path_area, self.path_folder_dpt)     
     valid.complete_validation_shp = complete_validate_shp
     valid.ex_raster = self.raster_path[0]
     
     # TODO: Call the RasterSat_by_Date class here instead of the Precision_moba class
     
     valid.preprocess_to_raster_precision(self.output_name_moba, 'FBPHY_SUB') # To the classification's data
     valid.preprocess_to_raster_precision(complete_validate_shp, val[1]) # To the validation's data
     
     # Compute precision on the output classification
     valid.confus_matrix(valid.complete_img[0].raster_data(valid.img_pr[0])[0], \
                         valid.complete_img[1].raster_data(valid.img_pr[1])[0])