def createCloudMask(band_array, path):
    stacked = np.stack(band_array, -1)
    # print(stacked / 10000)
    # swapped = np.moveaxis(stacked, 0, 2)  # shape (y_pixels, x_pixels, n_bands)

    arr4d = np.expand_dims(
        stacked / 10000, 0
    )  # shape (1, y_pixels, x_pixels, n_bands) # s2cloudless requires binary map
    cloud_detector = S2PixelCloudDetector(threshold=0.7,
                                          average_over=4,
                                          dilation_size=2)
    cloud_prob_map = cloud_detector.get_cloud_probability_maps(np.array(arr4d))
    cloud_masks = cloud_detector.get_cloud_masks(np.array(arr4d))
    # show files
    plt.figure(figsize=(50, 50))
    plt.imshow(np.squeeze(
        cloud_masks))  # check whz process not stopping, when closing
    plt.show()
    # save files

    template_file = "/home/Usuario/Documents/sentinelHub/S2A_MSIL1C_20190904T140051_N0208_R067_T21LWH_20190904T172119.SAFE/GRANULE/L1C_T21LWH_A021943_20190904T140439/IMG_DATA/T21LWH_20190904T140051_B01.jp2"
    print(template_file)
    out_prob = path[:-31] + "out_prob.tif"
    out_mask = path[:-31] + "out_mask.tif"
    output = gdal_array.SaveArray(cloud_prob_map[0, :, :],
                                  out_prob,
                                  format="GTiff",
                                  prototype=template_file)
    output = None
    output = gdal_array.SaveArray(cloud_masks[0, :, :],
                                  out_mask,
                                  format="GTiff",
                                  prototype=template_file)
    output = None
def save_results_to_tif(bfast_array, method, name):
    '''
    Saves BFAST results to a .tif file
    Input:
    bfast_array: Array [] with BFAST classifications
    method: String "" If Monhtly Images or BAP Images were used and breaks or mean
    name: String "" Name of the Index and tile
    '''
    if name.find('LYH') != -1:
        template_file = '/scratch/tmp/s_lech05/hls_data/21LYH/template_file_21LYH.tif'
        out_path = '/scratch/tmp/s_lech05/hls_data/21LYH/' + name + method + '_bfast.tif'
        output = gdal_array.SaveArray(bfast_array,
                                      out_path,
                                      format="GTiff",
                                      prototype=template_file)
        print("saved file to " + out_path)
        out_path = None
        output = None
        bfast_array = None
        template_file = None
    else:
        template_file = '/scratch/tmp/s_lech05/hls_data/21LYG/template_file_21LYG.tif'
        out_path = '/scratch/tmp/s_lech05/hls_data/21LYG/' + name + method + '_bfast.tif'
        output = gdal_array.SaveArray(bfast_array,
                                      out_path,
                                      format="GTiff",
                                      prototype=template_file)
        print("saved file to " + out_path)
        out_path = None
        output = None
        bfast_array = None
        template_file = None
def create_mask(band_array, path):
    """Crete the mask."""
    stacked = np.stack(band_array, -1)
    # shape (1, y_pixels, x_pixels, n_bands)
    # s2cloudless requires binary map
    arr4d = np.expand_dims(stacked / 10000, 0)
    cloud_detector = S2PixelCloudDetector(threshold=0.7,
                                          average_over=4,
                                          dilation_size=2)
    cloud_prob_map = cloud_detector.get_cloud_probability_maps(np.array(arr4d))
    cloud_masks = cloud_detector.get_cloud_masks(np.array(arr4d))
    template_file = [
        os.path.join(path, item) for item in os.listdir(path)
        if item.endswith("01.jp2")
    ][0]
    # save files
    out_prob = os.path.dirname(path) + "_s2cloudless_prob.tif"
    out_mask = os.path.dirname(path) + "_s2cloudless_mask.tif"
    output = gdal_array.SaveArray(cloud_prob_map[0, :, :],
                                  out_prob,
                                  format="GTiff",
                                  prototype=template_file)
    output = None
    output = gdal_array.SaveArray(cloud_masks[0, :, :],
                                  out_mask,
                                  format="GTiff",
                                  prototype=template_file)
    output = None
    return (out_prob, out_mask)
