Ejemplo n.º 1
0
    def layer_rasterization(self, raster_head, attribute_r):
        """
        Function to rasterize a vector. Complete the gdal pointer empty properties with the layer's information
        of the vector and a defined field.
        
        :param raster_head: Raster path that will look like the final raster of the rasterization
        :type raster_head: str
        :param attribute_r: Field name of the shapefile that contains class names
        :type attribute_r: str
        :param class_r: Class name corresponding to integer class name
        :type class_r: list of str
        :param class_out: Value field pixels for the output raster
        :type class_out: int
        """
        
        # Export a example of a raster out information
        # for the validation shapefile
        example_raster = RasterSat_by_date('', '', [0]) # Call the raster class
        raster_info = example_raster.raster_data(raster_head)# Extract data info
        
        # Define the validation's vector
        valid_raster = self.vector_used[:-3]+'TIF' # Name of the output raster
        if os.path.exists(str(valid_raster)):
            os.remove(valid_raster)
            
        # Create the empty raster with the same properties
        info_out = example_raster.create_raster(valid_raster, raster_info[0], raster_info[1])
        self.raster_ds = example_raster.out_ds
        
        # Virtual rasterize the vector 
        pt_rast = gdal.RasterizeLayer(self.raster_ds, [1], self.data_source.GetLayer(), \
                                      options=["ATTRIBUTE=" + str(attribute_r)])
        if pt_rast != 0:
            raise Exception("error rasterizing layer: %s" % pt_rast)
        
        new_data = self.raster_ds.ReadAsArray()
#         # Expression to find classes in numpy's mask
#         np_mask = ''
#         # Convert string in a list. For that, it remove
#         # space and clip this string with comma (Add everywhere if the script modified
#         # because the process work with a input string chain)
#         class_r = class_r.replace(' ','').split(',')
#         for nm in class_r:
#             np_mask = np_mask + '(new_data == ' + str(nm) + ') | '
#         np_mask = np_mask[:-3]
#         # Replace value data by our own value
#         new_data=np.ma.masked_where(eval(np_mask), new_data)
#         new_data.fill_value = class_out
#         
#         new_data = new_data.filled()
               
        self.raster_ds = None
        # Complete the raster creation
        example_raster.complete_raster(info_out, new_data)
        
        return valid_raster
Ejemplo n.º 2
0
    def i_img_sat(self):
        
        """
        Interface function to processing satellite images:
        
            1. Clip archive images and modify Archive class to integrate clip image path.
            With :func:`Toolbox.clip_raster` in ``Toolbox`` module.
        
            2. Search cloud's percentage :func:`RasterSat_by_date.RasterSat_by_date.pourc_cloud`, select 
            image and compute ndvi index :func:`RasterSat_by_date.RasterSat_by_date.calcul_ndvi`. If cloud's percentage is 
            greater than 40%, then not select and compute ndvi index.
        
            3. Compute temporal stats on ndvi index [min, max, std, min-max]. With :func:`Toolbox.calc_serie_stats` 
            in ``Toolbox`` module.
            
            4. Create stats ndvi raster and stats cloud raster.
            
            >>> import RasterSat_by_date
            >>> stats_test = RasterSat_by_date(class_archive, big_folder, one_date)
            >>> stats_test.complete_raster(stats_test.create_raster(in_raster, stats_data, in_ds), stats_data)
        """

        # Clip archive images and modify Archive class to integrate clip image path
        for clip in self.check_download.list_img:
            clip_index = self.check_download.list_img.index(clip)
            self.check_download.list_img[clip_index][3] = clip_raster(clip[3], self.path_area) # Multispectral images
            self.check_download.list_img[clip_index][4] = clip_raster(clip[4], self.path_area) # Cloud images
           
        # Images pre-processing
        spectral_out = []
        for date in self.check_download.single_date:
               
            check_L8 = RasterSat_by_date(self.check_download, self.folder_processing, date)
            check_L8.mosaic_by_date()
             
            # Search cloud's percentage, select image and compute ndvi index if > cl
            cl = check_L8.pourc_cloud(check_L8._one_date[3], check_L8._one_date[4])
            if cl > 0.60:
                check_L8.calcul_ndvi(check_L8._one_date[3])
                spectral_out.append(check_L8._one_date)
        
        # Compute temporal stats on ndvi index [min, max, std, min-max]
        spectral_trans = np.transpose(np.array(spectral_out, dtype=object))
        stats_name = ['Min', 'Max', 'Std', 'MaxMin']
        stats_ndvi, stats_cloud = calc_serie_stats(spectral_trans)

        # Create stats ndvi raster and stats cloud raster
        stats_L8 = RasterSat_by_date(self.check_download, self.folder_processing, [int(self.classif_year)])
        # Stats cloud raster
        out_cloud_folder = stats_L8._class_archive._folder + '/' + stats_L8._big_folder + '/' + self.classif_year + \
                           '/Cloud_number_' + self.classif_year + '.TIF'
        stats_L8.complete_raster(stats_L8.create_raster(out_cloud_folder, stats_cloud, \
                                                         stats_L8.raster_data(self.check_download.list_img[0][4])[1]), \
                                 stats_cloud)
        
        # Stats ndvi rasters        
        for stats_index in range(len(stats_ndvi)):
            out_ndvistats_folder = stats_L8._class_archive._folder + '/' + stats_L8._big_folder + '/' + self.classif_year + \
                           '/' + stats_name[stats_index] + '_' + self.classif_year + '.TIF'
            self.out_ndvistats_folder_tab[stats_index] = out_ndvistats_folder
            stats_L8.complete_raster(stats_L8.create_raster(out_ndvistats_folder, stats_ndvi[stats_index], \
                                                            stats_L8.raster_data(self.check_download.list_img[0][4])[1]), \
                                     stats_ndvi[stats_index])