示例#1
0
def hdf_to_tiff(base_folder_modis, list_of_files, output_folder, datasets):

    for f in list_of_files:

        path_to_file = os.path.join(base_folder_modis, f)
        modis_data = gdal.Open(path_to_file)
        subdatasets = modis_data.GetSubDatasets()

        vals = []
        output_paths = []
        if type(datasets) == int:
            val = subdatasets[datasets][0]
            vals.append(val)
            filename = 'mod_' + f[:-4] + str(datasets) + '.tif'
            output_path = os.path.join(output_folder, filename)
            output_paths.append(output_path)
            gdal.Translate(output_path,
                           val,
                           options=gdal.TranslateOptions([b'-ot', b'Float32']))
        else:
            for ds in datasets:
                val = subdatasets[ds][0]
                vals.append(val)
                filename = 'mod_' + f[:-4] + str(ds) + '.tif'
                output_path = os.path.join(output_folder, filename)
                output_paths.append(output_path)
                gdal.Translate(output_path,
                               val,
                               options=gdal.TranslateOptions(
                                   [b'-ot', b'Float32']))

        del modis_data
        for p in output_paths:
            print('File {} processed successfully'.format(p))
    return output_paths
示例#2
0
def renderVRT(fname, data_lyr, geotrans=None, drivername='ENVI', gdal_fmt='float32', proj=None, nodata=None, verbose=False):
    """Exports raster and renders corresponding VRT file."""
    gdalMap = { 'byte'   : 1,
                'int16'  : 3,
                'int32'    : 5,
                'float32'  : 6,
                'float64' : 7,
                'cfloat32' : 10,
                'cfloat64': 11}

    gdalfile=gdal.GetDriverByName(drivername).Create(fname, data_lyr.shape[1], data_lyr.shape[0], 1, gdalMap[gdal_fmt])
    gdalfile.GetRasterBand(1).WriteArray(data_lyr)
    if geotrans: #If user wishes to update geotrans.
        gdalfile.SetGeoTransform(geotrans)
    if proj: #If user wishes to update projection.
        gdalfile.SetProjection(proj)
    if nodata is not None: #If user wishes to set nodata val.
        gdalfile.GetRasterBand(1).SetNoDataValue(nodata)
        # Finalize VRT
        gdal.Translate(fname+'.vrt', gdalfile, options=gdal.TranslateOptions(format="VRT", noData=nodata))
    else:
        # Finalize VRT
        gdal.Translate(fname+'.vrt', gdalfile, options=gdal.TranslateOptions(format="VRT"))

    gdalfile = None

    return
示例#3
0
def multilook(infile, outname=None, alks=5, rlks=15, multilook_tool="isce", no_data=None):
    '''
    Take looks.
    '''

    if multilook_tool=="gdal":

        from osgeo import gdal

        print("multi looking using gdal ...")
        if outname is None:
            spl = os.path.splitext(infile)
            ext = '.{0}alks_{1}rlks'.format(alks, rlks)
            outname = spl[0] + ext + spl[1]
        
        print(infile)
        ds = gdal.Open(infile + ".vrt", gdal.GA_ReadOnly)

        xSize = ds.RasterXSize
        ySize = ds.RasterYSize

        outXSize = xSize/int(rlks)
        outYSize = ySize/int(alks)

        if no_data:
            gdalTranslateOpts = gdal.TranslateOptions(format="ENVI", width=outXSize, height=outYSize, noData=no_data)
        else:
            gdalTranslateOpts = gdal.TranslateOptions(format="ENVI", width=outXSize, height=outYSize)

        gdal.Translate(outname, ds, options=gdalTranslateOpts)       
        ds = None

        
        ds = gdal.Open(outname, gdal.GA_ReadOnly)
        gdal.Translate(outname+".vrt", ds, options=gdal.TranslateOptions(format="VRT"))
        ds = None

    else:
        from mroipac.looks.Looks import Looks

        print('Multilooking {0} ...'.format(infile))

        inimg = isceobj.createImage()
        inimg.load(infile + '.xml')

        if outname is None:
            spl = os.path.splitext(inimg.filename)
            ext = '.{0}alks_{1}rlks'.format(alks, rlks)
            outname = spl[0] + ext + spl[1]

        lkObj = Looks()
        lkObj.setDownLooks(alks)
        lkObj.setAcrossLooks(rlks)
        lkObj.setInputImage(inimg)
        lkObj.setOutputFilename(outname)
        lkObj.looks()

    return outname
