Пример #1
0
def mt_layover(filelist, outfile, temp_dir, extent):
    '''
    This function is usally used in the time-series workflow of OST. A list
    of the filepaths layover/shadow masks

    :param filelist - list of files
    :param out_dir - directory where the output file will be stored
    :return path to the multi-temporal layover/shadow mask file generated
    '''

    # get the start time for Info on processing time
    start = time.time()
    # create path to out file
    ls_layer = opj(temp_dir, os.path.basename(outfile))

    # create a vrt-stack out of
    print(' INFO: Creating common Layover/Shadow Mask')
    vrt_options = gdal.BuildVRTOptions(srcNodata=0, separate=True)
    gdal.BuildVRT(opj(temp_dir, 'ls.vrt'), filelist, options=vrt_options)

    with rasterio.open(opj(temp_dir, 'ls.vrt')) as src:

        # get metadata
        meta = src.meta
        # update driver and reduced band count
        meta.update(driver='GTiff', count=1, dtype='uint8')

        # create outfiles
        with rasterio.open(ls_layer, 'w', **meta) as out_min:

            # loop through blocks
            for _, window in src.block_windows(1):

                # read array with all bands
                stack = src.read(range(1, src.count + 1), window=window)

                # get stats
                arr_max = np.nanmax(stack, axis=0)
                arr = arr_max / arr_max

                out_min.write(np.uint8(arr), window=window, indexes=1)

    ras.mask_by_shape(ls_layer,
                      outfile,
                      extent,
                      to_db=False,
                      datatype='uint8',
                      rescale=False,
                      ndv=0)
    # os.remove(ls_layer)
    h.timer(start)

    return outfile
Пример #2
0
def mt_extent(list_of_scenes, out_file, temp_dir, buffer=None):

    out_dir = os.path.dirname(out_file)
    vrt_options = gdal.BuildVRTOptions(srcNodata=0, separate=True)

    # build vrt stack from all scenes
    gdal.BuildVRT(opj(out_dir, 'extent.vrt'),
                  list_of_scenes,
                  options=vrt_options)

    start = time.time()

    outline_file = opj(temp_dir, os.path.basename(out_file))
    ras.outline(opj(out_dir, 'extent.vrt'), outline_file, 0, False)

    vec.exterior(outline_file, out_file, buffer)
    h.delete_shapefile(outline_file)

    os.remove(opj(out_dir, 'extent.vrt'))
    h.timer(start)
Пример #3
0
def _grd_remove_border(infile):
    '''An OST function to remove GRD border noise from Sentinel-1 data

    This is a custom routine to remove GRD border noise
    from Sentinel-1 GRD products. It works on the original intensity
    images.

    NOTE: For the common dimap format, the infile needs to be the
    ENVI style file inside the *data folder.

    The routine checks the outer 3000 columns for its mean value.
    If the mean value is below 100, all values will be set to 0,
    otherwise the routine will continue fpr another 150 columns setting
    the value to 0. All further columns towards the inner image are
    considered valid.

    Args:
        infile: string or os.path object for a
                gdal compatible intensity file of Sentinel-1

    Notes:
        The file will be manipulated inplace, meaning,
        no new outfile is created.
    '''

    # print(' INFO: Removing the GRD Border Noise.')
    currtime = time.time()

    # read raster file and get number of columns adn rows
    raster = gdal.Open(infile, gdal.GA_Update)
    cols = raster.RasterXSize
    rows = raster.RasterYSize

    # create 3000xrows array for the left part of the image
    array_left = np.array(
        raster.GetRasterBand(1).ReadAsArray(0, 0, 3000, rows))

    for x in range(3000):
        # condition if more than 50 pixels within the line have values
        # less than 500, delete the line
        # if np.sum(np.where((array_left[:,x] < 200)
        # & (array_left[:,x] > 0) , 1, 0)) <= 50:
        if np.mean(array_left[:, x]) <= 100:
            array_left[:, x].fill(0)
        else:
            z = x + 150
            if z > 3000:
                z = 3000
            for y in range(x, z, 1):
                array_left[:, y].fill(0)

            cols_left = y
            break

    try:
        cols_left
    except NameError:
        cols_left = 3000

    # write array_left to disk
    # print(' INFO: Total amount of columns: {}'.format(cols_left))
    # print(' INFO: Number of colums set to 0 on the left side: '
    #     ' {}'.format(cols_left))
    # raster.GetRasterBand(1).WriteArray(array_left[:, :+cols_left], 0, 0, 1)
    raster.GetRasterBand(1).WriteArray(array_left[:, :+cols_left], 0, 0)

    array_left = None

    # create 2d array for the right part of the image (3000 columns and rows)
    cols_last = cols - 3000
    array_right = np.array(
        raster.GetRasterBand(1).ReadAsArray(cols_last, 0, 3000, rows))

    # loop through the array_right columns in opposite direction
    for x in range(2999, 0, -1):

        if np.mean(array_right[:, x]) <= 100:
            array_right[:, x].fill(0)
        else:
            z = x - 150
            if z < 0:
                z = 0
            for y in range(x, z, -1):
                array_right[:, y].fill(0)

            cols_right = y
            break

    try:
        cols_right
    except NameError:
        cols_right = 0

    #
    col_right_start = cols - 3000 + cols_right
    # print(' INFO: Number of columns set to 0 on the'
    #     ' right side: {}'.format(3000 - cols_right))
    # print(' INFO: Amount of columns kept: {}'.format(col_right_start))
    raster.GetRasterBand(1).WriteArray(array_right[:, cols_right:],
                                       col_right_start, 0)
    array_right = None
    h.timer(currtime)
