Exemplo n.º 1
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
Exemplo n.º 2
0
def test_gdalbuildvrt_lib_resampling_methods(resampleAlg, resampleAlgStr):

    option_list = gdal.BuildVRTOptions(resampleAlg=resampleAlg,
                                       options='__RETURN_OPTION_LIST__')
    assert option_list == ['-r', resampleAlgStr]
    assert gdal.BuildVRT('', '../gcore/data/byte.tif',
                         resampleAlg=resampleAlg) is not None
Exemplo n.º 3
0
    def add_virtual_layer_to_canvas(self, img_dir, imgs):
        """create and add virtual layer from images"""

        layer_name = os.path.basename(img_dir)

        # create output file path from parent directory name
        # if file exists, add incremental number to the filename (filename-1)
        layer_name_original = layer_name
        i = 0
        while True:
            output_file_name = f'{layer_name}.vrt'
            output_file_path = os.path.join(img_dir, output_file_name)
            if not os.path.exists(output_file_path):
                break
            i += 1
            layer_name = f'{layer_name_original}-{i}'

        # create vrt file
        gdal_options = gdal.BuildVRTOptions(
            resolution='highest',
            resampleAlg=gdal.GRA_Lanczos,
        )
        gdal.BuildVRT(output_file_path, imgs, options=gdal_options)

        # add vrt file to canvas
        if os.path.exists(output_file_path):
            self.iface.addRasterLayer(output_file_path, layer_name)
        else:
            print('Error on gdalbuildvrt')
def make_vrt(base_raster_pattern, target_nodata, target_raster_path,
             target_dem_vrt_token_path):
    """Make a VRT given a list of files.

    Parameters:
        base_raster_pattern (str): pattern of rasters to build to vrt.
        target_nodata (numeric): desired nodata.
        target_raster_path (str): path to desired target vrt
        target_dem_vrt_token_path (str): path to a file to write when
            complete.

    Returns:
        None.

    """
    base_raster_path_list = glob.glob(base_raster_pattern)
    vrt_options = gdal.BuildVRTOptions(VRTNodata=target_nodata)
    gdal.BuildVRT(target_raster_path,
                  base_raster_path_list,
                  options=vrt_options)
    target_dem = gdal.OpenEx(target_raster_path, gdal.OF_RASTER)
    if target_dem is not None:
        with open(target_dem_vrt_token_path, 'w') as token_file:
            token_file.write(str(datetime.datetime.now()))
    else:
        raise RuntimeError(
            "didn't make VRT at %s on: %s, from: %s" %
            (target_raster_path, base_raster_path_list, base_raster_pattern))
Exemplo n.º 5
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)))
Exemplo n.º 6
0
def gdalbuildvrt(src, dst, options=None, void=True):
    """
    a simple wrapper for gdal.BuildVRT

    Parameters
    ----------
    src: str, ogr.DataSource or gdal.DataSource
        the input data set
    dst: str
        the output data set
    options: dict
        additional parameters passed to gdal.BuildVRT;
        see http://gdal.org/python/osgeo.gdal-module.html#BuildVRTOptions
    void: bool
        just write the results and don't return anything? If not, the spatial object is returned

    Returns
    -------

    """
    options = {} if options is None else options
    out = gdal.BuildVRT(dst, src, options=gdal.BuildVRTOptions(**options))
    if void:
        out = None
    else:
        return out
def _create_feature_vrt(filepath_vrt: str, filepath_focal_quad: str,
                        filepaths_contextual_quads: List[str],
                        buffer: int) -> None:
    # Get raster parameters for building VRT
    focal_raster = gdal.Open(filepath_focal_quad)
    cols = focal_raster.RasterXSize
    rows = focal_raster.RasterYSize
    llx, xres, _, y0, _, yres = focal_raster.GetGeoTransform()
    urx = llx + cols * xres
    y1 = y0 + rows * yres
    lly = min([y0, y1])
    ury = max([y0, y1])
    # Modify raster parameters to build in buffer
    llx -= buffer * xres
    urx += buffer * xres
    lly += buffer * yres
    ury -= buffer * yres
    # Build VRT
    focal_raster = gdal.Open(filepath_focal_quad)
    focal_srs = osr.SpatialReference(wkt=focal_raster.GetProjection())
    filepaths_quads = [filepath_focal_quad] + filepaths_contextual_quads
    options_buildvrt = gdal.BuildVRTOptions(
        bandList=[1, 2, 3],
        outputBounds=(llx, lly, urx, ury),
        outputSRS=focal_srs,
        VRTNodata=-9999,
    )
    gdal.BuildVRT(filepath_vrt, filepaths_quads, options=options_buildvrt)