示例#4
0
def paso2SuomiNPP(paso1, paso2):
    print('Obteniendo datos...\nUniendo paso 1 y paso 2...')

    xmin = -118
    xmax = -85
    ymin = 12
    ymax = 35

    ds_1 = gdal.Open(paso1)
    ds_2 = gdal.Open(paso2)

    gdal.Warp('data_1_4326.tif',
              ds_1,
              options=gdal.WarpOptions(dstSRS='EPSG:4326', dstNodata=-9999))
    gdal.Warp('data_2_4326.tif',
              ds_2,
              options=gdal.WarpOptions(dstSRS='EPSG:4326', dstNodata=-9999))

    ds_1 = gdal.Open('data_1_4326.tif')
    ds_2 = gdal.Open('data_2_4326.tif')

    gdal.Translate(
        'data_1_4326_rec.tif',
        ds_1,
        options=gdal.TranslateOptions(projWin=[xmin, ymax, -96.5, ymin]))
    gdal.Translate(
        'data_2_4326_rec.tif',
        ds_2,
        options=gdal.TranslateOptions(projWin=[-96.5, ymax, xmax, ymin]))

    ds_1 = gdal.Open('data_1_4326_rec.tif')
    ds_2 = gdal.Open('data_2_4326_rec.tif')

    data_1 = ds_1.ReadAsArray()
    data_2 = ds_2.ReadAsArray()

    data = np.concatenate((data_1, data_2), axis=1)

    data[data == -9999.000000] = np.nan
    data[data == -340282346638528859811704183484516925440.000000] = np.nan

    nx = data.shape[0]
    ny = data.shape[1]

    ds_1 = None
    ds_2 = None
    data_1 = None
    data_2 = None

    os.remove('data_1_4326.tif')
    os.remove('data_2_4326.tif')
    os.remove('data_1_4326_rec.tif')
    os.remove('data_2_4326_rec.tif')

    return data, xmin, ymin, xmax, ymax, nx, ny
示例#5
0
def process_dir(subdir):
    args = get_args()
    start2 = time.time()

    plot = subdir.split('/')[-1]
    plot_split = plot.split(' ')
    cwd = os.getcwd()
    plot_name = '_'.join(plot_split)

    vrt_options = gdal.BuildVRTOptions(srcNodata="0 0 0",
                                       resampleAlg='cubic',
                                       addAlpha=False)

    # Create VRT
    #os.chdir(subdir)
    images = glob.glob(f'{subdir}/*.tif')
    vrt = gdal.BuildVRT('my.vrt', images, options=vrt_options)

    # Create geoTiff from VRT
    translateOptions = gdal.TranslateOptions(
        creationOptions=["TILED=YES", "COMPRESS=LZW", "BIGTIFF=YES"])
    gdal.Translate(f'{args.outdir}/{plot_name}_ortho.tif',
                   vrt,
                   driver="GTiff",
                   options=translateOptions)
    vrt = None
示例#6
0
def mosaic(rstr_lst, out_path):
    ''' Takes a list of raster files and merges them together '''

    print("--- starting mosaic ---")

    vrt_options = gdal.BuildVRTOptions(resampleAlg='nearest',
                                       addAlpha=True,
                                       xRes=30,
                                       yRes=30)

    #create the VRT with the raster list
    temp_vrt = gdal.BuildVRT('temp.vrt', rstr_lst, options=vrt_options)

    #we need to specify the translation option before,
    # here we add Gtiff and COMPRESS; to deal with big rasters and compression, the final output will have
    # 4gb
    #we can set other commands as well
    translateoptions = gdal.TranslateOptions(
        gdal.ParseCommandLine(("-of Gtiff -co COMPRESS=LZW")))

    #time it
    start_time = time.time()
    #apply gdalTranslate and then save the raster
    gdal.Translate(out_path, temp_vrt, options=translateoptions)
    #print a message as soon as it is over!
    print("--- {0} merged in {1} minutes ---".format(
        os.path.basename(out_path), round((time.time() - start_time) / 60, 2)))
