示例#1
0
def change_from_composite(image_path, composite_path, model_path, class_out_path, prob_out_path=None):
    """
    Stacks an image with a composite and classifies each pixel change with a scikit-learn model
    The image that is classified is has the following bands

    1. composite blue
    2. composite green
    3. composite red
    4. composite IR
    5. image blue
    6. image green
    7. image red
    8. image IR

    Parameters
    ----------
    image_path
        The path to the image
    composite_path
        The path to the composite
    model_path
        The path to a .pkl of a scikit-learn classifier that takes 8 features
    class_out_path
        A location to save the resulting classification .tif
    prob_out_path
        A location to save the probability raster of each pixel


    """
    with TemporaryDirectory() as td:
        stacked_path = os.path.join(td, "comp_stack.tif")
        stack_images((composite_path, image_path), stacked_path)
        classify_image(stacked_path, model_path, class_out_path, prob_out_path)
def create_stack_image(working_dir, path_to_s1_image):
    os.chdir(working_dir)
    for image in os.listdir("images/stacked/with_indices"):
        if image.endswith(".tif"):
            path_to_image = os.path.join("images/stacked/with_indices", image)
            path_to_brightness = os.path.join("segmentation/brightness", image)
            ras.stack_images(
                [path_to_image, path_to_s1_image, path_to_brightness],
                os.path.join("images/stacked/with_s1_seg", image))
示例#3
0
def test_windows_monkeypatch(monkeypatch):
    # monkeypatch.setattr(sys,'platform','win32')
    out_file = r"test_outputs\windows_stack.tif"
    try:
        os.remove(out_file)
    except FileNotFoundError:
        pass
    from pyeo import raster_manipulation as ras
    test_rasters = [
        r"test_data\class_reproj.tif", r"test_data\composite_reproj.tif"
    ]
    ras.stack_images(test_rasters, out_file)
    out = gdal.Open(out_file)
    out_array = out.ReadAsArray()
    assert out_array.max() > 0
示例#4
0
def test_windows_stacking(monkeypatch):
    monkeypatch.setattr(pyeo.windows_compatability, 'WINDOWS_PREFIX', '')
    out_file = r"test_outputs/windows_stack.tif"
    try:
        os.remove(out_file)
    except FileNotFoundError:
        pass
    from pyeo import raster_manipulation as ras
    test_rasters = [
        r"test_data/indonesia_s2_image.tif",
        r"test_data/indonesia_s2_l1_image.tif"
    ]
    ras.stack_images(test_rasters, out_file)
    out = gdal.Open(out_file)
    out_array = out.ReadAsArray()
    assert out_array.max() > 0