Example #4
0
def doit(src_filename, dst_magnitude, dst_phase):
    src_ds = gdal.Open(src_filename)
    xsize = src_ds.RasterXSize
    ysize = src_ds.RasterYSize
    print('{} x {}'.format(xsize, ysize))

    src_image = src_ds.GetRasterBand(1).ReadAsArray()
    mag_image = pow(np.real(src_image) * np.real(src_image) +
                    np.imag(src_image) * np.imag(src_image), 0.5)
    gdal_array.SaveArray(mag_image, dst_magnitude)

    phase_image = np.angle(src_image)
    gdal_array.SaveArray(phase_image, dst_phase)
    return 0
def create_clear_sky_image(path):
    hls_dataset = gdal.Open(path)
    cloud_mask = gdal.Open(path[:-3] + "/cloud_mask.tif")
    if not cloud_mask:
        return
    cloud_mask_array = cloud_mask.ReadAsArray()
    invert_cloud_mask_array = np.zeros_like(cloud_mask_array)
    invert_cloud_mask_array[cloud_mask_array == 0] = 1
    invert_cloud_mask_array[cloud_mask_array == 1] = 0
    clear_sky_paths = []
    subdatasets = hls_dataset.GetSubDatasets()
    for dataset_path in subdatasets:
        dataset = gdal.Open(dataset_path[0])
        dataset_array = dataset.ReadAsArray()
        clear_sky_array = dataset_array * invert_cloud_mask_array
        template_file = dataset
        out_path = path[:-3] + "/" + dataset_path[0][-3:] + '_clear_sky.tif'
        if (os.path.isfile(out_path)):
            clear_sky_paths.append(out_path)
            continue
        clear_sky_file = gdal_array.SaveArray(clear_sky_array,
                                              out_path,
                                              format="GTiff",
                                              prototype=template_file)
        clear_sky_file = None
    print(f"Finished creating clear_sky images for file {path}")
    return clear_sky_paths
Example #6
0
def raster_band2jpeg(infile, band, outfile, format='JPEG'):
    ''' raster_band2jpeg() - Extract a raster band to a file

    Parameters
    ----------
    infile : str
        input file name

    band : int
        image band to process
    
    outfile : str
        output file name

    format : str, default='JPEG'
        output file format

    Returns
    -------
    nothing

    '''
    from osgeo import gdal_array as gdal_array

    srcArray = gdal_array.LoadFile(infile)
    band = srcArray[band - 1]
    gdal_array.SaveArray(band, outfile, format=format)
    return
Example #7
0
def save_ind_img(path, array_img, name, tile, overwrite, metadata):
    month = extract_sensing_month(metadata)
    year = extract_sensing_year(metadata)
    day = extract_sensing_day(metadata)
    if month < 10:
        month = "0" + str(month)
    if day < 10:
        day = "0" + str(day)
    out_path = f"{path}/{name}d{day}m{month}y{year}.tif"
    if (os.path.isfile(out_path) and not overwrite):
        print(f"file {out_path} already exists")
        return
    ind_output = gdal_array.SaveArray(
        array_img,
        out_path,
        format="GTiff",
        prototype=
        f"../../../../scratch/tmp/s_lech05/hls_data/{tile}/template_file_{tile}.tif"
    )
    print(f"saved file to {out_path}")
    daystring = extract_sensing_day_from_filename(out_path)
    monthstring = extract_sensing_month_from_filename(out_path)
    yearstring = extract_sensing_year_from_filename(out_path)
    print(f"day: {daystring}, month:{monthstring} year:{yearstring}")
    ind_output = None
Example #8
0
def doit(src_filename, dst_filename):
    class_defs = [(1, 10, 20),
                  (2, 20, 30),
                  (3, 128, 255)]

    src_ds = gdal.Open(src_filename)
    xsize = src_ds.RasterXSize
    ysize = src_ds.RasterYSize

    src_image = gdal_array.LoadFile(src_filename)

    dst_image = np.zeros((ysize, xsize))

    for class_info in class_defs:
        class_id = class_info[0]
        class_start = class_info[1]
        class_end = class_info[2]

        class_value = np.ones((ysize, xsize)) * class_id

        mask = np.bitwise_and(
            np.greater_equal(src_image, class_start),
            np.less_equal(src_image, class_end))

        dst_image = np.choose(mask, (dst_image, class_value))

    gdal_array.SaveArray(dst_image, dst_filename)