示例#7
0
def dataVentana(x, y, offset):
    print('Obteniendo datos de ventana...')

    coorVentana = coordenadasVentana(x, y, offset)

    ds = gdal.Open('tmp.tif')

    gdal.Warp('tmp_4326.tif',
              ds,
              options=gdal.WarpOptions(dstSRS='EPSG:4326',
                                       dstNodata=-9999.000))

    ds = gdal.Open('tmp_4326.tif')

    gdal.Translate('tmp_4326_rec.tif',
                   ds,
                   options=gdal.TranslateOptions(projWin=coorVentana,
                                                 noData=np.nan))

    ds = gdal.Open('tmp_4326_rec.tif')

    data = ds.ReadAsArray()

    data[data == -9999.000] = np.nan

    ds = None

    os.remove('tmp.tif')
    os.remove('tmp_4326.tif')
    os.remove('tmp_4326_rec.tif')

    return data
    def build_clc_raster(self):
        xy_resolution = self.config['AGGREGATION']['DEM']
        for filename in [
                self.filepath.root_work_path + self.get_layer_fn('clc'),
                self.filepath.root_tmp_path + 'clc_rst.vrt'
        ]:
            if os.path.exists(filename):
                os.remove(filename)

        input_fn = self.filepath.root_clc_path + self.config['FILES']['CLC']
        vrt_options = gdal.BuildVRTOptions(
            resampleAlg=Resampling.mode,
            xRes=xy_resolution,
            yRes=xy_resolution,
            outputBounds=self.padded_bounding_box)
        test_vrt = gdal.BuildVRT(self.filepath.root_tmp_path + 'clc_rst.vrt',
                                 input_fn,
                                 options=vrt_options)

        test_vrt = None

        gdal.Translate(self.filepath.root_work_path + self.get_layer_fn('clc'),
                       self.filepath.root_tmp_path + 'clc_rst.vrt')
        translate_options = gdal.TranslateOptions(
            xRes=xy_resolution,
            yRes=xy_resolution,
            outputBounds=self.padded_bounding_box)

        gdal.Translate(self.filepath.root_work_path + self.get_layer_fn('clc'),
                       input_fn,
                       options=translate_options)
示例#9
0
def save_as_png(tif):
    """
    Save tif as a JPEG with web mercator projection

    tif = 'path/to/tif/file'
    """
    print("Saving jpeg...")
    start_time = time.time()
    # setup jpg access
    jpg = tif.replace('.tif', '.png')

    # define web mercator for web maps
    web_srs = osr.SpatialReference()
    web_srs.ImportFromEPSG(3857)

    # setup options
    translate_options = gdal.TranslateOptions(
        format='PNG',
        outputType=gdal.GDT_Byte,
        scaleParams=[''],
        # scaleParams=[[0, 4294967296, 0, 255]],
        outputSRS=web_srs)

    # convert tif to jpg
    gdal.Translate(destName=jpg, srcDS=tif, options=translate_options)
    print("done... took {0}".format(time.time() - start_time))
示例#10
0
def stack_geotiff(tifs,
                  outtif='stacked.tif',
                  options=['INTERLEAVE=PIXEL'],
                  remove=True):
    """
    Merges multiple TIFFs to one Multilayer TIFF, all files need to have the same extent, projection, pixel size    

    Args:
        remove (boolean): Define if the result should be removed after the operation
        tifs (list): A list with the path to the TIFF files

    Returns:
        stacked.tif (TIFF): Writes a Multiband TIFF to the current work directory + returns it as GDAL Dataset
    """
    import os
    from osgeo import gdal
    outvrt = '/vsimem/stacked.vrt'  # /vsimem is special in-memory virtual "directory"
    bv_options = gdal.BuildVRTOptions(options, separate=True)
    outds = gdal.BuildVRT(outvrt, tifs, options=bv_options)
    tr_options = gdal.TranslateOptions(creationOptions=options)
    outds = gdal.Translate(outtif, outds, options=tr_options)
    if remove:
        for i in tifs:
            os.remove(i)
    print('\n The stacked tiff can be found in the working directory under ' +
          outtif)
    return
示例#11
0
def upload():
    if request.method == "GET":
        flash("GET /upload: You shouldn't be here.")
        return redirect("/")
    else:
        if 'file' not in request.files:
            flash("no file present")
            return redirect('/')
        file = request.files['file']
        if file.filename == '':
            flash("no file selected")
            return redirect('/')
        if file and allowed_file(file.filename):
            src_full = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)

            src_name = os.path.splitext(file.filename)[0]
            src_path = os.path.join(app.config['UPLOAD_FOLDER'], src_name)

            save_file(file)
            run_gdal(
                src_full, src_path,
                gdal.TranslateOptions(format="PDS4",
                                      options=make_options(request.form)))

            flash("PDS4 label generated.")

            return sendlabelfile(src_path)