示例#5
0
def build_cocoa_map(working_dir,
                    path_to_aoi,
                    start_date,
                    end_date,
                    path_to_s1_image,
                    path_to_config,
                    epsg_for_map,
                    path_to_model,
                    cloud_cover=20,
                    log_path="build_cocoa_map.log",
                    use_sen2cor=False,
                    sen2cor_path=None,
                    skip_download_and_preprocess=False,
                    skip_composite=False):

    # Step 0: Get things ready. Folder structure for downloads, load credentials from config.
    fu.init_log(log_path)
    os.chdir(working_dir)
    # fu.create_file_structure(os.getcwd())
    #
    # make_directory("images/merged/10m")
    # make_directory("images/merged/20m")
    #
    # make_directory("images/stacked/with_indices")
    # make_directory("images/stacked/with_s1_seg")
    # make_directory("images/stacked/all_19bands")
    #
    # make_directory("segmentation")
    # make_directory("composites")
    # make_directory("composites/10m")
    # make_directory("composites/20m")
    #
    # make_directory("composites/10m_full")
    # make_directory("composites/20m_full")
    general_functions.make_all_dirs(working_dir)

    # Step 1: Download S2 3imagery for the timescale
    if not skip_download_and_preprocess:
        config = configparser.ConfigParser()
        config.read(path_to_config)

        images_to_download = query.check_for_s2_data_by_date(
            path_to_aoi, start_date, end_date, config, cloud_cover)
        if not use_sen2cor:
            images_to_download = query.filter_non_matching_s2_data(
                images_to_download)
        else:
            images_to_download = query.filter_to_l1_data(images_to_download)
        query.download_s2_data(images_to_download, "images/L1", "images/L2")

        # Step 2: Preprocess S2 imagery. Perform atmospheric correction if needed, stack and mask 10 and 20m bands.
        if use_sen2cor:
            ras.atmospheric_correction("images/L1"
                                       "images/L2",
                                       sen2cor_path=sen2cor_path)
        ras.preprocess_sen2_images("images/L2",
                                   "images/merged/10m",
                                   "images/L1",
                                   cloud_threshold=0,
                                   epsg=epsg_for_map,
                                   bands=("B02", "B03", "B04", "B08"),
                                   out_resolution=10)
        ras.preprocess_sen2_images("images/L2",
                                   "images/merged/20m",
                                   "images/L1",
                                   cloud_threshold=0,
                                   epsg=epsg_for_map,
                                   bands=("B02", "B03", "B04", "B05", "B06",
                                          "B07", "B8A", "B11", "B12"),
                                   out_resolution=20)

    if not skip_composite:
        # Step 2.5: Build a pair of cloud-free composites
        sort_into_tile("images/merged/10m")
        sort_into_tile("images/merged/20m")

        for tile in os.listdir("images/merged/10m"):
            tile_path = os.path.join("images/merged/10m", tile)
            this_composite_path = ras.composite_directory(
                tile_path, "composites/10m")
            new_composite_path = "{}_{}.tif".format(
                this_composite_path.rsplit('.')[0], tile)
            os.rename(this_composite_path, new_composite_path)

        for tile in os.listdir("images/merged/20m"):
            tile_path = os.path.join("images/merged/20m", tile)
            this_composite_path = ras.composite_directory(
                tile_path, "composites/20m")
            new_composite_path = "{}_{}.tif".format(
                this_composite_path.rsplit('.')[0], tile)
            os.rename(this_composite_path, new_composite_path)

    # Step 3: Generate the bands. Time for the New Bit.
    clip_to_aoi = False
    if clip_to_aoi:
        for image in os.listdir("composites/10m_full"):
            if image.endswith(".tif"):
                image_path_10m_full = os.path.join("composites/10m_full",
                                                   image)
                image_path_20m_full = os.path.join("composites/20m_full",
                                                   image)

                image_path_10m_clipped = os.path.join("composites/10m", image)
                image_path_20m_clipped = os.path.join("composites/20m", image)

                # config = configparser.ConfigParser()
                # conf = config.read(path_to_config)
                # print(conf)
                # aoi = config['cocoa_mapping']['path_to_aoi']

                aoi = "/media/ubuntu/Data/Ghana/cocoa_big/shp/cocoa_big.shp"

                ras.clip_raster(raster_path=image_path_10m_full,
                                aoi_path=aoi,
                                out_path=image_path_10m_clipped,
                                srs_id=32630)
                ras.clip_raster(raster_path=image_path_20m_full,
                                aoi_path=aoi,
                                out_path=image_path_20m_clipped,
                                srs_id=32630)

    do_segmentation = True
    if do_segmentation == True:
        for image in os.listdir("composites/10m"):
            if image.endswith(".tif"):
                with TemporaryDirectory() as td:
                    image_path_10m = os.path.join("composites/10m", image)
                    image_path_20m = os.path.join("composites/20m", image)
                    resample_path_20m_v1 = os.path.join(
                        td,
                        image)  # You'll probably regret this later, roberts.
                    shutil.copy(image_path_20m, resample_path_20m_v1)
                    ras.resample_image_in_place(resample_path_20m_v1, 10)

                    index_image_path = os.path.join(td, "index_image.tif")
                    temp_pre_seg_path = os.path.join(td, "pre_seg.tif")
                    temp_seg_path = os.path.join(td, "seg.tif")
                    temp_shp_path = os.path.join(td, "outline.shp")
                    temp_clipped_seg_path = os.path.join(td, "seg_clip.tif")

                    # This bit's your show, Qing
                    temp_s1_outline_path = os.path.join(td, "s1_outline.shp")
                    ras.get_extent_as_shp(in_ras_path=image_path_10m,
                                          out_shp_path=temp_s1_outline_path)

                    resample_path_20m = os.path.join(
                        td, image[:-4] + '_to_10moutline.tif')
                    general_functions.clip_rst(
                        in_tif=resample_path_20m_v1,
                        outline_shp=temp_s1_outline_path,
                        out_tif=resample_path_20m,
                        keep_rst_extent=False)

                    generate_veg_index_tif(image_path_10m, resample_path_20m,
                                           index_image_path)
                    ras.stack_images([index_image_path, image_path_10m],
                                     "images/stacked/with_indices/" + image)

                    # Now, we do Highly Experimental Image Segmentation. Please put on your goggles.
                    # SAGA, please.
                    # Meatball science time
                    vis_10m = gdal.Open(image_path_10m)
                    vis_20m_resampled = gdal.Open(resample_path_20m)
                    vis_10m_array = vis_10m.GetVirtualMemArray()
                    vis_20m_array = vis_20m_resampled.GetVirtualMemArray()
                    # NIR, SWIR, red
                    array_to_classify = np.stack([
                        vis_10m_array[3, ...], vis_20m_array[7, ...],
                        vis_10m_array[2, ...]
                    ])

                    g, arr = general_functions.read_tif(intif=image_path_10m)
                    general_functions.create_tif(filename=temp_pre_seg_path,
                                                 g=g,
                                                 Nx=arr.shape[1],
                                                 Ny=arr.shape[2],
                                                 new_array=array_to_classify,
                                                 noData=0,
                                                 data_type=gdal.GDT_UInt32)
                    out_segment_tif = os.path.join("segmentation", image)
                    segment_image(temp_pre_seg_path, out_segment_tif)

                    print('Generate brighness raster from the segments')
                    make_directory("segmentation/brightness")

                    out_brightness_value_ras = os.path.join(
                        "segmentation/brightness", image)
                    output_filtered_value_ras = False

                    ras.get_extent_as_shp(in_ras_path=temp_pre_seg_path,
                                          out_shp_path=temp_shp_path)

                    general_functions.clip_rst(in_tif=out_segment_tif,
                                               outline_shp=temp_shp_path,
                                               out_tif=temp_clipped_seg_path,
                                               keep_rst_extent=False)

                    cal_seg_mean(
                        temp_pre_seg_path,
                        temp_clipped_seg_path,
                        out_brightness_value_ras,
                        output_filtered_value_ras=output_filtered_value_ras)

                # image_20m_6bands_array = vis_20m_array[3:,:,:]
                # try:
                #     os.mkdir("composites/20m/20m_6bands")
                # except FileExistsError:
                #     pass
                #
                # out_20m_tif_for_stack = os.path.join("composites/20m/20m_6bands", image)
                # general_functions.create_tif(filename=out_20m_tif_for_stack,g=g,Nx=arr.shape[1],Ny=arr.shape[2],
                #                              new_array=image_20m_6bands_array,data_type=gdal.GDT_UInt16,noData=0)

    do_stack = True
    if do_stack == True:
        # Step 4: Stack the new bands with the S1, seg, and 6 band 20m rasters
        for image in os.listdir("images/stacked/with_indices"):
            if image.endswith(".tif"):
                path_to_image = os.path.join("images/stacked/with_indices",
                                             image)
                path_to_brightness_image = os.path.join(
                    "segmentation/brightness", image)
                # path_to_20m_image = os.path.join("composites/20m/20m_6bands", image)
                # ras.stack_images([path_to_image, path_to_s1_image,path_to_20m_image,out_brightness_value_ras], os.path.join("images/stacked/all_19bands", image))
                ras.stack_images([
                    path_to_image, path_to_s1_image, path_to_brightness_image
                ], os.path.join("images/stacked/with_s1_seg", image))

    #sys.exit()
    #
    # Step 5: Classify with trained model
    for image in os.listdir("images/stacked/with_s1_seg"):
        if image.endswith(".tif"):
            path_to_image = os.path.join("images/stacked/with_s1_seg", image)
            path_to_out = os.path.join("output", image)
            PYEO_model.classify_image(path_to_image, path_to_model,
                                      path_to_out)