Exemplo n.º 8
0
def build_gdal_vrt(vrtFileName, sourceGeoTiffFileNameList):
    """build GDAL VRT to compose terrain from multiple GeoTiff files

    Note: GDAL's earlier versions (<2.3.3) can't read BigTIFF files.

    It is an equivalent of the GDAL command: gdalbuildvrt result.vrt *.tif
    https://gdal.org/programs/gdalbuildvrt.html

    Attributes
    -------
    vrtFileName: {string} -- result VRT file name
    sourceGeoTiffFileNameList: {list} -- list of source GeoTiff file names

    Returns
    -------

    """

    from osgeo import gdal

    vrt_options = gdal.BuildVRTOptions(resampleAlg='cubic', addAlpha=True)

    #result_vrt = gdal.BuildVRT(vrtFileName, sourceGeoTiffFileNameList, options=vrt_options)
    result_vrt = gdal.BuildVRT(vrtFileName, sourceGeoTiffFileNameList)

    #write the vrt to file
    result_vrt = None
    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)
Exemplo n.º 10
0
def vrt_from_dir(input_dir, output_path="./output.vrt", **kwargs):
    """
    Build a VRT Indexing all Tiffs in a directory
    """
    inputs = glob.glob(f"{input_dir}*.tif*")
    vrt_opts = gdal.BuildVRTOptions(**kwargs)
    gdal.BuildVRT(output_path, inputs, options=vrt_opts)
Exemplo n.º 11
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
Exemplo n.º 12
0
    def gdal_dataset(self, extent=None, size=None):
        if extent is not None and size is not None:
            xmin, ymin, xmax, ymax = extent
            width, height = size
            items = (
                RasterMosaicItem.filter(
                    func.st_intersects(
                        func.st_transform(
                            func.st_makeenvelope(xmin, ymin, xmax, ymax, self.srs.id), 4326
                        ),
                        RasterMosaicItem.footprint,
                    ),
                    RasterMosaicItem.resource_id == self.id,
                )
                .order_by(RasterMosaicItem.position)
                .all()
            )

            fnames = []
            for item in items:
                fname = env.raster_mosaic.workdir_filename(item.fileobj)
                fnames.append(fname)

            if len(fnames) > 0:
                ds = gdal.BuildVRT(
                    "",
                    fnames,
                    options=gdal.BuildVRTOptions(
                        xRes=(xmax - xmin) / width,
                        yRes=(ymax - ymin) / height,
                    )
                )
                return ds
Exemplo n.º 13
0
def stack_rasters_vrt(
    rasters,
    out_path: str,
    seperate: bool = True,
    resample_alg="nearest",
    options: tuple = (),
    overwrite: bool = True,
    creation_options: Union[list, None] = None,
) -> str:
    """Stacks a list of rasters into a virtual rasters (.vrt)."""
    type_check(rasters, [list], "rasters")
    type_check(out_path, [str], "out_path")
    type_check(seperate, [bool], "seperate")
    type_check(resample_alg, [str], "resample_alg")
    type_check(options, [tuple], "options")
    type_check(overwrite, [bool], "overwrite")
    type_check(creation_options, [list], "creation_options", allow_none=True)

    resample_algorithm = translate_resample_method(resample_alg)
    options = gdal.BuildVRTOptions(resampleAlg=resample_algorithm,
                                   separate=seperate)

    gdal.BuildVRT(out_path, rasters, options=options)

    return out_path
    def _create_global_vrt(self, fn, local=True):
        """Create VRT file for file.

        By default, the .vrt-file will be written to a local temporary
        directory. If `local` is set to False, the file is written to the
        directory the input file (fn) currently lives in.

        :param fn: file name
        :param local: create temporary local vrt.
        :returns: file name of created vrt, or initial file name if no success.
        :rtype: string

        """
        # TODO should it be an option in config if vrt is created and where?
        logging.info('Creating vrt file from {}.'.format(fn))
        self._check_gdal_compliance(fn)
        try:
            temp_fn = ('{}_prior_{}_{}.vrt'.format(self.variable, self.ptype,
                                                   self.date8))
            out_fn = os.path.join(self.output_directory, temp_fn)
            vrt_options = gdal.BuildVRTOptions(outputBounds=(-180, -90, 180,
                                                             90))
            gdal.BuildVRT(out_fn, fn, options=vrt_options)
            res = '{}'.format(out_fn)
            assert os.path.isfile(res), "{} is not a file.".format(res)
            self._check_gdal_compliance(res)
            return res
        except Exception as e:
            logging.warning('Cannot create .vrt file'
                            ' {} - returning {}.'.format(res, fn))
            return '{}'.format(fn)
