Esempio n. 1
0
def get_image(layer, pos, forest, name, cachedir):
    vector = get_geojson(pos, cachedir)
    if layer == "base":
        ca_search = forest.search(geography='California',
                                  metric='CanopyBaseHeight',
                                  year=default_options['year'],
                                  resolution=10)
        options = gdal.WarpOptions(
            creationOptions=[
                "COMPRESS=DEFLATE", "TILED=YES", "BIGTIFF=YES",
                "NUM_THREADS=ALL_CPUS"
            ],
            cutlineDSName=vector,
            cropToCutline=True,
            resampleAlg=gdal.GRA_Average,
            dstSRS="EPSG:3857",
            #             dstSRS = "EPSG:4326",
            width=1080,
            height=1080)
    elif layer == "cover":
        ca_search = forest.search(geography='California',
                                  metric='CanopyCover',
                                  year=default_options['year'],
                                  resolution=3)
        options = gdal.WarpOptions(
            creationOptions=[
                "COMPRESS=DEFLATE", "TILED=YES", "BIGTIFF=YES",
                "NUM_THREADS=ALL_CPUS"
            ],
            cutlineDSName=vector,
            cropToCutline=True,
            resampleAlg=gdal.GRA_Average,
            dstSRS="EPSG:3857",
            #             dstSRS = "EPSG:4326",
            width=3240,
            height=3240)
    elif layer == "height":
        ca_search = forest.search(geography='California',
                                  metric='CanopyHeight',
                                  year=default_options['year'],
                                  resolution=3)
        options = gdal.WarpOptions(
            creationOptions=[
                "COMPRESS=DEFLATE", "TILED=YES", "BIGTIFF=YES",
                "NUM_THREADS=ALL_CPUS"
            ],
            cutlineDSName=vector,
            cropToCutline=True,
            resampleAlg=gdal.GRA_Average,
            dstSRS="EPSG:3857",
            #             dstSRS = "EPSG:4326",
            width=3240,
            height=3240)

    cs_vegetation = ca_search[0]
    input_file = forest.fetch(cs_vegetation, gdal=True)
    output_file = f"/usr/local/share/gridlabd/geodata/vegetation/2020/{name}.tif"

    warp = gdal.Warp(output_file, input_file, options=options)
    warp.FlushCache()
Esempio n. 2
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
Esempio n. 3
0
def shp_to_raster(shp, attr, out_path, ds_eg, tem_path, **kwargs):
    # create out put name
    out_file = os.path.join(out_path, os.path.splitext(
        os.path.basename(shp))[0] + '.tif')
    if os.path.exists(out_file):
        return
    tem_file = os.path.join(tem_path, os.path.splitext(
        os.path.basename(shp))[0] + '.tif')

    # extent warp options
    ds_ex = gdal.Translate('/vsimem/_extent.tif', ds_eg, bandList=[1])
    t = ds_eg.GetGeoTransform()
    temp_option = gdal.WarpOptions(multithread=True, options=CONFIG,
                                   creationOptions=CREATION, **kwargs,
                                   xRes=t[1] / 10, yRes=t[5] / 10,
                                   outputType=gdal.GDT_Float64)

    ds_tem = gdal.Warp(tem_file, ds_ex, options=temp_option)
    band = ds_tem.GetRasterBand(1)
    option = gdal.WarpOptions(multithread=True, options=CONFIG,
                              creationOptions=CREATION,  **kwargs,
                              xRes=t[1], yRes=t[5], resampleAlg=gdal.GRA_Average,
                              outputType=gdal.GDT_Float64)

    driver = ogr.GetDriverByName('ESRI Shapefile')
    shp_factor = driver.Open(shp)
    layer = shp_factor.GetLayer()

    # create and use RasterizeLayer
    band.Fill(band.GetNoDataValue())
    gdal.RasterizeLayer(ds_tem, [1], layer,
                        options=["ATTRIBUTE=%s" % attr, 'ALL_TOUCHED=TRUE'])
    ds_out = gdal.Warp(out_file, ds_tem, options=option)

    # deal with units
    if os.path.splitext(os.path.basename(out_file))[0] == 'permeability':
        band_out = ds_out.GetRasterBand(1)
        no_data = band_out.GetNoDataValue()
        values = ds_out.ReadAsArray()
        values[values != no_data] = values[values != no_data] / 100
        band_out.WriteArray(values)

        band = ds_tem.GetRasterBand(1)
        no_data = band.GetNoDataValue()
        values = ds_tem.ReadAsArray()
        values[values != no_data] = values[values != no_data] / 100
        band.WriteArray(values)

    band_out = None
    ds_out = None
    band = None
    ds_tem = None
    shp_factor = None
    layer = None