def convert_hdf5_to_geotiff(filename: str) -> bool:
    from pathlib import Path

    ogr.UseExceptions()
    inp_dir = os.environ.get('NTL_HDF5_DIR')
    out_dir = os.environ.get('NTL_TIF_DIR')
    if filename.endswith('.h5'):
        targetfile = Path(filename).stem
        targetfile += '.tif'
        hdflayer = gdal.Open(inp_dir + filename, gdal.GA_ReadOnly)

        subhdflayer = hdflayer.GetSubDatasets()[0][0]
        rlayer = gdal.Open(subhdflayer, gdal.GA_ReadOnly)

        # collect bounding boxs
        horizontal_tile_number = int(rlayer.GetMetadata_Dict()[
                                     "HorizontalTileNumber"])
        vertical_tile_number = int(rlayer.GetMetadata_Dict()[
                                   "VerticalTileNumber"])
        west_bound_coord = (10 * horizontal_tile_number) - 180
        north_bound_coord = 90 - (10 * vertical_tile_number)
        east_bound_coord = west_bound_coord + 10
        south_bound_coord = north_bound_coord - 10

        epsg = "-a_srs EPSG:4326"  # WGS84 coordinate system

        translate_option_text = epsg+" -a_ullr " + str(west_bound_coord) + " " + str(
            north_bound_coord) + " " + str(east_bound_coord) + " " + str(south_bound_coord)
        translate_options = gdal.TranslateOptions(
            gdal.ParseCommandLine(translate_option_text))
        gdal.Translate(out_dir + targetfile, rlayer, options=translate_options)

        return True

    return False
示例#13
0
def country_biomass_compute(iso3,
                            input_dir="data_raw",
                            output_dir="data",
                            proj="EPSG:3395"):
    """Function to mosaic and resample biomass data from WHRC.

    This function mosaics and resamples the biomass data obtained from
    GEE. A reprojection can be performed.

    :param iso3: Country ISO 3166-1 alpha-3 code.

    :param input_dir: Directory with input files for biomass.

    :param output_dir: Output directory.

    :param proj: Projection definition (EPSG, PROJ.4, WKT) as in
        GDAL/OGR. Default to "EPSG:3395" (World Mercator).

    """

    # Create output directory
    make_dir("data")

    # Mosaicing
    files_tif = input_dir + "/biomass_whrc_" + iso3 + "*.tif"
    input_list = glob(files_tif)
    output_file = input_dir + "/biomass_whrc_gee.vrt"
    gdal.BuildVRT(output_file, input_list)

    # Resampling without compression using .vrt file
    # See: https://trac.osgeo.org/gdal/wiki/UserDocs/GdalWarp#GeoTIFFoutput-coCOMPRESSisbroken
    input_file = input_dir + "/biomass_whrc_gee.vrt"
    output_file = input_dir + "/biomass_whrc_warp.vrt"
    param = gdal.WarpOptions(options=["overwrite", "tap"],
                             format="VRT",
                             xRes=30,
                             yRes=30,
                             srcNodata=-9999,
                             dstNodata=-9999,
                             srcSRS="EPSG:4326",
                             dstSRS=proj,
                             resampleAlg=gdal.GRA_Bilinear,
                             outputType=gdal.GDT_Int16,
                             multithread=True,
                             warpMemoryLimit=500,
                             warpOptions=["NUM_THREADS=ALL_CPUS"])
    gdal.Warp(output_file, input_file, options=param)

    # Compressing
    input_file = input_dir + "/biomass_whrc_warp.vrt"
    output_file = output_dir + "/biomass_whrc.tif"
    param = gdal.TranslateOptions(options=["overwrite", "tap"],
                                  format="GTiff",
                                  creationOptions=[
                                      "TILED=YES", "BLOCKXSIZE=256",
                                      "BLOCKYSIZE=256", "COMPRESS=LZW",
                                      "PREDICTOR=2", "BIGTIFF=YES"
                                  ])
    gdal.Translate(output_file, input_file, options=param)
示例#14
0
文件: window.py 项目: WFP-VAM/modape
    def _translate(src, dst, **kwargs):

        topt = gdal.TranslateOptions(**kwargs)

        ds = gdal.Translate(dst, src, options=topt)
        assert ds
        ds = None
        return True
示例#15
0
    def close(self):
        #gdalTranslateOpts = gdal.TranslateOptions(format='VRT',
        #                                      width=outXSize, height=outYSize,
        #                                      srcWin=[0,0,outXSize*xlooks, outYSize*ylooks],
        #                                      noData=noData, resampleAlg=method)
        gdalTranslateOpts = gdal.TranslateOptions(format='VRT', unscale=True)

        gdal.Translate(self.stackName, self.vrt, options=gdalTranslateOpts)