Exemplo n.º 15
0
def make_temp_vrt(ds, extent: GeoRectangle):
    options = gdal.BuildVRTOptions(outputBounds=(extent.min_x, extent.min_y,
                                                 extent.max_x, extent.max_y))
    vrt_filename = tempfile.mktemp(suffix='.vrt')
    vrt_ds = gdal.BuildVRT(vrt_filename, ds, options=options)
    if vrt_ds is None:
        raise Exception("Error! cannot create vrt. Cannot proceed")
    return vrt_filename, vrt_ds
Exemplo n.º 16
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)
Exemplo n.º 17
0
def combine_cog(PATH, OUTPATH, TILE, YEAR):
    logging.info("Combining GeoTIFFs")
    if int(YEAR) > 2000:
        bands = ['HH', 'HV', 'linci', 'date', 'mask']
    else:
        bands = ['HH', 'linci', 'date', 'mask']
    output_cogs = []

    gtiff_abs_path = os.path.abspath(PATH)
    outtiff_abs_path = os.path.abspath(OUTPATH)

    for band in bands:
        # Find all the files
        all_files = []
        for path, subdirs, files in os.walk(gtiff_abs_path):
            for fname in files:
                if int(YEAR) > 2010:
                    if '_{}_'.format(band) in fname and not fname.endswith('.hdr'):
                        in_filename = os.path.join(path, fname)
                        all_files.append(in_filename)
                else:
                    if '_{}'.format(band) in fname and not fname.endswith('.hdr'):
                        in_filename = os.path.join(path, fname)
                        all_files.append(in_filename)

        # Create the VRT
        logging.info("Building VRT for {} with {} files found".format(
            band, len(all_files)))
        vrt_path = os.path.join(gtiff_abs_path, '{}.vrt'.format(band))
        if int(YEAR) > 2010:
            cog_filename = os.path.join(outtiff_abs_path, '{}_{}_sl_{}_F02DAR.tif'.format(TILE, YEAR[-2:], band))
        else:
            cog_filename = os.path.join(outtiff_abs_path, '{}_{}_sl_{}.tif'.format(TILE, YEAR[-2:], band))
        vrt_options = gdal.BuildVRTOptions()
        gdal.BuildVRT(
            vrt_path,
            all_files,
            options=vrt_options
        )

        # Default to nearest resampling
        resampling = 'nearest'
        if band in ['HH', 'HV', 'linci']:
            resampling = 'average'

        cog_translate(
            vrt_path,
            cog_filename,
            cog_profile,
            config={"GDAL_TIFF_OVR_BLOCKSIZE": "512"},
            overview_level=5,
            overview_resampling=resampling
        )

        output_cogs.append(cog_filename)

    # Return the list of written files
    return output_cogs
