コード例 #1
0
ファイル: Processing.py プロジェクト: SylvioL/PHYMOBAT
 def i_images_processing(self, vs): 
     
     """
     Interface function to launch processing VHRS images :func:`i_vhrs` and satellite images :func:`i_img_sat` in multi-processing.
     
     :param vs: Boolean variable to launch processing because of interface checkbox -> 0 or 1.
 
         - 0 means, not texture processing
         - 1 means, launch texture processing
     :type vs: int
     """
     
     # Multiprocessing
     mgr = BaseManager()
     mgr.register('defaultdict', defaultdict, DictProxy)
     mgr.start()
     self.out_ndvistats_folder_tab = mgr.defaultdict(list)
     
     p_img_sat = Process(target=self.i_img_sat)
     p_img_sat.start()
     if self.mp == 0:
         p_img_sat.join()
     
     if vs == 1:
         p_vhrs = Process(target=self.i_vhrs)#, args=(vs, ))
         p_vhrs.start()
         p_vhrs.join()
     
     if self.mp == 1:
         p_img_sat.join()
     
     # List of output raster path
     self.raster_path.append(self.out_ndvistats_folder_tab[0])
     # List of output raster band
     self.list_band_outraster.append(1)
     
     if vs == 1:
         self.raster_path.append(self.out_ndvistats_folder_tab['sfs'])
         self.list_band_outraster.append(4)
         self.raster_path.append(self.out_ndvistats_folder_tab['haralick'])
         self.list_band_outraster.append(2)
     
     # To slope, to extract scree
     if self.path_mnt != '':
         self.raster_path.append(self.path_mnt)
         self.list_band_outraster.append(1)
         
     self.raster_path.append(self.out_ndvistats_folder_tab[1])
     # example raster path tab :
     #                [path_folder_dpt + '/' + folder_processing + '/' + classif_year + '/Min_2014.TIF',\
     #                os.path.dirname(path_ortho) + '/Clip_buffer_surface_dep_18_IRCOrtho65_2m_sfs.TIF',\
     #                os.path.dirname(path_ortho) + '/Clip_buffer_surface_dep_18_IRCOrtho65_2m_haralick.TIF',\
     #                path_folder_dpt + '/' + folder_processing + '/' + classif_year + '/Max_2014.TIF']
     
     # List of output raster band
     self.list_band_outraster.append(1) #[1, 4, 2, 1]
     
     print("End of images processing !")
コード例 #2
0
ファイル: Processing.py プロジェクト: SylvioL/PHYMOBAT
    def i_classifier(self): 
        """
        Interface function to launch decision tree classification with a input segmentation :func:`Segmentation.Segmentation`.
        
        This function store optimal threshold by class **Segmentation.out_threshold**. Then compute zonal statistics by polygons
        for every images in multi-processing (if **mp** = 1).
        """ 
        
        # Multiprocessing
        mgr = BaseManager()
        mgr.register('defaultdict', defaultdict, DictProxy)
        mgr.start()
        multi_process_var = [] # Multi processing variable
          
        # Extract final cartography
        out_carto = Segmentation(self.path_segm, self.path_area) 
        out_carto.output_file = self.output_name_moba
        out_carto.out_class_name = self.in_class_name
        out_carto.out_threshold = []
        for ind_th in range(len(self.sample_name)):
            out_carto.out_threshold.append(self.decis[ind_th].threshold[0])
            if '>' in self.decis[ind_th].threshold[0]:
                out_carto.out_threshold.append(self.decis[ind_th].threshold[0].replace('>', '<='))
            elif '<' in self.decis[ind_th].threshold[0]:
                out_carto.out_threshold.append(self.decis[ind_th].threshold[0].replace('<', '>='))
        #     out_carto.zonal_stats((raster_path[ind_th], list_band_outraster[ind_th]))
            multi_process_var.append([self.raster_path[ind_th], self.list_band_outraster[ind_th]])
         
        # Compute zonal stats on slope raster
        multi_process_var.append([self.raster_path[ind_th+1], self.list_band_outraster[ind_th+1]])
        out_carto.out_threshold.append('<'+str(self.slope_degree)) # To agriculture
        out_carto.out_threshold.append('>='+str(self.slope_degree)) # To scree
        if self.path_mnt != '':
            # Add class indexes
            self.tree_direction[0].append(6)
            self.tree_direction[0].append(7)
            
        # Compute zonal stats on Max NDVI raster  
        try:  
            # out_carto.zonal_stats((raster_path[ind_th+1], list_band_outraster[ind_th+1]))
            multi_process_var.append([self.raster_path[ind_th+2], self.list_band_outraster[ind_th+2]])
            # Compute stats twice, because there is 3 classes and not 2
            # out_carto.zonal_stats((raster_path[ind_th+1], list_band_outraster[ind_th+1]))
            multi_process_var.append([self.raster_path[ind_th+2], self.list_band_outraster[ind_th+2]])
        except:
            print('Not MNT on the 3rd step')
            multi_process_var.append([self.raster_path[ind_th+1], self.list_band_outraster[ind_th+1]])
            multi_process_var.append([self.raster_path[ind_th+1], self.list_band_outraster[ind_th+1]])

        # Compute zonal stats with multi processing
        out_carto.stats_dict = mgr.defaultdict(list)
        p = []
        kwargs = {}
        for i in range(len(multi_process_var)):
            kwargs['rank'] = i
            kwargs['nb_img'] = len(multi_process_var)
            p.append(Process(target=out_carto.zonal_stats, args=(multi_process_var[i], ), kwargs=kwargs))
            p[i].start()
            
            if self.mp == 0:
                p[i].join()
        
        if self.mp == 1:       
            for i in range(len(multi_process_var)):
                p[i].join()

        # If there is more one fieldnames line edit fulled in classification tab
        if len(self.sample_name) > 2:
            # Compute the biomass and density distribution
            out_carto.compute_biomass_density()
            
        out_carto.class_tab_final = defaultdict(list)
        self.i_tree_direction()
        out_carto.decision_tree(self.tree_direction)
        
        # If there is more one fieldnames line edit fulled in classification tab
        if len(self.sample_name) > 2:     
            # Compute biomass and density scale
            out_carto.append_scale(self.in_class_name[2], 'self.stats_dict[ind_stats][3]/self.max_bio')
            out_carto.append_scale(self.in_class_name[3], 'self.stats_dict[ind_stats][2]/self.max_wood_idm')
          
        # Final cartography
        out_carto.create_cartography(self.out_fieldname_carto, self.out_fieldtype_carto)