def classify(file_path, file_name, classes, lut, start=0):
    """
    分类并保存为图片
    :param file_path:
    :param file_name
    :param classes: 分类区间
    :param lut: 颜色 RGB 元祖, len(classes)+1
    :param start
    :return:
    """
    # 读取数据
    src_arr = read_nc_data(file_path, var='APCP_surface')
    # 根据类别数目将直方图分割成5个颜色区间
    # classes = gdal_array.numpy.histogram(srcArr, bins=5)[1]
    # 创建一个RGB颜色的JPEG输出图片
    rgb = gdal_array.numpy.zeros((3, src_arr.shape[0], src_arr.shape[1]), gdal_array.numpy.float32)
    # 处理所有类并声明颜色
    for i in range(len(classes)):
        mask = gdal_array.numpy.logical_and(start <= src_arr, src_arr <= classes[i])
        for j in range(len(lut[i])):
            rgb[j] = gdal_array.numpy.choose(mask, (rgb[j], lut[i][j]))
        start = classes[i] + 0.001  # 0.001 要<=像元值精度
    # 保存图片
    output = gdal_array.SaveArray(rgb.astype(gdal_array.numpy.uint8), file_name, format='JPEG')
    # 取消输出避免在某些平台上损坏文件
    output = None
Example #10
0
def img_stretch(infile, outfile):
    '''img_stretch() - Stretch the color bands of an image

    Parameters
    ----------
    infile : str
        image file name

    outfile : str
        modified image output file name

    Returns
    -------
    nothing
    
    '''
    from osgeo import gdal_array as gdal_array
    from stretch import stretch

    arr = gdal_array.LoadFile(infile)
    stretched = stretch(arr)
    output = gdal_array.SaveArray(arr,
                                  outfile,
                                  format='GTiff',
                                  prototype=infile)
    output = None

    return
Example #11
0
def img_swap_bands(infile, outfile, band1=1, band2=2):
    ''' img_swap_bands() - Reads a TIFF file, swaps two bands, and saves it
    
    Parameters
    ----------
    infile : str
        input image file name
    
    outfile : str
        output image file name
    
    band1, band2 : int
        bands to be swapped
    
    Return
    ------
    nothing
    
    '''

    from osgeo import gdal_array as gdal_array

    arr = gdal_array.LoadFile(infile)
    output = gdal_array.SaveArray(arr[[band1, 0, 2], :],
                                  outfile,
                                  format='GTIFF',
                                  prototype=infile)
    # prototype copies the georeferencing information to output file
    output = None  # force release from memory
    return
def save_ind_img(path, ind, name, template_file):
    out_path = path + name + '.tif'
    if (os.path.isfile(out_path)):
        return
    # print(f"saved file to {out_path}")
    ind_output = gdal_array.SaveArray(ind, out_path, format="GTiff", prototype=gdal.Open(template_file))
    ind_output = None
Example #13
0
def classify(file, src_raster):
    """
    分类并保存为图片
    :param file:
    :param src_raster:
    :return:
    """

    classes = np.array([0, 3, 10, 20, 50, 70, 1000])
    lut = [[255, 255, 255], [154, 250, 142], [60, 163, 10], [106, 178, 250],
           [8, 21, 255], [70, 251, 247], [111, 13, 10]]
    start = 0.0001

    # 读取数据
    data = gdal_array.LoadFile(src_raster)
    data[data == 0] = None

    # 根据类别数目将直方图分割成5个颜色区间
    # classes = gdal_array.numpy.histogram(srcArr, bins=5)[1]
    # 创建一个RGB颜色的JPEG输出图片
    rgb = gdal_array.numpy.zeros((3, data.shape[0], data.shape[1]),
                                 gdal_array.numpy.float32)
    # 处理所有类并声明颜色
    for i in range(len(classes)):
        mask = gdal_array.numpy.logical_and(start <= data, data <= classes[i])
        for j in range(len(lut[i])):
            rgb[j] = gdal_array.numpy.choose(mask, (rgb[j], lut[i][j]))
        start = classes[i] + 0.001  # 0.001 要<=像元值精度
    # 保存图片
    output = gdal_array.SaveArray(rgb.astype(gdal_array.numpy.uint8),
                                  file,
                                  format='PNG')
    # 取消输出避免在某些平台上损坏文件
    output = None