示例#16
0
def _api_convert_cloud_optimized_geotiff2(src_fpath,
                                          dst_fpath,
                                          compress='JPEG',
                                          blocksize=256):
    """
    CommandLine:
        xdoctest -m ~/code/ndsampler/ndsampler/utils/util_gdal.py _api_convert_cloud_optimized_geotiff --bench
    """
    from osgeo import gdal
    # data_set = gdal.Open(src_fpath)
    data_set = gdal.Open(src_fpath, gdal.GA_ReadOnly)

    # TODO: optional RESAMPLING option
    # NEAREST/AVERAGE/BILINEAR/CUBIC/CUBICSPLINE/LANCZOS
    overview_resample = 'NEAREST'
    # overviewlist = [2, 4, 8, 16, 32, 64]
    overviewlist = []
    if len(overviewlist) > 0:
        # TODO: check if the overviews already exist
        # main_band = data_set.GetRasterBand(1)
        # ovr_count = main_band.GetOverviewCount()
        # if ovr_count != len(overviewlist):
        # TODO: expose overview levels as arg
        data_set.BuildOverviews(str(overview_resample), overviewlist)

    options = [
        'TILED=YES',
        'BIGTIFF=YES',
        'BLOCKXSIZE={}'.format(blocksize),
        'BLOCKYSIZE={}'.format(blocksize),
    ]
    if compress != 'RAW':
        options += ['COMPRESS={}'.format(compress)]

    if compress == 'JPEG' and data_set.RasterCount == 3:
        # Using YCBCR speeds up jpeg compression by quite a bit
        options += ['PHOTOMETRIC=YCBCR']
    if overviewlist:
        options.append('COPY_SRC_OVERVIEWS=YES')
    options = list(map(str, options))  # python2.7 support

    translate_opts = gdal.TranslateOptions(format='GTiff',
                                           creationOptions=options)
    data_set2 = gdal.Translate(dst_fpath, data_set, options=translate_opts)
    data_set2 = None  # NOQA

    # # Copy the in-memory dataset to an on-disk GeoTiff
    # driver2 = gdal.GetDriverByName(str('GTiff'))
    # data_set2 = driver2.CreateCopy(dst_fpath, data_set, options=options)
    # data_set = None

    # # OK, so setting things to None turns out to be important. Gah!
    # data_set2.FlushCache()

    # # Dereference everything
    # data_set2 = None
    # driver2 = None
    return dst_fpath
示例#17
0
def IO(input_img, out_path, xsize, ysize, save_vrt=True):
    """
    :param input_img: Geographic raster data as input
    :param out_path: Output directory for tiles images
    :param xsize: Size of X dimension
    :param ysize: Size of Y dimension
    :param save_vrt: (optional) Save output tiles into VRT
    :return:
    """

    if xsize < 1 or ysize < 1:
        raise Exception(
            print(
                "[ ERROR! ] width or height dimension should be more then 1px")
        )
    else:
        pass

    tile_size_x = xsize
    tile_size_y = ysize

    if not os.path.exists(out_path):
        os.mkdir(out_path)
    else:
        _deleteFilesIn(out_path)

    ds = gdal.Open(input_img)
    band = ds.GetRasterBand(1)
    x_size = band.XSize
    y_size = band.YSize

    # get only filename without extension
    output_filename = os.path.splitext(os.path.basename(input_img))[0]
    print(output_filename)

    count = 0
    for i in range(0, x_size, tile_size_x):
        for j in tqdm(range(0, y_size, tile_size_y), leave=False):
            count += 1
            translate_options = gdal.TranslateOptions(
                bandList=[1, 2, 3],
                noData="none",
                srcWin=[i, j, tile_size_x, tile_size_y])
            filename = os.path.join(
                out_path,
                str(output_filename) + "_" + str(count) + ".tif")
            gdal.Translate(filename, ds, options=translate_options)
            sleep(0.005)

    print(f'{count} tiles saved in {out_path}')

    if save_vrt:
        # Get the list of all files in directory tree at given path
        listOfFiles = _tiles_list(out_path)
        vrt_output = os.path.join(out_path,
                                  str(output_filename) + "_tiles_mosaic.vrt")
        vrt_opt = gdal.BuildVRTOptions(VRTNodata='none', srcNodata="NaN")
        gdal.BuildVRT(vrt_output, listOfFiles, options=vrt_opt)