Esempio n. 4
0
    def rasterProject(in_file,
                      out_file,
                      out_proj,
                      resampling=1,
                      x_res=None,
                      y_res=None):
        """
        栅格文件投影转换

        :param in_file: 输入文件路径 str
        :param out_file: 输出文件路径 str
        :param out_proj: 输出文件投影信息 可以是以下几种情况
                            (1)输入.prj文件
                            (2)输入其他gdal可读取栅格文件
                            (3)输入WKT空间参照系统字符串
        :param resampling: 重采样方式 默认值为 1 最邻近 以下三种可选
                            (1)1 最邻近 gdal.GRA_NearestNeighbour
                            (2)2 双线性内插 gdal.GRA_Bilinear
                            (3)3 三次卷积 gdal.GRA_Cubic
        :param x_res: x向分辨率
        :param y_res: y向分辨率
        :return:
        """
        in_ds = gdal.Open(in_file, gdal.GA_ReadOnly)
        src_srs = osr.SpatialReference()
        src_srs.ImportFromWkt(in_ds.GetProjection())

        if os.path.isfile(out_proj) and (not out_proj.endswith('.prj')):
            tmp_ds = gdal.Open(out_proj, gdal.GA_ReadOnly)
            dst_srs = osr.SpatialReference()
            dst_srs.ImportFromWkt(tmp_ds.GetProjection())
        else:
            dst_srs = out_proj

        resampleAlg = ConstParam.RESAMPLING_MODEL[resampling]
        if x_res and y_res:
            options = gdal.WarpOptions(format='GTiff',
                                       srcSRS=src_srs,
                                       dstSRS=dst_srs,
                                       xRes=x_res,
                                       yRes=y_res,
                                       resampleAlg=resampleAlg)
        else:
            options = gdal.WarpOptions(format='GTiff',
                                       srcSRS=src_srs,
                                       dstSRS=dst_srs,
                                       resampleAlg=resampleAlg)
        gdal.Warp(out_file, in_file, options=options)
Esempio n. 5
0
def main(src_dir, dst_dir, dst_srs=None):
    # %%
    tif_files = list(src_dir.glob("*.tif"))
    tif_files
    # %%
    # tile_ids = [x.stem.split("-")[-2:] for x in tif_files]
    # tile_ids = ["-".join(x) for x in tile_ids]
    # print(tile_ids)
    unique_ids = [x.stem.split("-")[:-2] for x in tif_files]
    unique_ids = ["-".join(x) for x in unique_ids]
    unique_ids = set(unique_ids)
    # unique_ids

    # %%
    for uid in unique_ids:
        files_to_merge = list(src_dir.glob(f"{uid}*.tif"))
        dst_tif_path = dst_dir / f"{uid}.tif"
        print(f"mosaicking image {dst_tif_path}...", end="")
        my_ras = gdal.Warp(
            str(dst_tif_path),
            [str(x) for x in files_to_merge],
            options=gdal.WarpOptions(dstSRS=dst_srs, creationOptions=GDAL_CO),
        )
        my_ras = None
        print(" done.")
Esempio n. 6
0
        def create_geotiff(path,
                           array,
                           geo_transform=affine_transform,
                           projection=cog_proj,
                           dataType=gdal.GDT_UInt16,
                           driver=gdal.GetDriverByName("GTiff")):
            ds = driver.Create(str(path), array.shape[1], array.shape[0],
                               array.shape[2], dataType,
                               ['COMPRESS=DEFLATE', 'BIGTIFF=YES'])
            ds.SetGeoTransform(geo_transform)
            ds.SetProjection(projection.ExportToWkt())
            for i in range(1, array.shape[2]):
                ds.GetRasterBand(i).WriteArray(array[::, ::, i])
            ds.FlushCache()

            cog_path = str(Path(path.parent, f'cog_{path.name}'))

            warp_opts = gdal.WarpOptions(callback=warp_callback,
                                         warpOptions=["NUM_THREADS=ALL_CPUS"],
                                         creationOptions=[
                                             "NUM_THREADS=ALL_CPUS",
                                             "COMPRESS=DEFLATE", "BIGTIFF=YES",
                                             "TILED=YES"
                                         ],
                                         multithread=True,
                                         warpMemoryLimit=warpMemoryLimit,
                                         format=args.output_format)
            logger.info(f'Converting {str(path)} to {cog_path}...')
            # NOTE:
            # We can't directly write TIFFs on S3 as the result of the gdal.Warp operation
            # see: https://github.com/OSGeo/gdal/issues/1189
            with timing("GDAL Warp"):
                gdal.Warp(str(cog_path), str(path), options=warp_opts)

            return cog_path