Example #14
0
    def array3DToImage(self, imageArray=None, rasterFormat='ERDAS'):
        '''
        Saves a 3D array to an image file of type HFA.
        NOTE. THIS WILL NOT REQUIRE TO OPEN A HOLDER data set.
        It will use like_src_dataset
        '''

        #FIXME:error if the name and out_name are not the same...shijo's problem check in outFileExt...weird...
        #FIXME: try to solve the need of outfileExt
        def update():
            self.out_filename = self.name[:-4] + '_new'
            self.outfileExt = self.name[
                -4:]  # required to test for name duplicates
            if self.out_path == None:
                self.out_path = 'D://My Docs//working//out_images//'
            pass

        if imageArray == None:
            imageArray = self.asArray()
            pass
        if self.out_filename == None:
            update(
            )  # It will make a copy of itself if no out filename is given.
        if (self.name == (self.out_filename + self.outfileExt)): update()
        self.status = None
        idriver, fileExt = self.knownRasterFormats[rasterFormat]
        self.outfileExt = fileExt
        outfilenamePlusPath = self.out_path + self.out_filename + self.outfileExt
        oga.SaveArray(imageArray,
                      outfilenamePlusPath,
                      format=idriver,
                      prototype=self.dataset)
        self.status = True
        pass
Example #15
0
    def test_on_image(self,
                      image_dir,
                      output_dir,
                      lr_block_size=(20, 20),
                      metrics=[psnr, r2]):
        # Load images
        print('loading image pairs from {}'.format(image_dir))
        input_images, valid_image = load_image_pairs(image_dir,
                                                     scale=self.scale)
        assert len(input_images) == 3
        name = input_images[-1].filename.name if hasattr(
            input_images[-1], 'filename') else ''
        print('Predict on image {}'.format(name))

        # Generate output image and measure run time
        # x_inputs的shape为四数组(数目,长度,宽度,通道数)
        x_inputs = [self.validate(img_to_array(im)) for im in input_images]
        assert x_inputs[0].shape[1] % lr_block_size[0] == 0
        assert x_inputs[0].shape[2] % lr_block_size[1] == 0
        x_train, _ = load_test_set((input_images, valid_image),
                                   lr_block_size=lr_block_size,
                                   scale=self.scale)

        model = self.compile(self.build_model(*x_train))
        if self.model_file.exists():
            model.load_weights(str(self.model_file))

        t_start = time.perf_counter()
        y_preds = model.predict(x_train, batch_size=1)  # 结果的shape为四维
        # 预测结束后进行恢复
        y_pred = np.empty(x_inputs[1].shape[-3:], dtype=np.float32)
        row_step = lr_block_size[0] * self.scale
        col_step = lr_block_size[1] * self.scale
        rows = x_inputs[0].shape[2] // lr_block_size[1]
        cols = x_inputs[0].shape[1] // lr_block_size[0]
        count = 0
        for j in range(rows):
            for i in range(cols):
                y_pred[i * row_step:(i + 1) * row_step,
                       j * col_step:(j + 1) * col_step] = y_preds[count]
                count += 1
        assert count == rows * cols
        t_end = time.perf_counter()

        # Record metrics
        row = pd.Series()
        row['name'] = name
        row['time'] = t_end - t_start
        y_true = self.validate(img_to_array(valid_image))
        y_pred = self.validate(y_pred)
        for metric in metrics:
            row[metric.__name__] = K.eval(metric(y_true, y_pred))

        prototype = str(valid_image.filename) if hasattr(
            valid_image, 'filename') else None
        gdal_array.SaveArray(y_pred[0].squeeze().astype(np.int16),
                             str(output_dir / name),
                             prototype=prototype)

        return row
Example #16
0
 def func_standardFakeColor(self, count1, count2, count3):
     '''
    图像预处理:
        假彩色处理(图像变换),对已加载的影像进行假彩色合成处理,用户可自定义红绿蓝三通道分别选取何种波段。
     '''
     self.showpanel.append(
         "当前操作--假彩色变换,请等待--------------------------------")
     if self.noPictureWarn() == False:  # 处理异常
         return
     path = self.path
     arr = gdal_array.LoadFile("{}".format(path))
     prepicture_path = "optimized/standardFakeColor.tif"
     output = gdal_array.SaveArray(arr[[count1, count2, count3], :],
                                   prepicture_path,
                                   format="GTiff",
                                   prototype=path)
     output = None  # 取消输出避免在某些平台上损坏文件
     img = cv.imread("{}".format(prepicture_path))
     img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
     x = img.shape[1]
     y = img.shape[0]
     self.zoomscale = 1
     frame = QImage(img, x, y, QImage.Format_RGB888)
     pix = QPixmap.fromImage(frame)
     self.item.setPixmap(pix)
     self.scene.addItem(self.item)
     self.showpanel.append("standard fake color over")  #通知栏打印结果