Exemplo n.º 18
0
def raster_vrt_stitch(inrasters,
                      outraster,
                      epsg,
                      clip=None,
                      clean=False,
                      warp_options: dict = {}):
    """[summary]
    https://gdal.org/python/osgeo.gdal-module.html#BuildVRT
    Keyword arguments are :
        options --- can be be an array of strings, a string or let empty and filled from other keywords..
        resolution --- 'highest', 'lowest', 'average', 'user'.
        outputBounds --- output bounds as (minX, minY, maxX, maxY) in target SRS.
        xRes, yRes --- output resolution in target SRS.
        targetAlignedPixels --- whether to force output bounds to be multiple of output resolution.
        separate --- whether each source file goes into a separate stacked band in the VRT band.
        bandList --- array of band numbers (index start at 1).
        addAlpha --- whether to add an alpha mask band to the VRT when the source raster have none.
        resampleAlg --- resampling mode.
            near: nearest neighbour resampling (default, fastest algorithm, worst interpolation quality).
            bilinear: bilinear resampling.
            cubic: cubic resampling.
            cubicspline: cubic spline resampling.
            lanczos: Lanczos windowed sinc resampling.
            average: average resampling, computes the average of all non-NODATA contributing pixels.
            mode: mode resampling, selects the value which appears most often of all the sampled points.
            max: maximum resampling, selects the maximum value from all non-NODATA contributing pixels.
            min: minimum resampling, selects the minimum value from all non-NODATA contributing pixels.
            med: median resampling, selects the median value of all non-NODATA contributing pixels.
            q1: first quartile resampling, selects the first quartile value of all non-NODATA contributing pixels.
            q3: third quartile resampling, selects the third quartile value of all non-NODATA contributing pixels.
        outputSRS --- assigned output SRS.
        allowProjectionDifference --- whether to accept input datasets have not the same projection. Note: they will *not* be reprojected.
        srcNodata --- source nodata value(s).
        VRTNodata --- nodata values at the VRT band level.
        hideNodata --- whether to make the VRT band not report the NoData value.
        callback --- callback method.
        callback_data --- user data for callback.
    """
    log = Logger('Raster Stitch')

    # Build a virtual dataset that points to all the rasters then mosaic them together
    # clipping out the HUC boundary and reprojecting to the output spatial reference
    path_vrt = get_unique_file_path(
        os.path.dirname(outraster),
        os.path.basename(outraster).split('.')[0] + '.vrt')

    log.info('Building temporary vrt: {}'.format(path_vrt))
    vrt_options = gdal.BuildVRTOptions()
    gdal.BuildVRT(path_vrt, inrasters, options=vrt_options)

    raster_warp(path_vrt, outraster, epsg, clip, warp_options)

    if clean:
        for rpath in inrasters:
            safe_remove_file(rpath)
Exemplo n.º 19
0
def buildVRT(outFolder, inFolder=None, rasterList=None):
    vrt_options = gdal.BuildVRTOptions(resampleAlg='cubic', addAlpha=True)
    if inFolder != None:
        image_list = []
        for image in glob.glob(inFolder + '/*.tif'):
            image_list.append(image)
    if rasterList != None:
        image_list = rasterList

    saveFile = os.path.join(outFolder, 'mosaic.vrt')
    gdal.BuildVRT(saveFile, image_list, options=vrt_options)
Exemplo n.º 20
0
 def create_vrt(self, src_paths, vrt_path):
     vrt_options = gdal.BuildVRTOptions(addAlpha=False, hideNodata=False, allowProjectionDifference=False,
                                        resolution='highest')
     if not vrt_path.exists() or self.overwrite_products:
         logging.debug("Creating vrt file: " + str(vrt_path))
         if not vrt_path.parent.exists():  # Create directory if it does not exist
             os.makedirs(vrt_path.parent)
         vrt = gdal.BuildVRT(str(vrt_path), src_paths, options=vrt_options)
         vrt = None  # Required for saving the vrt (https://gis.stackexchange.com/a/314580)
     else:
         logging.debug("vrt file already exists: " + str(vrt_path))
Exemplo n.º 21
0
def create_vrt_file_if_needed(filepath):
    color_interp = get_color_interpretations(filepath)
    nodata_values = get_nodata_values(filepath)
    if color_interp[-1] == 'Alpha' and not is_normalized_alpha_needed(
            filepath, color_interp=color_interp, nodata_values=nodata_values):
        base_name = os.path.splitext(util.get_deepest_real_file(filepath))[0]
        vrt_file_path = base_name + '.vrt'
        band_list = list(range(1, len(color_interp)))
        vrt_options = gdal.BuildVRTOptions(bandList=band_list)
        gdal.BuildVRT(vrt_file_path, [filepath], options=vrt_options)
    else:
        vrt_file_path = None
    return vrt_file_path