Пример #4
0
def grd_remove_border(infile):
    """OST function to remove GRD border noise from Sentinel-1 data


    This is a custom routine to remove GRD border noise
    from Sentinel-1 GRD products. It works on the original intensity
    images.

    NOTE: For the common dimap format, the infile needs to be the
    ENVI style file inside the *data folder.

    The routine checks the outer 3000 columns for its mean value.
    If the mean value is below 100, all values will be set to 0,
    otherwise the routine will continue fpr another 150 columns setting
    the value to 0. All further columns towards the inner image are
    considered valid.

    :param infile:
    :return:
    """

    logger.debug(f'Removing the GRD Border Noise for {infile.name}.')
    currtime = time.time()

    # read raster file and get number of columns adn rows
    raster = gdal.Open(str(infile), gdal.GA_Update)
    cols = raster.RasterXSize
    rows = raster.RasterYSize

    # create 3000xrows array for the left part of the image
    array_left = np.array(
        raster.GetRasterBand(1).ReadAsArray(0, 0, 3000, rows))

    cols_left = 3000
    for x in range(3000):
        # if mean value of column is below 100, fil with 0s
        if np.mean(array_left[:, x]) <= 100:
            array_left[:, x].fill(0)
        else:
            z = x + 200
            if z > 3000:
                z = 3000
            for cols_left in range(x, z, 1):
                array_left[:, cols_left].fill(0)

            break

    # write array_left to disk
    logger.debug(f'Number of colums set to 0 on the left side: {cols_left}.')

    raster.GetRasterBand(1).WriteArray(array_left[:, :+cols_left], 0, 0)
    del array_left

    # create 2d array for the right part of the image (3000 columns and rows)
    cols_last = cols - 3000
    array_right = np.array(
        raster.GetRasterBand(1).ReadAsArray(cols_last, 0, 3000, rows))

    # loop through the array_right columns in opposite direction
    cols_right = 3000
    for x in range(2999, 0, -1):

        if np.mean(array_right[:, x]) <= 100:
            array_right[:, x].fill(0)
        else:
            z = x - 200
            if z < 0:
                z = 0
            for cols_right in range(x, z, -1):
                array_right[:, cols_right].fill(0)

            break

    col_right_start = cols - 3000 + cols_right
    logger.debug(
        f'Number of columns set to 0 on the right side: {3000 - cols_right}.')
    logger.debug(f'Amount of columns kept: {col_right_start}.')
    raster.GetRasterBand(1).WriteArray(array_right[:, cols_right:],
                                       col_right_start, 0)

    logger.debug(h.timer(currtime))