Esempio n. 7
0
def clip_dataset_list_groupby_time(grid_list, time):
    '''输入相同时间的一组grid_list,对其进行拼接剪切
    '''
    output_path = os.path.join(conf.sRslPath, conf.ID + time + '_1.tif')
    options = gdal.WarpOptions(format='GTiff',
                               cutlineDSName=conf.jsonpath,
                               dstSRS='EPSG:900913')
    tif_dataset = gdal.Warp(output_path, grid_list, options=options)
    mean = NdviCompute.ndvi_compute_byds(
        tif_dataset,
        os.path.join(conf.sRslPath,
                     conf.ID + time + '_2' + conf.output_format),
        NdviCompute.IMAGE_TYPE_GF1)
    if conf.output_format == '.png':
        iRowRange = tif_dataset.RasterYSize
        iColumnRange = tif_dataset.RasterXSize
        r_band = tif_dataset.GetRasterBand(3).ReadAsArray(
            0, 0, iColumnRange, iRowRange)
        g_band = tif_dataset.GetRasterBand(2).ReadAsArray(
            0, 0, iColumnRange, iRowRange)
        b_band = tif_dataset.GetRasterBand(1).ReadAsArray(
            0, 0, iColumnRange, iRowRange)
        res = np.dstack((r_band, g_band, b_band)) * 1.0
        res = (res - np.min(res)) * 255 / np.max(res)
        im = Image.fromarray(np.uint8(res))
        im.save(output_path[:-5] + "1.png")
        del im, tif_dataset
        os.remove(output_path)

    conf.export_excel.loc[len(conf.export_excel)] = [conf.ID, mean]
    conf.export_excel.to_excel(
        os.path.join(conf.sRslPath, conf.ID + time + ".xlsx"))
Esempio n. 8
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
Esempio n. 9
0
def project_raster(ds, out_path, **kwargs):
    ds, ras = ds_name(ds)
    out_file = context_file(ras, out_path)

    if os.path.exists(out_file):
        return out_file

    # input SpatialReference
    in_srs = kwargs.pop('srcSRS', None)
    inSpatialRef = read_srs([ds, in_srs])

    # output SpatialReference
    out_srs = kwargs.pop('dstSRS', "+proj=longlat +datum=WGS84 +ellps=WGS84")
    outSpatialRef = read_srs(out_srs)

    resample_alg = kwargs.pop('resampleAlg', gdal.GRA_Average)
    option = gdal.WarpOptions(creationOptions=CREATION,
                              resampleAlg=resample_alg,
                              srcSRS=inSpatialRef,
                              dstSRS=outSpatialRef,
                              multithread=True,
                              **kwargs)
    gdal.Warp(out_file, ds, options=option)

    return out_file
Esempio n. 10
0
def convert_uint8(ds, in_no_data=None, out_no_data=255):
    ds, ras = ds_name(ds)
    frist_band = ds.GetRasterBand(1)

    if (frist_band.DataType != gdal.GDT_Byte
            or frist_band.ReadAsArray(0, 0, 1, 1).dtype != np.int8):
        return ras

    if frist_band.GetNoDataValue() is not None:
        in_no_data = frist_band.GetNoDataValue()
    if in_no_data is None:
        raise (ValueError("in_no_data must be initialed"))

    option = gdal.WarpOptions(multithread=True,
                              creationOptions=CREATION,
                              outputType=gdal.GDT_Byte)
    out_file = rep_file(os.path.dirname(ras), ras)
    ds_out = gdal.Warp(out_file, ras, options=option)

    for i in range(1, 1 + ds.RasterCount):
        band = ds.GetRasterBand(i)
        band_out = ds_out.GetRasterBand(i)
        if band.GetNoDataValue() is None:
            band.SetNoDataValue(in_no_data)
        band_out.SetNoDataValue(out_no_data)
        block_write(ds, band, band_out, map_no_data)

    ds = None
    ds_out = None
    gdal.GetDriverByName('GTiff').Delete(ras)
    os.rename(out_file, ras)

    return ras