Example #17
0
def save_results_to_tif(bfast_array, method, name):
    if name.find('LYH') != -1:
        template_file = '/data/sarah/pythonscripts/hls/Images/21LYH/2018/L30/HLS.L30.T21LYH.2018019.v1.4/d01_clear_sky.tif'
        out_path = '/data/sarah/pythonscripts/hls/Images/21LYH/' + name + method + '_bfast_tif'
        output = gdal_array.SaveArray(bfast_array,
                                      out_path,
                                      format="GTiff",
                                      prototype=template_file)
        output = None
    else:
        template_file = '/data/sarah/pythonscripts/hls/Images/21LYG/2016/L30/HLS.L30.T21LYG.2016110.v1.4/d01_clear_sky.tif'
        out_path = '/data/sarah/pythonscripts/hls/Images/21LYG/' + name + method + '_bfast_tif'
        output = gdal_array.SaveArray(bfast_array,
                                      out_path,
                                      format="GTiff",
                                      prototype=template_file)
        output = None
Example #18
0
def controller_for_clip(work_dir, input_tif, input_shp, output_tif):
    #work_dir 裁剪功能工作目录,定义这个后可基于此调用相对路径
    #input_tif 用于裁剪的Tif数据的相对路径
    #input_shp 用于裁剪的栅格shp数据的相对路径
    #output_tif 输出Tif数据
    #下面是一个例子

    os.chdir(work_dir)
    run = CHANGE()

    # 用于裁剪的栅格数据
    raster = input_tif
    # 用于裁剪的多边形shp文件
    shp = input_shp
    # 裁剪后的栅格数据
    output = output_tif

    src_array = gdal_array.LoadFile(raster)
    # 同时载入gdal库的图片从而获取geotransform
    src_image = gdal.Open(raster)
    geo_trans = src_image.GetGeoTransform()

    # 使用PyShp库打开shp文件
    r = shapefile.Reader("{}.shp".format(shp))

    # 将图层扩展转换为图片像素坐标
    min_x, min_y, max_x, max_y = r.bbox
    ul_x, ul_y = run.world_to_pixel(geo_trans, min_x, max_y)
    lr_x, lr_y = run.world_to_pixel(geo_trans, max_x, min_y)
    # 计算新图片的尺寸
    px_width = int(lr_x - ul_x)
    px_height = int(lr_y - ul_y)

    clip = src_array[:, ul_y:lr_y, ul_x:lr_x]
    # 为图片创建一个新的geomatrix对象以便附加地理参照数据
    geo_trans = list(geo_trans)
    geo_trans[0] = min_x
    geo_trans[3] = max_y
    # 在一个空白的8字节黑白掩膜图片上把点映射为像元绘制市县
    # 边界线
    pixels = []
    for p in r.shape(0).points:
        pixels.append(run.world_to_pixel(geo_trans, p[0], p[1]))
    raster_poly = Image.new("L", (px_width, px_height), 1)
    # 使用PIL创建一个空白图片用于绘制多边形
    rasterize = ImageDraw.Draw(raster_poly)
    rasterize.polygon(pixels, 0)
    # 使用PIL图片转换为Numpy掩膜数组
    mask = run.image_to_array(raster_poly)
    # 根据掩膜图层对图像进行裁剪
    clip = gdal_array.numpy.choose(mask,
                                   (clip, 0)).astype(gdal_array.numpy.float)
    # 将裁剪结果保存为tiff文件
    gdal_array.SaveArray(clip,
                         "{}.tif".format(output),
                         format="GTiff",
                         prototype=raster)
Example #19
0
def write_gdal_grid(filename, grid, griddef):

    ps_x = (griddef[2] - griddef[0]) / (griddef[4] - 1)
    ps_y = (griddef[3] - griddef[1]) / (griddef[5] - 1)
    geotransform = (griddef[0] - ps_x * 0.5, ps_x, 0.0,
                    griddef[1] - ps_y * 0.5, 0.0, ps_y)

    grid = grid.astype(numpy.float32)
    ds = gdal_array.SaveArray(grid, filename, format='CTable2')
    ds.SetGeoTransform(geotransform)