Пример #5
0
def _timeseries_to_timescan(burst_inventory, processing_dir, temp_dir,
                            burst_dir, to_db, metrics, outlier_removal):

    product_list = [
        'BS.HH', 'BS.VV', 'BS.HV', 'BS.VH', 'coh.VV', 'coh.VH', 'Alpha',
        'Entropy', 'Anisotropy'
    ]

    for product in product_list:
        for timeseries in glob.glob(
                opj(burst_dir, 'Timeseries', '*{}*vrt'.format(product))):

            print(' INFO: Creating timescan for {}'.format(product))
            timescan_dir = opj(burst_dir, 'Timescan')
            os.makedirs(timescan_dir, exist_ok=True)

            # we get the name of the time-series parameter
            polarisation = timeseries.split('/')[-1].split('.')[2]
            if polarisation == 'vrt':
                timescan_prefix = opj(
                    '{}'.format(timescan_dir),
                    '{}'.format(timeseries.split('/')[-1].split('.')[1]))
            else:
                timescan_prefix = opj(
                    '{}'.format(timescan_dir), '{}.{}'.format(
                        timeseries.split('/')[-1].split('.')[1], polarisation))

            start = time.time()
            if 'BS.' in timescan_prefix:  # backscatter
                ts.mt_metrics(timeseries,
                              timescan_prefix,
                              metrics,
                              rescale_to_datatype=True,
                              to_power=to_db,
                              outlier_removal=outlier_removal)
            else:  # non-backscatter
                ts.mt_metrics(timeseries,
                              timescan_prefix,
                              metrics,
                              rescale_to_datatype=False,
                              to_power=False,
                              outlier_removal=outlier_removal)

            h.timer(start)

    # rename and create vrt
    # print('renaming')
    i, list_of_files = 0, []
    for product in itertools.product(product_list, metrics):

        file = glob.glob(
            opj(burst_dir, 'Timescan',
                '*{}.{}.tif'.format(product[0], product[1])))

        if file:
            i += 1
            outfile = opj(burst_dir, 'Timescan',
                          '{}.{}.{}.tif'.format(i, product[0], product[1]))
            shutil.move(file[0], outfile)
            list_of_files.append(outfile)

    # create vrt
    vrt_options = gdal.BuildVRTOptions(srcNodata=0, separate=True)
    gdal.BuildVRT(opj(burst_dir, 'Timescan', 'Timescan.vrt'),
                  list_of_files,
                  options=vrt_options)
Пример #6
0
def mt_layover(filelist, outfile, temp_dir, extent, update_extent=False):
    '''
    This function is usally used in the time-series workflow of OST. A list
    of the filepaths layover/shadow masks

    :param filelist - list of files
    :param out_dir - directory where the output file will be stored
    :return path to the multi-temporal layover/shadow mask file generated
    '''
    if type(filelist) == str:
        filelist = filelist.replace("'", '').strip('][').split(', ')
    if type(update_extent) == str:
        if update_extent == 'False':
            update_extent = False
    # get some info
    burst_dir = os.path.dirname(outfile)
    burst = os.path.basename(burst_dir)
    extent = opj(burst_dir, '{}.extent.shp'.format(burst))

    # get the start time for Info on processing time
    start = time.time()
    # create path to out file
    ls_layer = opj(temp_dir, os.path.basename(outfile))

    # create a vrt-stack out of
    print(' INFO: Creating common Layover/Shadow Mask')
    vrt_options = gdal.BuildVRTOptions(srcNodata=0, separate=True)
    gdal.BuildVRT(opj(temp_dir, 'ls.vrt'), filelist, options=vrt_options)

    with rasterio.open(opj(temp_dir, 'ls.vrt')) as src:

        # get metadata
        meta = src.meta
        # update driver and reduced band count
        meta.update(driver='GTiff', count=1, dtype='uint8')

        # create outfiles
        with rasterio.open(ls_layer, 'w', **meta) as out_min:

            # loop through blocks
            for _, window in src.block_windows(1):

                # read array with all bands
                stack = src.read(range(1, src.count + 1), window=window)

                # get stats
                arr_max = np.nanmax(stack, axis=0)
                arr = arr_max / arr_max

                out_min.write(np.uint8(arr), window=window, indexes=1)

    ras.mask_by_shape(ls_layer,
                      outfile,
                      extent,
                      to_db=False,
                      datatype='uint8',
                      rescale=False,
                      ndv=0)
    os.remove(ls_layer)
    h.timer(start)

    if update_extent:
        print(' INFO: Calculating symetrical difference of extent and ls_mask')
        # polygonize the multi-temporal ls mask
        ras.polygonize_raster(outfile, '{}.shp'.format(outfile[:-4]))

        # create file for masked extent
        extent_ls_masked = opj(burst_dir, '{}.extent.masked.shp'.format(burst))

        # calculate difference between burst exntetn and ls mask, fr masked extent
        vec.difference(extent, '{}.shp'.format(outfile[:-4]), extent_ls_masked)