Exemplo n.º 22
0
 def createVrt():
     options = gdal.BuildVRTOptions(
         resampleAlg=gdal.GRIORA_NearestNeighbour)
     vrt = f"{self._downloadName}_{self._zoom}.vrt"
     filepath = os.path.join(dirPath, vrt)
     _ds = gdal.BuildVRT(filepath,
                         self._currentTask.filepathImages,
                         options=options)
     _ds.FlushCache()
     _ds = None
     if hasAddTiles:
         layer = QgsRasterLayer(filepath, getName(filepath))
         self.project.addMapLayer(layer)
Exemplo n.º 23
0
def build_vrts(filenames, dss, extent: GeoRectangle, suffix):
    vrt_filenames = []
    for filename, ds in zip(filenames, dss):
        options = gdal.BuildVRTOptions(outputBounds=(extent.min_x, extent.min_y, extent.max_x, extent.max_y))
                                       # hideNodata=True,
                                       # separate=False)
        # vrt_filename = filename + suffix
        vrt_filename = tempfile.mktemp(suffix='.vrt')
        vrt_filenames.append(vrt_filename)
        vrt_ds = gdal.BuildVRT(vrt_filename, ds, options=options)
        if vrt_ds is None:
            return None
        del vrt_ds
    return vrt_filenames
 def create_vrt(self, fn, bounds=None, xy_resolution=100, src_fns=None):
     if not src_fns:
         src_fns = self.filepath.dem_source_fns
     vrt_fn = self.filepath.root_tmp_path + fn + '.vrt'
     vrt_options = gdal.BuildVRTOptions(
         resampleAlg=gdal.GRA_Average,  # 'cubic',
         xRes=xy_resolution,
         yRes=xy_resolution,
         addAlpha=True,
         outputBounds=bounds)
     test_vrt = gdal.BuildVRT(vrt_fn, src_fns, options=vrt_options)
     test_vrt = None
     setattr(self.filepath, fn + '_vrt_fn', vrt_fn)
     return
Exemplo n.º 25
0
def is_nodata_out_of_min_max(filepath, *, nodata_values):
    if to_nodata_value(nodata_values) is None:
        result = False
    else:
        base_name = os.path.splitext(util.get_deepest_real_file(filepath))[0]
        vrt_file_path = base_name + '.ignore_nodata.vrt'
        vrt_options = gdal.BuildVRTOptions(hideNodata=True)
        gdal.BuildVRT(vrt_file_path, [filepath], options=vrt_options)
        stats = get_statistics(vrt_file_path)
        result = any(nodata_val < stats[band_idx][0]  # nodata_val < min
                     or nodata_val > stats[band_idx][1]  # nodata_val > max
                     for band_idx, nodata_val in enumerate(nodata_values)
                     if nodata_val is not None)
        os.remove(vrt_file_path)
    return result
Exemplo n.º 26
0
def main(src, dst, src_nodata, dst_nodata):
    vrt_file = tempfile.mktemp(dir=os.path.dirname(dst), prefix='VRT_', suffix='.vrt')
    src_files_list = [str(file) for file in src]
    vrt_options = gdal.BuildVRTOptions(srcNodata=str(src_nodata),
                                       VRTNodata=str(dst_nodata),
                                       hideNodata=True)
    gdal.BuildVRT(vrt_file, src_files_list, options=vrt_options)
    dst_driver = gdal.GetDriverByName('GTiff')
    if os.path.exists(dst):
        dst_driver.Delete(dst)
    vrt_ds = gdal.Open(vrt_file)
    dst_ds = dst_driver.CreateCopy(dst, vrt_ds, callback=progress)
    dst_ds.FlushCache()
    dst_ds = vrt_ds = None
    os.remove(vrt_file)
    return None
Exemplo n.º 27
0
    def merge(self, out_file=None):
        """
        Create virtual merge of individual terrain tiles at given zoom, bounds

        Merging with rasterio.merge may run up on system limits, just use gdal.
        https://github.com/mapbox/rasterio/issues/1636
        https://github.com/smnorris/terraincache/issues/2
        """
        self.merged = os.path.join(self.tempfolder, "merge.vrt")
        vrt_options = gdal.BuildVRTOptions(resampleAlg=self.resampling)
        gdal.BuildVRT(self.merged, self.files, options=vrt_options)
        if out_file:
            LOG.info(f"writing {out_file}")
            cmd = ["gdal_translate", "-ot", "Int16", self.merged, out_file]
            subprocess.run(cmd)
            self.merged = out_file