Example #20
0
def img_diff(img1, img2, outfile):
    ''' img_diff() - Perform a simple difference image change detection 
        on matched 'before' and 'after' images

    Parameters
    ----------
    img1 : str
        input image file name
    
    img2 : str
        input image file name
    
    outfile : str
        output image file name
    
    Return
    ------
    nothing
    
    '''

    from osgeo import gdal_array as gdal_array
    import numpy as np

    # Load before and after into arrays
    ar1 = gdal_array.LoadFile(img1).astype(np.int8)
    ar2 = gdal_array.LoadFile(img2)[1].astype(np.int8)
    diff = ar2 - ar1  # Perform a simple array difference on the images

    # Set up our classification scheme to try and isolate significant changes
    classes = np.histogram(diff, bins=5)[1]

    # The color black is repeated to mask insignificant changes
    lut = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 255, 0],
           [255, 0, 0]]

    start = 1  # Starting value for classification

    # Set up the output image
    rgb = np.zeros((
        3,
        diff.shape[0],
        diff.shape[1],
    ), np.int8)

    for i in range(len(classes)):  # Process all classes and assign colors
        mask = np.logical_and(start <= diff, diff <= classes[i])
        for j in range(len(lut[i])):
            rgb[j] = np.choose(mask, (rgb[j], lut[i][j]))
        start = classes[i] + 1

    output = gdal_array.SaveArray(rgb, outfile, format='GTiff', prototype=img2)
    output = None  # explicitly release memory

    return
Example #21
0
def calculateNdvi(clip_raster, ndvi_raster):
    print("calculate NDVI...")
    img_array = ga.LoadFile(clip_raster)
    a = img_array[3] - img_array[2]
    b = img_array[3] + img_array[2]
    ndvi_array = a / b
    outndvi = ga.SaveArray(ndvi_array,
                           ndvi_raster,
                           format="GTiff",
                           prototype=clip_raster)
    outndvi = None
Example #22
0
def saveGTiff(ndarray, fileName):
    """
    ndarray保存为GTiff格式图片\n
    参数:\n
    ndarray - (ndarray)图片\n
    fileName - (str)文件名\n
    返回值:\n
    None
    """
    ga.SaveArray(ndarray, fileName, format="GTiff")
    return
Example #23
0
def thumbnail(root_scene, scene_dir, verbose=False):
    red_file = '%s/%s_B4.TIF' % (scene_dir, root_scene)
    grn_file = '%s/%s_B3.TIF' % (scene_dir, root_scene)
    blu_file = '%s/%s_B2.TIF' % (scene_dir, root_scene)

    if not os.path.exists(red_file) or not os.path.exists(grn_file) \
            or not os.path.exists(blu_file):
        print 'Missing one or more of %s, %s and %s, skip thumbnailing.' % (
            red_file, grn_file, blu_file)
        return

    large_thumbnail = numpy.array([
        get_band(red_file, 15),
        get_band(grn_file, 15),
        get_band(blu_file, 15)])
    
    small_thumbnail = numpy.array([
        get_band(red_file, 3),
        get_band(grn_file, 3),
        get_band(blu_file, 3)])

    # Set the scale values for both images from the larger one:
    scale_min, scale_max = get_scale(large_thumbnail)
    
    large_thumbnail = scale_image(large_thumbnail, scale_min, scale_max)
    small_thumbnail = scale_image(small_thumbnail, scale_min, scale_max)

    # TODO: Georeference these jpegs
    gdal_array.SaveArray(
        large_thumbnail,
        '%s/%s_thumb_large.jpg' % (scene_dir, root_scene),
        format = 'JPEG')

    gdal_array.SaveArray(
        small_thumbnail,
        '%s/%s_thumb_small.jpg' % (scene_dir, root_scene),
        format = 'JPEG')

    for filename in os.listdir(scene_dir):
        if filename.endswith('.aux.xml'):
            os.unlink(os.path.join(scene_dir,filename))
    def water_from_dem(self):
        slope = os.path.join(self.dem_dir, self.tile + "_dem_slope_4326.tif")
        slope2 = os.path.join(self.dem_dir,
                              self.tile + "_dem_slope_4326_2.tif")
        data = gdal_array.LoadFile(slope)

        # De-speckle
        data = scipy.signal.medfilt2d(data, kernel_size=3)
        gdal_array.SaveArray(data.astype(numpy.int8),
                             slope2,
                             prototype=gdal.Open(slope))
        print "saved water from dem", slope2
