def cog(input_tif, output_tif,no_data=None):
    
    translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                    '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                    '-co COMPRESS=LZW '))
    
    if no_data != None:
        translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                        '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                        '-co COMPRESS=LZW '\
                                                                        '-a_nodata {}'.format(no_data)))
    ds = gdal.Open(input_tif, gdal.OF_READONLY)

    gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
    ds.BuildOverviews('NEAREST', [2,4,8,16,32])
    
    ds = None

    ds = gdal.Open(input_tif)
    gdal.Translate(output_tif,
                   ds, 
                   options=translate_options)
    ds = None

    os.remove('{}.ovr'.format(input_tif))
    os.remove(input_tif)
def cog(input_tif):
    
    temp_tif = 'temp.tif'
    
    shutil.move(input_tif, temp_tif)
    
    translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                    '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                    ' -co COMPRESS=DEFLATE'))

    ds = gdal.Open(temp_tif, gdal.OF_READONLY)

    gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
    ds.BuildOverviews('NEAREST', [2,4,8,16,32])
    
    ds = None

    ds = gdal.Open(temp_tif)
    gdal.Translate(input_tif,
                   ds, 
                   options=translate_options)
    ds = None

    os.remove('{}.ovr'.format(temp_tif))
    os.remove(temp_tif)
Beispiel #3
0
def cog(input_tif):

    temp_tif = "temp.tif"

    shutil.move(input_tif, temp_tif)

    translate_options = gdal.TranslateOptions(
        gdal.ParseCommandLine(
            "-co TILED=YES " "-co COPY_SRC_OVERVIEWS=YES " " -co COMPRESS=DEFLATE"
        )
    )

    ds = gdal.Open(temp_tif, gdal.OF_READONLY)

    gdal.SetConfigOption("COMPRESS_OVERVIEW", "DEFLATE")
    ds.BuildOverviews("NEAREST", [2, 4, 8, 16, 32])

    ds = None

    ds = gdal.Open(temp_tif)
    gdal.Translate(input_tif, ds, options=translate_options)
    ds = None

    os.remove("{}.ovr".format(temp_tif))
    os.remove(temp_tif)
Beispiel #4
0
def cog(ds, output_name):

    translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                    '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                    '-co COMPRESS=LZW '))

    gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
    ds.BuildOverviews('NEAREST', [2, 4, 8, 16, 32])
    gdal.Translate(output_name, ds, options=translate_options)
    ds = None
Beispiel #5
0
def cog(input_tif, output_tif, band=None):
    
    if band is not None:
        
        translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                        '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                        '-co BIGTIFF=YES ' \
                                                                        '-co COMPRESS=LZW ' \
                                                                        '-ot Float32 ' \
                                                                        '-b {}'.format(band)))
    else:
        translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                        '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                        '-co BIGTIFF=YES ' \
                                                                        '-co COMPRESS=LZW ' \
                                                                        '-ot Float32 ' \
                                                                        '-b 1 -b 2 -b 3'))

    ds = gdal.Open(input_tif, gdal.OF_READONLY)
    
    gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
    ds.BuildOverviews('NEAREST', [2,4,8,16,32])
    
    ds = None

    ds = gdal.Open(input_tif)

    gdal.Translate(output_tif,
                   ds, 
                   options=translate_options)

    ds = None
    if (os.path.exists(output_tif)):
        ciop.log('INFO','COG {} created'.format(output_tif))
    else:
        ciop.log('ERROR','COG {} NOT created'.format(output_tif))
       
    try:
        os.remove('{}.ovr'.format(input_tif))
    except:
        pass
def create_rbb(in_rgb, out_rbb):

    #gdal_translate -ot UInt16 -a_nodata 256 ${1::-14}RED-BLUE.rgb.tif ${1::-8}.acd.tif -co COMPRESS=LZW -b 1 -b 3 -b 3

    translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co COMPRESS=LZW '\
                                                                    '-ot UInt16 ' \
                                                                    '-a_nodata 256 ' \
                                                                    '-b 1 -b 3 -b 3 '))

    ds = gdal.Open(in_rgb, gdal.OF_READONLY)

    gdal.Translate(out_rbb, ds, options=translate_options)