示例#18
0
def convert_file(band, des_name):
    ''' convert file by band
	'''
    translate_options = gdal.TranslateOptions(
        format=TRANSLATE_OPTIONS_ARGV['format'], )

    return gdal.Translate(destName=des_name,
                          srcDS=band,
                          options=translate_options)
示例#19
0
 def vrt_to_geotiff(self, vrt_path, img_path):
     if not img_path.exists() or self.overwrite_products:
         ds = gdal.Open(str(vrt_path))
         creation_options = ['TILED=YES', 'COPY_SRC_OVERVIEWS=YES', 'BLOCKXSIZE=512', 'BLOCKYSIZE=512',
                             'NUM_THREADS=ALL_CPUS', 'BIGTIFF=IF_SAFER']
         config_options = ['GDAL_CACHEMAX=8192']
         translate_options = gdal.TranslateOptions(creationOptions=creation_options)
         ds = gdal.Translate(destName=str(img_path), srcDS=ds, options=translate_options,
                             config_options=config_options)
         ds = None
示例#20
0
    def _extract_band(self, rasterFileName, band):
        sourceDir = os.path.join(os.path.dirname(self.stackName),
                                 "source_bands")
        if not os.path.exists(sourceDir):
            os.makedirs(sourceDir)

        bndVRT = os.path.join(sourceDir, os.path.basename(rasterFileName))
        gdalTranslateOpts = gdal.TranslateOptions(format='VRT',
                                                  bandList=[band])
        gdal.Translate(bndVRT, rasterFileName, options=gdalTranslateOpts)
        return os.path.abspath(bndVRT)
示例#21
0
def test_gdal_translate_lib_resampling_methods(resampleAlg, resampleAlgStr):

    option_list = gdal.TranslateOptions(resampleAlg=resampleAlg,
                                        options='__RETURN_OPTION_LIST__')
    assert option_list == ['-r', resampleAlgStr]
    assert gdal.Translate('',
                          '../gcore/data/byte.tif',
                          format='MEM',
                          width=2,
                          height=2,
                          resampleAlg=resampleAlg) is not None
 def run(self, input_ds, filetype, compress=None):
     self.locs['converted'].default_ext = filetype
     coptions = []
     if compress:
         coptions.append('COMPRESS=' + compress)
     convert_translate_options = gdal.TranslateOptions(
         format=input_ds.filetypes[filetype], creationOptions=coptions)
     gdal.Translate(self.locs['converted'].path,
                    input_ds.dataset,
                    options=convert_translate_options,
                    format=input_ds.filetypes[filetype])
示例#23
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
示例#24
0
def match_pixel_size(rasters,
                     dst_dir=None,
                     sfx=None,
                     resampleAlg='cubic',
                     in_mem=False):

    rasters = [Path(r) for r in rasters]
    rasters_res = {}
    for r in rasters:
        src = gdal.Open(str(r))
        gt = src.GetGeoTransform()
        rasters_res[r] = (gt[1], gt[5])
        src1 = None

    max_x_raster = max(rasters_res.keys(),
                       key=lambda k: abs(rasters_res[k][0]))
    max_y_raster = max(rasters_res.keys(),
                       key=lambda k: abs(rasters_res[k][1]))
    outputs = [max_x_raster]
    if max_x_raster != max_y_raster:
        logger.error(
            'Could not locate a raster with both maximum x-resolution and y-resolution.'
        )
        print(rasters_res)
    else:
        logger.info(
            'Maximum pixel size raster located: {}'.format(max_x_raster))
        rasters.remove(max_x_raster)
        max_x = rasters_res[max_x_raster][0]
        max_y = rasters_res[max_y_raster][1]
        logger.info('({}, {})'.format(max_x, max_y))
        for r in rasters:
            if in_mem == True:
                dst_dir = Path(r'/vsimem/')
            if not dst_dir:
                dst_dir = r.parent
            if not sfx:
                sfx = 'match_px_sz'
            dst = dst_dir / '{}_{}{}'.format(r.stem, sfx, r.suffix)
            if in_mem:
                dst = dst.as_posix()
            logger.info('Translating: {}'.format(r))
            logger.info('Destination: {}'.format(dst))
            trans_opts = gdal.TranslateOptions(xRes=max_x,
                                               yRes=max_y,
                                               resampleAlg=resampleAlg)
            output = gdal.Translate(destName=str(dst),
                                    srcDS=str(r),
                                    options=trans_opts)
            outputs.append(dst)

    return outputs
