Exemple #1
0
 def test_calculate_ndvi(self):
     from madmex.util import get_parent
     from madmex.mapper.data import raster
     from madmex.processing.raster import calculate_ndvi
     import numpy
     image = "/Users/erickpalacios/Documents/CONABIO/Tareas/4_RedisenioMadmex/5_Clasificacion/rapideyemapgrid/folder_test/rapideye/1649125/2014/2014-01-23/L3A/1649125_2014-01-23_RE4_3A_301519.tif"
     gdal_format = "GTiff"
     data_class = raster.Data(image, gdal_format)
     array = data_class.read_data_file_as_array()
     width, height, bands = data_class.get_attribute(raster.DATA_SHAPE)
     feature_bands = numpy.zeros([2, width, height])
     feature_bands[0, :, :] = calculate_ndvi(array[4, :, :], array[2, :, :])
     feature_bands[1, :, :] = calculate_ndvi(array[3, :, :], array[2, :, :])
     out = get_parent(image) + 'result_ndvi'
     raster.create_raster_tiff_from_reference(data_class.metadata, out, feature_bands)
Exemple #2
0
 def get_thumbnail(self):
     '''
     Creates a thumbnail for the scene in true color.
     '''
     from subprocess import call
     file_1 = self.file_dictionary[_BASE % (self.get_mission(), 'B1[0-9].TIF')]
     file_2 = self.file_dictionary[_BASE % (self.get_mission(), 'B2[0-9].TIF')]
     file_3 = self.file_dictionary[_BASE % (self.get_mission(), 'B3[0-9].TIF')]
     parent_directory = get_parent(self.path)
     thumnail_directory = create_file_name(parent_directory, 'thumbnail')
     create_directory_path(thumnail_directory)
     parent_directory
     filename = create_file_name(thumnail_directory, 'vrt.tif')
     merge_command = ['/Library/Frameworks/GDAL.framework/Programs/gdalbuildvrt', '-separate', '-o', filename, file_3, file_2, file_1]
     call(merge_command)
     thumbnail = create_file_name(thumnail_directory, 'thumbnail.jpg')
     resize_command = ['/Library/Frameworks/GDAL.framework/Programs/gdal_translate', filename, '-of', 'JPEG', '-outsize', '5%', '5%', thumbnail]
     call(resize_command)
Exemple #3
0
    def preprocess(self):
        '''
        Top of atmosphere is calculated and persisted into a file. Then a cloud
        mask is created with the given algorithm.
        '''
        solar_zenith = self.get_sensor().parser.get_attribute(rapideye.SOLAR_ZENITH)
        data_acquisition_date = self.get_sensor().parser.get_attribute(rapideye.ACQUISITION_DATE)
        solar_azimuth = self.get_sensor().parser.get_attribute(rapideye.SOLAR_AZIMUTH)
        geotransform = self.get_raster().get_attribute(raster.GEOTRANSFORM)
        data = self.get_raster().read_data_file_as_array()

        sun_earth_distance = calculate_distance_sun_earth(data_acquisition_date)
        top_of_atmosphere_data = calculate_toa_rapideye(calculate_rad_rapideye(data), sun_earth_distance, solar_zenith)
        top_of_atmosphere_directory = create_file_name(get_parent(self.path), 'TOA')

        create_directory_path(top_of_atmosphere_directory)
        output_file = create_file_name(top_of_atmosphere_directory, get_base_name(self.get_files()[2]) + '_toa.tif')  # TODO: change [2] in self.get_files()[2] 

        create_raster_from_reference(output_file,
                                     top_of_atmosphere_data,
                                     self.file_dictionary[_IMAGE],
                                     data_type=NumericTypeCodeToGDALTypeCode(numpy.float32)
                                     )
        LOGGER.debug('Top of atmosphere file was created.')
        cloud_output_file = create_file_name(top_of_atmosphere_directory, get_base_name(self.get_files()[2]) + '_cloud.tif')

        if self.algorithm == ANOMALY_DETECTION:            
            LOGGER.debug('Cloud mask by anomaly detection process.')
            clouds = self.anomaly_detection_cloud_mask(top_of_atmosphere_data, cloud_output_file, solar_zenith, solar_azimuth, geotransform)
        elif self.algorithm == TIME_SERIES:
            LOGGER.debug('Cloud mask by reference with time series process.')
            tile_id = self.get_sensor().get_attribute(TILE_ID)
            clouds = self.masking_with_time_series(data, cloud_output_file, solar_zenith, solar_azimuth, geotransform, tile_id)

        create_raster_from_reference(cloud_output_file,
                     clouds,
                     self.file_dictionary[_IMAGE],
                     data_type=NumericTypeCodeToGDALTypeCode(numpy.float32)
                     )
        LOGGER.info('Cloud mask was created.')