Beispiel #7
0
    def __init__(self, **kwargs):
        kwargs['defaults'] = {
         'store_msg'  : [],\
         'database'   : None,\
         'product'    : 'MCD15A3H',\
         'tile'       : 'h08v06',\
         'log'        : None,\
         'day'        : '01',\
         'doy'        : None,
         'month'      : '*',\
         'sds'        : None,
         'year'       : "2019",\
         'site'       : 'https://e4ftl01.cr.usgs.gov',\
         'size_check' : False,\
         'noclobber'  : True,\
         'local_dir'  : 'work',\
         'local_file' : None,\
         'db_file'    : None,\
         'db_dir'     : 'work',\
         'verbose'    : False,\
         'stderr'     : sys.stderr
        }
        self.__dict__.update(ginit(self, **kwargs))
        if 'database' in self.__dict__ and type(self.database) == Database:
            # already have databse stored
            pass
        else:
            self.database = Database(self.db_file,\
                              **(fdict(self.__dict__.copy(),ignore=['db_dir','db_file'])))

        self.translateoptions = gdal.TranslateOptions(
            gdal.ParseCommandLine("-of Gtiff -co COMPRESS=LZW"))
        # list of tiles
        if type(self.tile) is str:
            self.tile = [self.tile]

        if type(self.sds) is str:
            self.sds = [self.sds]
        if self.sds is not None:
            self.msg(f'initial SDS {self.sds}')
            self.required_sds = self.sds

        # for most transactions, we want all SDS
        # so self.sds should reflect that
        self.sds = None
        response = self.database.get_from_db('SDS', self.product)
        if response:
            self.msg("found SDS names in database")
            self.sds = response
            self.msg(self.sds)
            # require them all
            if 'required_sds' not in self.__dict__:
                self.required_sds = self.sds
def create_rgb(in_mineral, out_rgb):
    
    translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co COMPRESS=LZW '\
                                                                    '-ot UInt16 ' \
                                                                    '-a_nodata 256 ' \
                                                                    '-b 1 -b 2 -b 3 -b 4'))
                                                                    
    ds = gdal.Open(in_mineral, gdal.OF_READONLY)

    gdal.Translate(out_rgb, 
                   ds, 
                   options=translate_options)
Beispiel #9
0
    def batch_create_cog(self, list_of_files, out_dir):
        """
        Create cloud optimized geotiffs.

        :param list_of_files:   A list of input files.
        :param out_dir:         Output location.
        """

        # create gdal_translate command string
        trans_str = (f'-co TILED=YES '
                     f'-co COPY_SRC_OVERVIEWS=YES '
                     f'-co COMPRESS={self.compress_method} '
                     f'-co NUM_THREADS=ALL_CPUS '
                     f'-co BLOCKXSIZE={str(self.blockxsize)} '
                     f'-co BLOCKYSIZE={str(self.blockysize)}')

        # parse command line syntax to be passed to translate options
        tp = gdal.ParseCommandLine(trans_str)

        # pass parsed string to TranslateOptions object
        trans_opt = gdal.TranslateOptions(tp)

        # define output path for cloud optimized geotiffs
        out = os.path.join(out_dir, 'COG')
        os.mkdir(out)  # make the dir
        os.chdir(out)  # navigate to dir

        # loop the files and convert
        for file in tqdm(list_of_files):
            out_name = f'{os.path.splitext(os.path.basename(file))[0]}.tif'
            # if file already exists, pass
            if os.path.exists(out_name):
                continue

            # make COGS
            try:
                gdal.Translate(out_name, file, options=trans_opt
                               )  # pass translate options to gdal.Translate()

            # log any errors encountered
            except Exception as e:
                with open(os.path.join(out, 'COG_creation_errors.txt'),
                          'a+') as err_file:
                    err_file.write(f'{file}\t{e}\n')
Beispiel #10
0
    def create_translate_options(self):
        """
        Create translate options object.

        :return: gdal_translate options.
        """

        # amend gdal command string for jpeg
        if self.compress_method == 'JPEG':
            self.set_jpeg_quality()

        # Create gdal_translate command options
        trans_str = (f'-of GTiff -a_srs EPSG:{str(self.epsg)} '
                     f'-co PREDICTOR={str(self.predictor)} -q '
                     f'-co TILED=YES -co NUM_THREADS=ALL_CPUS '
                     f'-co COMPRESS={self.compress_method} '
                     f'-co ZLEVEL={str(self.compress_level)}')
        # pass parsed string to TranslateOptions object
        return gdal.TranslateOptions(gdal.ParseCommandLine(trans_str))
Beispiel #11
0
    def __init__(self, **kwargs):
        kwargs['defaults'] = {
         'store_msg'  : [],\
         'database'   : None,\
         'product'    : 'MCD15A3H',\
         'tile'       : 'h08v06',\
         'log'        : None,\
         'day'        : '01',\
         'doy'        : None,
         'month'      : '*',\
         'sds'        : None,
         'year'       : "2019",\
         'site'       : 'https://e4ftl01.cr.usgs.gov',\
         'size_check' : False,\
         'noclobber'  : True,\
         'local_dir'  : 'work',\
         'local_file' : None,\
         'db_file'    : None,\
         'db_dir'     : 'work',\
         'verbose'    : False,\
         'stderr'     : sys.stderr
        }
        self.__dict__.update(ginit(self, **kwargs))
        if 'database' in self.__dict__ and type(self.database) == Database:
            # already have databse stored
            pass
        else:
            self.database = Database(self.db_file,\
                              **(fdict(self.__dict__.copy(),ignore=['db_dir','db_file'])))

        self.translateoptions = gdal.TranslateOptions(
            gdal.ParseCommandLine("-of Gtiff -co COMPRESS=LZW"))

        # list of tiles
        if type(self.tile) is str:
            self.tile = [self.tile]

        if type(self.sds) is str:
            self.sds = [self.sds]