示例#25
0
def reproyectaJPG(x, y, offset, archivo):

    coorVentana = coorVentanaSentinelHub(x, y, offset)

    ds = gdal.Open(archivo)

    gdal.Translate('tmp.tif',
                   ds,
                   options=gdal.TranslateOptions(outputBounds=coorVentana,
                                                 outputSRS='EPSG:3857'))
    ds = gdal.Open('tmp.tif')
    gdal.Warp('tmp_4326.tif', ds, options=gdal.WarpOptions(dstSRS='EPSG:4326'))
    ds = None
示例#26
0
def translate(raster_in,
              raster_out,
              format_out='GTiff',
              tgt_res=None,
              interp_method='bilinear'):

    ds = gdal.Open(raster_in)
    opts = gdal.TranslateOptions(format=format_out,
                                 xRes=tgt_res[0],
                                 yRes=tgt_res[1],
                                 resampleAlg=interp_method)
    gdal.Translate(raster_out, ds, options=opts)
    ds = None
示例#27
0
def apply_trans(dem, trans_vec, out_path):
    """
    Apply a translation vector of format (dx, dy, dz) to DEM and save to out_path.
    Uses GDAL Translate which requires a the new window (ulx, uly, lrx, lry)

    Parameters
    ----------
    dem : os.path.abspath
        Path to the DEM to be translated.
    trans_vec : list/tuple
        Translation vector of format (dx, dy, dz).
    out_path : os.path.abspath
        Path to write the translated DEM to.

    Returns
    -------
    None.

    """
    # Get translation values
    dx, dy, dz = trans_vec
    
    # Open source DEM
    logger.info('Reading source DEM for translation:\n{}'.format(dem))
    src = Raster(dem)
    
    # Compute new/translated bounds
    tulx = src.x_origin + dx
    tuly = src.y_origin + dy
    tlrx = tulx + src.x_sz * src.pixel_width
    tlry = tuly + src.y_sz * src.pixel_height
    trans_bounds = [tulx, tuly, tlrx, tlry]
    logger.debug('Translated bounds (ulx, uly, lrx, lry): {}'
                 .format(trans_bounds))
    
    # Translate in x-y
    logger.info('Applying translation in x-y...')
    temp_trans = r'/vsimem/temp_trans.vrt'
    trans_options = gdal.TranslateOptions(outputBounds=trans_bounds)
    gdal.Translate(out_path, dem, options=trans_options)
    logger.debug('DEM translated. Output: {}'.format(temp_trans))
    
    # Shift in z
    logger.debug('Applying translation in z...')
    src_arr = src.MaskedArray
    dst_arr = src_arr - dz
    logger.debug('Writing translated raster to: {}'.format(out_path))
    src.WriteArray(dst_arr, out_path)
    
    src = None
    logger.debug('Translation complete.')
示例#28
0
def VNP_tif(inpath, outpath, layer1, layer2, layer3, layer4):
    if not os.path.exists(os.path.join(outpath, 'light')):
        os.makedirs(os.path.join(outpath, 'light'))
    if not os.path.exists(os.path.join(outpath, 'cloud')):
        os.makedirs(os.path.join(outpath, 'cloud'))
    if not os.path.exists(os.path.join(outpath, 'angle')):
        os.makedirs(os.path.join(outpath, 'angle'))
    if not os.path.exists(os.path.join(outpath, 'time')):
        os.makedirs(os.path.join(outpath, 'time'))

    f = os.listdir(inpath)
    for i in range(len(f)):
        if f[i].endswith('.h5'):
            infile = inpath + '//' + f[i]
            root_ds = gdal.Open(infile)
            ds_list = root_ds.GetSubDatasets()
            rlayer = gdal.Open(ds_list[layer1][0], gdal.GA_ReadOnly)
            alayer = gdal.Open(ds_list[layer2][0], gdal.GA_ReadOnly)
            clayer = gdal.Open(ds_list[layer3][0], gdal.GA_ReadOnly)
            tlayer = gdal.Open(ds_list[layer4][0], gdal.GA_ReadOnly)

            # collect bounding box coordinates
            # -a_ullr <ulx> <uly> <lrx> <lry>
            #a = rlayer.GetMetadata_Dict()
            WestBoundCoord = rlayer.GetMetadata_Dict(
            )["HDFEOS_GRIDS_VNP_Grid_DNB_WestBoundingCoord"]
            EastBoundCoord = rlayer.GetMetadata_Dict(
            )["HDFEOS_GRIDS_VNP_Grid_DNB_EastBoundingCoord"]
            NorthBoundCoord = rlayer.GetMetadata_Dict(
            )["HDFEOS_GRIDS_VNP_Grid_DNB_NorthBoundingCoord"]
            SouthBoundCoord = rlayer.GetMetadata_Dict(
            )["HDFEOS_GRIDS_VNP_Grid_DNB_SouthBoundingCoord"]
            EPSG = "-a_srs EPSG:4326"  # WGS84

            translateOptionText = EPSG + " -a_ullr " + WestBoundCoord + " " + NorthBoundCoord + " " + EastBoundCoord + " " + SouthBoundCoord

            translateoptions = gdal.TranslateOptions(
                gdal.ParseCommandLine(translateOptionText))

            outfile = outpath + '\\light\\' + f[i].strip('.h5') + '.tif'
            gdal.Translate(outfile, clayer, options=translateoptions)

            im_datas = alayer.GetRasterBand(1).ReadAsArray()
            min = np.min(im_datas[im_datas > 0])
            if min < 9000:
                outfilea = outpath + '\\angle\\' + f[i].strip('.h5') + '.tif'
                gdal.Translate(outfilea, alayer, options=translateoptions)

                outfilet = outpath + '\\time\\' + f[i].strip('.h5') + '.tif'
                gdal.Translate(outfilet, tlayer, options=translateoptions)