Esempio n. 11
0
def transformMask(maskDS, maskValue, t_srs, te, tr, outName):
    '''
        Use gdalwarp to transform the mask data set to the projection and 
         spatial extent of the reference data set.
    '''
    # Format option string
    optionStr = '-t_srs {} -te {} {} {} {} -tr {} {} -r near'.\
        format(t_srs,*te,*tr)

    print('*' * 9)
    print('Warping:')
    print(optionStr)

    # Apply transformation
    maskDS = gdal.Warp(outName,
                       maskDS,
                       options=gdal.WarpOptions(format='MEM',
                                                dstSRS=t_srs,
                                                outputBounds=te,
                                                xRes=tr[0],
                                                yRes=tr[1],
                                                resampleAlg='near'))

    # Overwrite data set if mask value is not zero
    if maskValue != 0:
        reformatMask(outName, maskDS, maskValue)

        print('Mask value switched.')
        maskDS = loadDS(outName)

    print('Transformed mask data set saved to: {:s}'.format(outName))

    return maskDS
Esempio n. 12
0
 def run(self, unique_ds, fine_ds):
     res_warp_options = gdal.WarpOptions(
         xRes=fine_ds.res.x,
         yRes=fine_ds.res.y,
         outputBounds=fine_ds.rev_warp_output_bounds,
         resampleAlg='average')
     gdal.Warp(path, unique_ds.dataset, options=res_warp_options)
Esempio n. 13
0
    def reproject_image(self, src, dst, crs='EPSG:32632'):
        src_tmp = None
        if src == dst:  # If you want to replace the src file with the output of this function
            src_tmp = Path(src.parent,
                           'temp' + src.suffix)  # Create path for temp file
            src.rename(src_tmp)  # Rename the file to the temp filename
            src = src_tmp  # Set the temp file to be the source file
        src_ds = gdal.Open(
            str(src)
        )  # src is a Path object but gdal requires a string, therefore str(src)
        creation_options = ['NUM_THREADS=ALL_CPUS']
        config_options = ['GDAL_CACHEMAX=8192']
        warp_options = gdal.WarpOptions(dstSRS=crs,
                                        resampleAlg='cubic',
                                        srcNodata="0 0 0",
                                        multithread=True,
                                        creationOptions=creation_options)
        dst_ds = gdal.Warp(str(dst),
                           src_ds,
                           options=warp_options,
                           config_options=config_options)
        dst_ds = None
        src_ds = None

        if src_tmp is not None:
            os.remove(src_tmp)
Esempio n. 14
0
    def render_image(self, extent, size):
        ds = gdal.Warp(
            "",
            self.parent.gdal_dataset(),
            options=gdal.WarpOptions(
                width=size[0],
                height=size[1],
                outputBounds=extent,
                format="MEM",
                warpOptions=['UNIFIED_SRC_NODATA=ON'],
                dstAlpha=True,
            ),
        )

        result = PIL.Image.new("RGBA", size, (0, 0, 0, 0))

        band_count = ds.RasterCount
        array = numpy.zeros((size[1], size[0], band_count), numpy.uint8)

        for i in range(band_count):
            array[:, :,
                  i] = gdal_array.BandReadAsArray(ds.GetRasterBand(i + 1), )

        ds = None
        wnd = PIL.Image.fromarray(array)
        result.paste(wnd)

        return result
Esempio n. 15
0
def gdal_warp(path_after_warp, path_before_warp, outputBounds):
    """
    Warp (reproject) raster to the selected outputBounds
    
    Parameters
    ----------
    path_after_warp: str
        Path of the raster after warp (save path).
    path_before_warp: str
        Path of the raster before warp.
    outputBounds: tuple of float
        (x_min, y_min, x_max, y_max)

    Examples
    --------
    >>> 

    Returns
    -------
    none
    """
    # srs = osr.SpatialReference(wkt=raster_original.GetProjection())
    options = gdal.WarpOptions(
        format="GTiff",  # format="VRT",
        outputBounds=outputBounds,
        # srcSRS = "EPSG:4326", dstSRS = "EPSG:4326",
        outputType=gdal.GDT_Float32,
        resampleAlg='near')
    ds = gdal.Warp(path_after_warp, path_before_warp, options=options)
    del ds