Exemplo n.º 28
0
def make_ros_vrt(ros: List[RasterOverview], extent: GeoRectangle, vrt_filename: Path, fix_open_options=True):
    options = gdal.BuildVRTOptions(outputBounds=(extent.min_x, extent.min_y, extent.max_x, extent.max_y))
    # dir_name = Path(os.path.dirname(str(vrt_filename)))
    # ds_list = [os.path.relpath(ro.path, dir_name) for ro in ros]
    ds_list = [ro.get_ds() for ro in ros]
    os.makedirs(os.path.dirname(str(vrt_filename)), exist_ok=True)
    vrt_ds = gdal.BuildVRT(str(vrt_filename), ds_list, options=options)
    if vrt_ds is None:
        raise Exception("Error! cannot create vrt. Cannot proceed")
    if fix_open_options:
        vrt_ds = None
        vrt_filename_temp = vrt_filename.with_suffix('.tmp.vrt')
        shutil.move(vrt_filename, vrt_filename_temp)
        vrt_fix_openoptions(ros, filename_in=vrt_filename_temp, filename_out=vrt_filename)
        os.remove(vrt_filename_temp)
        vrt_ds = open_ds(vrt_filename)
    return vrt_ds
def MakeImageComposite(imagery_folder):
    """Returns a multiband image based on Sentinel-2 imagery folder"""
    #Browse through the S2 product folder and retrieve the required bands.
    #Change depending on your input features
    S2_bands = []
    for root, dirs, files in os.walk(imagery_folder):
        for name in files:
            if name.endswith("B02_10m.jp2"):
                Bpath = os.path.join(root, name)
                S2_bands.append(Bpath)
            elif name.endswith("B03_10m.jp2"):
                Gpath = os.path.join(root, name)
                S2_bands.append(Gpath)
            elif name.endswith("B04_10m.jp2"):
                Rpath = os.path.join(root, name)
                S2_bands.append(Rpath)
            elif name.endswith("B08_10m.jp2"):
                NIRpath = os.path.join(root, name)
                S2_bands.append(NIRpath)
            elif name.endswith("B11_20m.jp2"):
                SWIR1path = os.path.join(root, name)
                S2_bands.append(SWIR1path)
            elif name.endswith("B12_20m.jp2"):
                SWIR2path = os.path.join(root, name)
                S2_bands.append(SWIR2path)
            else:
                pass

    print('{nBands} Bands retrieved from the Sentinel-2 imagery folder'.format(
        nBands=len(S2_bands)))

    #Stack the bands into a Composite Image
    outvrt = '/vsimem/stacked.vrt'  #/vsimem is special in-memory gdal virtual "directory"

    imgComp = os.path.join(
        workspace, 'inputs',
        'S2A_' + os.path.basename(Gpath)[:15] + '_Band_Composite.tif')
    VrtOptions = gdal.BuildVRTOptions(resolution='lowest', separate=True)
    outds = gdal.BuildVRT(outvrt, S2_bands, options=VrtOptions)
    translate_options = gdal.TranslateOptions(
        format='GTiff', creationOptions=['COMPRESS=LZW', 'BIGTIFF=YES'])
    outds = gdal.Translate(imgComp, outds, options=translate_options)

    print('S2 Band Composite created!')

    return imgComp
Exemplo n.º 30
0
def make_ros_vrt_overviews(ros: List[RasterOverview], extent: GeoRectangle, vrt_filename: Path, add_overviews=True, return_ds=False):
    options = gdal.BuildVRTOptions(outputBounds=(extent.min_x, extent.min_y, extent.max_x, extent.max_y))
    vrt_ds = gdal.BuildVRT(str(vrt_filename), ros[0].get_ds(), options=options)
    if vrt_ds is None:
        raise Exception("Error! cannot create vrt. Cannot proceed")
    if add_overviews:
        vrt_ds = None
        vrt_filename_temp = vrt_filename.with_suffix('.tmp.vrt')
        shutil.move(vrt_filename, vrt_filename_temp)
        vrt_add_overviews(ros[1:], filename_in=vrt_filename_temp, filename_out=vrt_filename)
        os.remove(vrt_filename_temp)
        if return_ds:
            vrt_ds = open_ds(vrt_filename)
    if return_ds:
        return vrt_ds
    else:
        return vrt_filename