示例#29
0
def recorta(extencion, fechaNom, productoNom, escaneo, ext, anio, numEvento,
            nomEvento, region, pathOutputTif):
    from osgeo import gdal

    ds = gdal.Open(anio + '_tmp.tif')
    gdal.Translate(pathOutputTif + region + '/EDL_CT_' + anio + '_' +
                   numEvento + '_' + nomEvento + '_' + productoNom +
                   '/EDL_CT_' + anio + '_' + numEvento + '_' + nomEvento +
                   '_' + productoNom + '_' + fechaNom + '_' + escaneo + '.tif',
                   ds,
                   options=gdal.TranslateOptions(projWin=[
                       extencion[0] - ext, extencion[3] + ext, extencion[2] +
                       ext, extencion[1] - ext
                   ]))
示例#30
0
    def processImages(self, layerWMS, webScaleParamList, outputFolder):
        scaleFeaturesDict = self.featuresByScale[self.scaleSelected]
        width, height, ncol, nrow = webScaleParamList

        for feat in scaleFeaturesDict.keys():
            bboxStr = scaleFeaturesDict[feat].asWktCoordinates().replace(
                ',', '').split(' ')
            bbox = []
            for i in bboxStr:
                bbox.append(float(i))
            temp_path = tempfile.TemporaryDirectory()
            complete_image = Image.new('RGB', (width * ncol, height * nrow))
            dx = (bbox[2] - bbox[0]) / ncol
            dy = (bbox[3] - bbox[1]) / nrow

            for column in range(ncol):
                xmin = bbox[0] + dx * column
                xmax = bbox[0] + dx * (column + 1)
                for line in range(nrow):
                    ymin = bbox[3] - dy * (line + 1)
                    ymax = bbox[3] - dy * line
                    imageURL = "https://bdgex.eb.mil.br/mapcache?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&BBOX={},{},{},{}&SRS=EPSG:4326&WIDTH={}&HEIGHT={}&LAYERS={}&STYLES=,,,&FORMAT=image/png&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE".format(
                        xmin, ymin, xmax, ymax, width, height, layerWMS)
                    partialImage = requests.get(imageURL, allow_redirects=True)
                    temp_file = os.path.join(temp_path.name, 'temp_image.png')
                    open(temp_file, "wb").write(partialImage.content)
                    complete_image.paste(Image.open(temp_file),
                                         (width * column, height * line))

            complete_image.save(
                os.path.join(temp_path.name, 'complete_image.png'))
            outputFile = os.path.join(outputFolder, feat + ".tif")

            p1 = gdal.GCP(bbox[0], bbox[1], 0, 0, height * nrow)
            p2 = gdal.GCP(bbox[2], bbox[1], 0, width * ncol, height * nrow)
            p3 = gdal.GCP(bbox[0], bbox[3], 0, 0, 0)
            p4 = gdal.GCP(bbox[2], bbox[3], 0, width * ncol, 0)

            opt = gdal.TranslateOptions(format='GTiff', GCPs=[p1, p2, p3, p4])
            gdal.Translate(outputFile,
                           os.path.join(temp_path.name, 'complete_image.png'),
                           options=opt)
            temp_path.cleanup()
            processing.run("gdal:assignprojection", {
                'INPUT': outputFile,
                'CRS': "EPSG:4326"
            })

        return