Example #1
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
Example #2
0
def resample(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

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

    return out_file
Example #3
0
def mosaic(ras_paths, out_path, **kwargs):
    ds = ras_paths[0]
    ds, ras = ds_name(ds)
    out_file = context_file(ras, out_path)

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

    separate = kwargs.pop('separate', False)
    resample_alg = kwargs.pop('resampleAlg', gdal.GRA_Average)
    ds = gdal.BuildVRT('/vsimem/Mosaic.vrt', ras_paths, separate=separate)

    option = gdal.WarpOptions(multithread=True,
                              creationOptions=CREATION,
                              resampleAlg=resample_alg,
                              **kwargs)
    gdal.Warp(out_file, ds, options=option)

    return out_file
Example #4
0
def map_calc(ds_multi, calc_args, out_path, band_idxs=None, multiprocess=True):
    iter_ds_multi = isinstance(ds_multi,
                               Iterable) and not isinstance(ds_multi, str)

    if iter_ds_multi:
        ds = ds_multi[0]
    else:
        ds = ds_multi

    ds, ras = ds_name(ds)
    out_file = context_file(ras, out_path)

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

    ds_multi, calc_args, band_idxs = broadcast_args(ds_multi, calc_args,
                                                    band_idxs)

    n = len(calc_args)
    args = zip(np.arange(1, n + 1, dtype=int), [ds_multi] * n, band_idxs,
               calc_args, [out_file] * n)

    if multiprocess:
        with Pool(min(cpu_count() - 1, n)) as p:
            tem_files = p.starmap(band_map, args)
    else:
        tem_files = []
        for arg in args:
            tem_files.append(band_map(*arg))

    if len(tem_files) == 1:
        os.rename(tem_files[0], out_file)
    else:
        mosaic(tem_files, out_file, separate=True)
        [os.remove(f) for f in tem_files]

    return out_file
Example #5
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

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

    return out_file