Esempio n. 16
0
def createwarpedmosaic(nativemosaic,
                       warpedmosaic,
                       bbox,
                       incrs='EPSG:28355',
                       outcrs='EPSG:4236',
                       resample='lanczos'):
    """
    input is the native-CRS VRT made with buildvrt
    output is a warped or geotiff, determined by the extension provided on the
    output file name.
    """

    print("writing warped and clipped mosaic to be used for tiling")
    bbox = np.around(bbox, decimals=1)

    if 's3://' in nativemosaic:
        nativemosaic = nativemosaic.replace('s3://', '/vsis3/')

    if 's3://' in warpedmosaic:
        warpedmosaic = warpedmosaic.replace('s3://', '/vsis3/')

    print(nativemosaic)
    print(warpedmosaic)

    warpoptions = gdal.WarpOptions(resampleAlg=resample,
                                   outputBounds=bbox,
                                   srcSRS=incrs,
                                   dstSRS=outcrs,
                                   dstAlpha=True)

    warpedfile = gdal.Warp(warpedmosaic, nativemosaic, options=warpoptions)
    warpedfile.FlushCache
    warpedfile = None

    return (warpedmosaic)
Esempio n. 17
0
def reproyecta(nc, variable, anio):
    from osgeo import gdal

    ds = gdal.Open('NETCDF:' + nc + ':' + variable)
    gdal.Warp(anio + '_tmp.tif',
              ds,
              options=gdal.WarpOptions(dstSRS='EPSG:4326'))
Esempio n. 18
0
def resampling_cubic_spline(input, output, size):

    # Création des répertoire de sortie
    head_output = os.path.dirname(output)
    if not os.path.exists(head_output):
        os.makedirs(head_output)

    # ouverture des images et extraction des dimensions
    print('Ouverture {}'.format(input))
    dataset = gdal.Open(input, gdal.GA_ReadOnly)
    largeur, hauteur = (dataset.RasterXSize, dataset.RasterYSize)
    proj = dataset.GetProjection()

    # Resampling
    print('Resampling...')
    warp_object = gdal.WarpOptions(width=largeur / size,
                                   height=hauteur / size,
                                   resampleAlg=3,
                                   srcSRS=proj,
                                   dstSRS=proj)
    gdal.Warp(destNameOrDestDS=output,
              srcDSOrSrcDSTab=input,
              options=warp_object)
    print('Terminé')
    print()
Esempio n. 19
0
    def run(self, coarse_mask_ds, fine_ds):
        # Projection
        input_srs = osr.SpatialReference(wkt=coarse_mask_ds.projection)

        # Shapefile for output (in same projection)
        shp_path = TempLocator('coarse_mask_outline', **self.cfg)
        shp_driver = ogr.GetDriverByName("ESRI Shapefile")
        outline_ds = shp_driver.CreateDataSource(shp_path.shp)
        layer = 'mask_outline'
        outline_layer = outline_ds.CreateLayer(layer, srs=input_srs)
        outline_field = ogr.FieldDefn('MO', ogr.OFTInteger)
        outline_layer.CreateField(outline_field)

        # Get Band
        mask_band = coarse_mask_ds.dataset.GetRasterBand(1)

        # Polygonize
        gdal.Polygonize(mask_band, mask_band, outline_layer, -9999)
        outline_ds.SyncToDisk()
        outline_ds = None

        warp_options = gdal.WarpOptions(format='GTiff',
                                        cutlineDSName=shp_path.shp,
                                        dstNodata=-9999)
        gdal.Warp(self.path.gtif, fine_ds.dataset, options=warp_options)