Exemple #4
0
 def handle(self, **options):
     '''
     This is the code that does the ingestion.
     '''
     interest_band = 1
     for image_path in options['path']:
             print image_path
             ds = gdal.Open(image_path)
             bands = ds.RasterCount           
             geotransform = ds.GetGeoTransform()     
             x_resolution = geotransform[1]
             y_resolution = geotransform[5]
             pixel_area = abs(x_resolution * y_resolution)                
             array = numpy.array(ds.GetRasterBand(interest_band).ReadAsArray())
             print numpy.unique(array)
             flat = numpy.ravel(array)
             length = len(flat)
             parent = get_parent(image_path)
             basename = '%s.txt' % get_base_name(image_path)
             target = create_file_name(parent, basename)
             count = 1                
             values = {}
             progress = 0
             for value in flat:
                 count = count + 1
                 if not values.get(value):
                     values[value] = 1
                 else:
                     values[value] = values[value] + 1
                 if count % 1000000 == 0:    
                     aux = progress
                     progress = math.floor(100 * count / float(length))
                     if not aux == progress:
                         print  str(int(progress)) + '%\r'
             added = self.add_up(INITIAL_ARRAY, values)
             area = self.transform_to_area(added, pixel_area)
             with open(target, "a") as f:
                 json.dump(area, f)
Exemple #5
0
 def test_maf_image(self):
     '''
     Perform a maf transformation with the result of imad transformation
     '''
     from madmex.mapper.data import raster
     from madmex.transformation import maf
     gdal_format = "GTiff"
     #image_imad = '/Users/erickpalacios/test_imad_pair_images/result_change_detection.tif'
     #image_imad = '/Users/erickpalacios/Documents/CONABIO/Tareas/1_DeteccionCambiosSpot/2_AdapterParaDeteccionDeCambios/Tarea2/res12CambiosMadTransfJulian/593_318_031210_SP5_593_318_021114_SP5_mad.tif'
     image_imad = '/LUSTRE/MADMEX/staging/antares_test/test_imad_pair_images/result_mad_prueba.tif'
     image_imad_class = raster.Data(image_imad, gdal_format)
     width, height , bands = image_imad_class.get_attribute(raster.DATA_SHAPE)
     print 'bands:'
     print bands
     geotransform = image_imad_class.get_attribute(raster.GEOTRANSFORM) 
     projection = image_imad_class.get_attribute(raster.PROJECTION)
     maf_class = maf.Transformation(image_imad_class.read_data_file_as_array())
     maf_class.execute()
     output = get_parent(image_imad) 
     output+= '/result_maf.tif' 
     print output
     image_maf = image_imad_class.create_from_reference(output, width, height, bands-1, geotransform, projection)
     print 'write'
     image_imad_class.write_raster(image_maf, maf_class.output)
Exemple #6
0
 def get_thumbnail(self):
     '''
     Creates a thumbnail for the scene in true color.
     '''
     parent_directory = get_parent(self.path)
     self.get_thumbnail_with_path(parent_directory)
Exemple #7
0
            clouds = self.masking_with_time_series(data, cloud_output_file, solar_zenith, solar_azimuth, geotransform, tile_id)

        create_raster_from_reference(cloud_output_file,
                     clouds,
                     self.file_dictionary[_IMAGE],
                     data_type=NumericTypeCodeToGDALTypeCode(numpy.float32)
                     )
        LOGGER.info('Cloud mask was created.')

if __name__ == '__main__':
    #path = '/Users/amaury/Documents/rapideye/df/1447813/2012/2012-03-14/l3a'
    #bundle = Bundle(path)
    #print bundle.get_files()
    #print bundle.can_identify()
    
    
    image = "/Users/agutierrez/Documents/rapideye/df/1447813/2012/2012-03-14/l3a/2012-03-14T182106_RE2_3A-NAC_11040070_149070.tif"
    gdal_format = "GTiff"
    data_class = raster.Data(image, gdal_format)
    array = data_class.read_data_file_as_array()
    width, height, bands = data_class.get_attribute(raster.DATA_SHAPE)
    feature_bands = numpy.zeros([2, width, height])
    from madmex.processing.raster import calculate_ndvi
    feature_bands[0, :, :] = calculate_ndvi(array[4, :, :], array[2, :, :])
    feature_bands[1, :, :] = calculate_ndvi(array[3, :, :], array[2, :, :])
    
    out1 = get_parent(image) + 'result_ndvi_1.tif'
    out2 = get_parent(image) + 'result_ndvi_2.tif'
    create_raster_from_reference(out1, feature_bands[0, :, :], image)
    create_raster_from_reference(out2, feature_bands[1, :, :], image)
    print 'Done'