Beispiel #12
0
def generate_compressed_images(in_files, input_image):
    """
    Converts the input file to every kind of GDAL compression and saves the time
    it takes to write each file.
    """
    try:
        print('|BEGIN - ' + os.path.basename(os.path.realpath(__file__)) +
              ' @ ' + sys._getframe().f_code.co_name + '|\n')

        # Set Command variables for GDAL_Translate
        command_uncompressed = "-of GTiff -co \"BIGTIFF=IF_SAFE\""
        command_packbits = "-of GTiff -co \"BIGTIFF=IF_SAFE\" -co \"COMPRESS=PACKBITS\" -co \"TILED=YES\""
        command_deflate_1 = "-of GTiff -co \"BIGTIFF=IF_SAFE\" -co \"COMPRESS=DEFLATE\" -co \"PREDICTOR=1\" -co \"TILED=YES\""
        command_deflate_2 = "-of GTiff -co \"BIGTIFF=IF_SAFE\" -co \"COMPRESS=DEFLATE\" -co \"PREDICTOR=2\" -co \"TILED=YES\""
        command_lzw_1 = "-of GTiff -co \"BIGTIFF=IF_SAFE\" -co \"COMPRESS=LZW\" -co \"PREDICTOR=1\" -co \"TILED=YES\""
        command_lzw_2 = "-of GTiff -co \"BIGTIFF=IF_SAFE\" -co \"COMPRESS=LZW\" -co \"PREDICTOR=2\" -co \"TILED=YES\""

        # Declare write speed variables
        write_uncompressed = ''
        write_packbits = ''
        write_deflate_1 = ''
        write_deflate_2 = ''
        write_lzw_1 = ''
        write_lzw_2 = ''

        # set gdal values
        for file in in_files:
            start_time = time.time()
            dest_name = file
            src_ds = input_image
            if file == uncompressed:
                gdal.Open(src_ds)
                translate_options = gdal.TranslateOptions(
                    gdal.ParseCommandLine(command_uncompressed))
                gdal.Translate(dest_name, src_ds, options=translate_options)
                write_uncompressed = time.time() - start_time
                continue

            elif file == packbits:
                gdal.Open(src_ds)
                translate_options = gdal.TranslateOptions(
                    gdal.ParseCommandLine(command_packbits))
                gdal.Translate(dest_name, src_ds, options=translate_options)
                write_packbits = time.time() - start_time
                continue

            elif file == deflate_1:
                gdal.Open(src_ds)
                translate_options = gdal.TranslateOptions(
                    gdal.ParseCommandLine(command_deflate_1))
                gdal.Translate(dest_name, src_ds, options=translate_options)
                write_deflate_1 = time.time() - start_time
                continue

            elif file == deflate_2:
                gdal.Open(src_ds)
                translate_options = gdal.TranslateOptions(
                    gdal.ParseCommandLine(command_deflate_2))
                gdal.Translate(dest_name, src_ds, options=translate_options)
                write_deflate_2 = time.time() - start_time
                continue

            elif file == lzw_1:
                gdal.Open(src_ds)
                translate_options = gdal.TranslateOptions(
                    gdal.ParseCommandLine(command_lzw_1))
                gdal.Translate(dest_name, src_ds, options=translate_options)
                write_lzw_1 = time.time() - start_time
                continue

            elif file == lzw_2:
                gdal.Open(src_ds)
                translate_options = gdal.TranslateOptions(
                    gdal.ParseCommandLine(command_lzw_2))
                gdal.Translate(dest_name, src_ds, options=translate_options)
                write_lzw_2 = time.time() - start_time
                continue

            else:
                continue

        output_write_speeds = [write_uncompressed, write_packbits,
                               write_deflate_1, write_deflate_2, write_lzw_1, write_lzw_2]

        # Console Success...
        print('|SUCCESS - ' + os.path.basename(os.path.realpath(__file__)) +
              ' @ ' + sys._getframe().f_code.co_name + '|\n')

        return output_write_speeds

    # Handles errors in general and 'err' holds the details of the error collected...
    except Exception as err:
        print('|FAIL - ' + os.path.basename(os.path.realpath(__file__)) +
              ' @ ' + sys._getframe().f_code.co_name + ' - ' + str(err) + '|\n')