Esempio n. 20
0
    def clip_bands(self, bands_to_clip, ref_band, temp_dir):

        opt = gdal.WarpOptions(cutlineDSName=self.shape_file,
                               cropToCutline=True,
                               srcNodata=-9999,
                               dstNodata=-9999,
                               outputType=gdal.GDT_Float32)

        for band in bands_to_clip:
            if band not in self._clipped_gdal_bands and band in self.gdal_bands:

                dest_name = (temp_dir / (band + '.tif')).as_posix()
                self._clipped_gdal_bands.update({
                    band:
                    gdal.Warp(destNameOrDestDS=dest_name,
                              srcDSOrSrcDSTab=self.gdal_bands[band],
                              options=opt)
                })
                self.gdal_bands[band] = self._clipped_gdal_bands[band]
                self.gdal_bands[band].FlushCache()

        self._ref_band_name = ref_band
        self._ref_band = self.gdal_bands[ref_band]
        self.temp_dir = temp_dir

        return
def resample(output_res):
    '''Resample the rasters to a 10 by 10 grid 

    Parameters
    ----------
    output_res : int
        output resolution to resample to, 10000 for 10 km 
    path : int
        the path identifior of the image you want to obtain information about 
    row : int
        the row identifior of the image you want to obtain information about
    '''

    files_available = [
        name for name in os.listdir('outputs/')
        if os.path.isfile(os.path.join('outputs/', name))
        and os.path.join('outputs/', name).endswith('.tif')
    ]

    for file_name in files_available:
        print(file_name)

        out_path = 'outputs/resample_' + str(file_name)

        xres = output_res
        yres = output_res
        resample_algorithm = 'cubicspline'
        options = gdal.WarpOptions(xRes=xres,
                                   yRes=yres,
                                   resampleAlg=resample_algorithm)
        data_resampler = gdal.Warp(out_path,
                                   os.path.join('outputs/', file_name),
                                   options=options)
        data_resampler = None
Esempio n. 22
0
    def open_gdal_masks(self, shape_file, temp_dir):

        gdal_masks = {}

        for mask_key, mask_name in self.TheiaMaskDict.items():
            mask_file = [file for file in self.masks_folder.glob(mask_name)][0]
            gdal_masks.update({mask_key: gdal.Open(mask_file.as_posix())})

        if shape_file:

            opt = gdal.WarpOptions(cutlineDSName=shape_file,
                                   cropToCutline=True,
                                   srcNodata=-9999,
                                   dstNodata=-9999,
                                   outputType=gdal.GDT_Int16)

            for mask_key, mask_ds in gdal_masks.items():
                dest_name = (temp_dir / mask_key).as_posix()
                clipped_mask_ds = gdal.Warp(destNameOrDestDS=dest_name,
                                            srcDSOrSrcDSTab=mask_ds,
                                            options=opt)
                clipped_mask_ds.FlushCache()
                gdal_masks.update({mask_key: clipped_mask_ds})

        return gdal_masks
Esempio n. 23
0
def clip(rasters: List[gdal.Dataset], aoi_path: str, pixel_size: int,
         dest: str):
    '''
  Clip raster datasets by AOI bounds

  Parameters:
    rasters: raster dataset(s)
    aoi_path: path to AOI shapefile
    pixel_size: size of 1 pixel in KM
    dest: output path
  '''
    if len(rasters) > 1:
        logging.info(f'merging and clipping boundary with {aoi_path}')
    else:
        logging.info(f'clipping boundary with {aoi_path}')

    driver = ogr.GetDriverByName('ESRI Shapefile')
    bnd = driver.Open(aoi_path)
    extent = round_extent(bnd, pixel_size)

    opt = gdal.WarpOptions(outputBounds=extent,
                           targetAlignedPixels=False,
                           cutlineDSName=aoi_path,
                           cutlineLayer=bnd.GetLayer().GetName(),
                           cropToCutline=True)

    bnd.Release()
    bnd = None

    gdal.Warp(dest, rasters, options=opt)
Esempio n. 24
0
def gdalwarp(src, dst, options, pbar=False):
    """
    a simple wrapper for :osgeo:func:`gdal.Warp`

    Parameters
    ----------
    src: str, :osgeo:class:`ogr.DataSource` or :osgeo:class:`gdal.Dataset`
        the input data set
    dst: str
        the output data set
    options: dict
        additional parameters passed to :osgeo:func:`gdal.Warp`; see :osgeo:func:`gdal.WarpOptions`
    pbar: bool
        add a progressbar?

    Returns
    -------

    """
    try:
        if pbar:
            options = options.copy()
            widgets = [pb.Percentage(), pb.Bar(), pb.Timer(), ' ', pb.ETA()]
            progress = pb.ProgressBar(max_value=100, widgets=widgets).start()
            options['callback'] = __callback
            options['callback_data'] = progress
        out = gdal.Warp(dst, src, options=gdal.WarpOptions(**options))
        if pbar:
            progress.finish()
    except RuntimeError as e:
        raise RuntimeError('{}:\n  src: {}\n  dst: {}\n  options: {}'.format(
            str(e), src, dst, options))
    out = None