def runRF(index, path, year, X, y, ts, tile, bap):
    '''
    Runs RF algorithm
    Input:
    index: String Name of index for which RF runs
    path: String Folder to save the result
    year: Int year for which RF runs
    X:sample data (pixel values)
    y:corresponding labels of pixel
    ts: timeseries (3D index array)
    bap: Boolean, BAP timeseries?
    Output:
    saves classification result to .tif file
    '''
    # Tell GDAL to throw Python exceptions, and register all drivers
    gdal.UseExceptions()
    gdal.AllRegister()
    img = np.asarray(ts)

    print('Creating classifier')
    # Initialize model with 500 trees
    rf = RandomForestClassifier(n_estimators=500, oob_score=True)

    # Fit model to training data
    rf = rf.fit(X, y)

    print('Our OOB prediction of for index {ind} accuracy is: {oob}%'.format(oob=rf.oob_score_ * 100, ind=index))

    print(img.shape[2])
    new_shape = (img.shape[0] * img.shape[1], img.shape[2])

    img_as_array = img.reshape(new_shape)
    print('Reshaped from {o} to {n}'.format(o=img.shape,
                                            n=img_as_array.shape))

    img_as_array = np.where(np.isnan(img_as_array), -9999, img_as_array.astype('float32'))
    img_as_array = np.where(np.isinf(img_as_array), -9999, img_as_array.astype('float32'))

    class_prediction = rf.predict(img_as_array)
    print(class_prediction.shape)
    #Labels: 1 - Forest; 2- Pasture; 3 - Agriculture 4- Deforestation
    class_prediction = class_prediction.reshape(img.shape[0], img.shape[1])
    # first_img = class_prediction[:, :, 0]
    if bap:
        out_path = path + 'classified_BAP' + index + str(year) + '.gtif'
    else:
        out_path = path + 'classified_Months' + index + str(year) + '.gtif'
    prediction_file = gdal_array.SaveArray(class_prediction, out_path, format="GTiff",
                                           prototype=path + "template_file_" + tile + '.tif')
    print(f"classified TS saved to {out_path}")
    prediction_file = None
    """  class_prediction = class_prediction.reshape(img[:, :, 0].shape)
Example #26
0
    def writeBandsAsImages(self, imageArray=None, rasterFormat='ERDAS'):
        '''
        Reads a GDAL supported image and save each individual band as a new file 
        in a different or current GDAL image supported formats. It keep when possible
        the metadata from the source image.
        - outPath should be in the format: D:\\working\\path\\
        - outfilename should not have extension.
    
        outFormat:
        ERDAS imaging = 'HFA'
        Geotiff format = 'GTiff'
        Idrisi fomat = 'RST'
        Ilwis format = 'ILWIS'
        '''
        years = [98, 99, 0, 1, 2, 3, 4, 5, 6, 7, 8]
        months = [4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3]
        day = [1, 2, 3]
        names = [
            'NDV' + str(iyear).zfill(2) + str(imonth).zfill(2) + str(iday) +
            'PL' for iyear in years for imonth in months for iday in day
        ]

        def update():
            self.out_filename = self.name[:-4]
            if self.out_path == None:
                self.out_path = 'D://My Docs//working//out_test//'
            pass

        if imageArray == None:
            imageArray = self.asArray()
            pass
        if self.out_filename == None:
            update(
            )  # It will make a copy of itself if no out filename is given.

        self.status = None
        idriver, fileExt = self.knownRasterFormats[rasterFormat]
        self.outfileExt = fileExt
        for iband in range(imageArray.shape[0]):
            #output = self.out_path+self.out_filename+'_b'+str(iband+1).zfill(3)+self.outfileExt
            output = self.out_path + names[iband] + self.outfileExt
            # Without the prototype will not save properly.
            oga.SaveArray(imageArray[iband, :, :],
                          output,
                          format=idriver,
                          prototype=self.dataset,
                          INTERLEAVE='BIL')
            pass
        return True
Example #27
0
def save_cloud_mask(path, cloud_mask):
    out_path = path[:-3] + "/" + "cloud_mask.tif"
    if path.find("cirrus") != -1:
        path = path[:-6]
    if path.find("cloud") != -1:
        path = path[:-5]
    if path.find("shadow") != -1:
        path = path[:-6]
    if path.find("adj") != -1:
        path = path[:-3]
    if path.find('LYH') != -1:
        template_file = '/scratch/tmp/s_lech05/hls_data/21LYH/template_file_21LYH.tif'
    else:
        template_file = '/scratch/tmp/s_lech05/hls_data/21LYG/template_file_21LYG.tif'
    output = gdal_array.SaveArray(cloud_mask,
                                  out_path,
                                  format="GTiff",
                                  prototype=template_file)
    output = None
Example #28
0
def tif_to_mask(model,
                sheet: str,
                display: bool = False,
                threshold=0.5,
                output: str = None):
    ds = gdal.Open(
        '{0}.tif'.format(sheet)
    )  #get from https://data.linz.govt.nz/layer/51870-wellington-03m-rural-aerial-photos-2012-2013/
    ary = np.dstack([ds.GetRasterBand(i).ReadAsArray() for i in range(1, 5)])
    print('Input tif has shape:', ary.shape)

    # Convert raster array to tiles, need to ensure it is perfectly tiled!!
    W_test, excluded = ary_to_tiles(ary, shape=(256, 256))
    if excluded > 0:
        print((excluded, ), *ary.shape)
        raise ValueError('''
        Need to ensure perfect tiles to create full mask of input tif! 
        You have missed {0} tiles from an input raster array of shape ({1},{2}) 
        Try and find common factors for the input shape {1},{2} 
        to input into the (img_height, img_width) parameters instead of ({4},{5})
        '''.format((excluded, ), *ary.shape, 256, 256))

    W_hat_test = model.predict(W_test, verbose=1)
    #print('Finished predict on {0} tiles of shape ({1},{2}) for: {4}'.format(*W_hat_test.shape + (sheet,)))

    W_hat_ary = tiles_to_ary(stacked_ary=W_hat_test, final_shape=ary.shape[:2])

    output_ary = W_hat_ary[:, :, 0]
    output_ary[output_ary < threshold] = 0
    output_ary[output_ary >= threshold] = 1

    if output != None:
        print('Output to:', output)
        out_ds = gdal_array.SaveArray(output_ary,
                                      output,
                                      "gtiff",
                                      prototype=ds)
        del out_ds

    del ds  #close opened tif

    return W_hat_ary
def changeDetection(image1, image2, outputname):

    ar1 = gdalnumeric.LoadFile(image1).astype(np.int8)
    ar2 = gdalnumeric.LoadFile(image2)[1].astype(np.int8)

    diff = ar2 - ar1

    classes = np.histogram(diff, bins=5)[1]

    lut = [[0,0,0], [0,0,0], [0,0,0], [0,0,0], [0,255,0], [255,0,0]]
    start = 1

    rgb = np.zeros((3, diff.shape[0], diff.shape[1],), np.int8)
    for i in range(len(classes)):
        mask = np.logical_and(start <= diff, diff <= classes[i])
        for j in range(len(lut[i])):
            rgb[j] = np.choose(mask, (rgb[j], lut[i][j]))
        start = classes[i] + 1

    gdal_array.SaveArray(rgb, outputname, format="GTiff")
Example #30
0
def segment_by_scene(scene_name):
    csv_path = "data/csv/%s.csv" % scene_name
    with open(csv_path, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        index = 0
        for row in reader:
            # reading data
            filename = "%s_%d" % (scene_name, index)
            print("Processing", filename)

            d = int(row[2])
            if d == 1 or d == 2 or d == 3 or d == 6:
                # preparing result folders
                directory = 'results/D%d/%s' % (d, filename)
                if not os.path.exists(directory):
                    os.makedirs(directory)

                data, size_row, size_column, size_band = read_image(
                    filename, d)

                mode = [(0, 0, np.array([0])), (1, 1, np.array([0])),
                        (2, 2, np.array([0])), (3, 3, np.array([0])),
                        (4, 2, np.array([1, 2, 3, 4])),
                        (5, 2, np.array([0, 1, 2, 3, 4])),
                        (6, 2, np.array([0, 4]))]
                for alg, wmode, band_idx in mode:
                    """
					Stage 1: coarse candidates selection using abnormality threshold
					"""
                    abnormal, binary_img = calc_binary_img(
                        data, band_idx, size_column, size_row, wmode)

                    # save temp results
                    gdal_array.SaveArray(
                        abnormal, 'results/D%d/%s/%d.tif' % (d, filename, alg),
                        "GTiff")
                    cv2.imwrite('results/D%d/%s/%d.png' % (d, filename, alg),
                                binary_img)

            index += 1