Esempio n. 25
0
def grib_to_tif(ds, out_path=None, **kwargs):
    ds, ras = ds_name(ds)

    if os.path.splitext(os.path.basename(ras))[1] != '.grib':
        return

    if out_path:
        out_file = context_file(ras, out_path)
    else:
        out_file = os.path.join(
            os.path.dirname(ras),
            os.path.splitext(os.path.basename(ras))[0] + '.tif')

    if os.path.exists(out_file):
        return out_file

    proj = "+proj=longlat +datum=WGS84 +ellps=WGS84"
    SpatialRef = osr.SpatialReference()
    SpatialRef.ImportFromProj4(proj)
    srs = kwargs.pop('dstSRS', SpatialRef)
    option = gdal.WarpOptions(multithread=True,
                              options=CONFIG,
                              creationOptions=CREATION,
                              **kwargs,
                              dstSRS=srs)
    gdal.Warp(out_file, ds, options=option)

    return out_file
Esempio n. 26
0
    def read_gdal_ds(file, shape_file, temp_dir):
        """
        Read a GDAL dataset clipping it with a given shapefile, if necessary
        :param file: Filepath of the GDAL file (.tif, etc.) as Pathlib
        :param shape_file: file path of the shapefile
        :param temp_dir: file path of the temporary directory
        :return: GDAL dataset
        """
        gdal_mask = gdal.Open(file.as_posix())

        if gdal_mask and shape_file:

            opt = gdal.WarpOptions(cutlineDSName=shape_file,
                                   cropToCutline=True,
                                   srcNodata=-9999,
                                   dstNodata=-9999,
                                   outputType=gdal.GDT_Int16)

            dest_name = (temp_dir / (file.stem + '_clipped')).as_posix()
            clipped_mask_ds = gdal.Warp(destNameOrDestDS=dest_name,
                                        srcDSOrSrcDSTab=gdal_mask,
                                        options=opt)
            clipped_mask_ds.FlushCache()
            gdal_mask = clipped_mask_ds

        return gdal_mask
    def run(self,
            input_ds,
            template_ds=None,
            bbox=None,
            resolution=CoordProperty(x=1 / 240., y=1 / 240.),
            grid_res=None,
            padding=CoordProperty(x=0, y=0),
            algorithm='bilinear'):
        if not grid_res:
            grid_res = resolution
        if template_ds:
            resolution = template_ds.resolution
            bbox = copy.copy(template_ds.bbox)
        if bbox:
            grid_box = [
                bbox.llc.x - padding.x, bbox.llc.y - padding.y,
                bbox.urc.x + padding.x, bbox.urc.y + padding.y
            ]
        else:
            grid_box = input_ds.gridcorners(grid_res,
                                            padding=padding).warp_output_bounds

        agg_warp_options = gdal.WarpOptions(
            xRes=resolution.x,
            yRes=resolution.y,
            outputBounds=grid_box,
            targetAlignedPixels=True,
            resampleAlg=algorithm,
        )
        gdal.Warp(self.locs['aligned'].path,
                  input_ds.dataset,
                  options=agg_warp_options)
 def run(self, input_ds, boundary_ds, algorithm='bilinear'):
     clip_warp_options = gdal.WarpOptions(format='GTiff',
                                          cutlineDSName=boundary_ds.loc.shp,
                                          cutlineBlend=.5,
                                          dstNodata=input_ds.nodata,
                                          resampleAlg=algorithm)
     gdal.Warp(path, input_ds.dataset, options=clip_warp_options)
Esempio n. 29
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)
 def run(self, input_ds, template_ds=None, algorithm='bilinear'):
     agg_warp_options = gdal.WarpOptions(
         outputBounds=template_ds.rev_warp_output_bounds,
         width=template_ds.size.x,
         height=template_ds.size.y,
         resampleAlg=algorithm,
     )
     gdal.Warp(self.locs['matched'].path,
               input_ds.dataset,
               options=agg_warp_options)