Example #1
0
def test_crop_all_with_non_overlapping_geom(in_paths, output_dir):
    """Test crop all if extents don't overlap. """
    bad_geom = Polygon(
        [(12, 12), (12, 14.25), (14.25, 14.25), (14.25, 12), (12, 12)]
    )
    with pytest.raises(ValueError, match="Input shapes do not ov"):
        es.crop_all(in_paths, output_dir, [bad_geom], overwrite=True)
Example #2
0
def test_crop_all_returns_list(in_paths, output_dir, basic_geometry_gdf):
    """Test that crop all returns a list. """
    img_list = es.crop_all(in_paths,
                           output_dir,
                           basic_geometry_gdf,
                           overwrite=True)
    assert type(img_list) == list
Example #3
0
def test_crop_all_files_exist(in_paths, output_dir, basic_geometry_gdf):
    """Test that crop all actually creates the files in the directory. """
    img_list = es.crop_all(
        in_paths, output_dir, basic_geometry_gdf, overwrite=True
    )
    for files in img_list:
        assert os.path.exists(files)
Example #4
0
def test_crop_all_verbose(in_paths, output_dir, basic_geometry_gdf):
    """Test that when verbose is set to false, nothing is returned. """
    out_list = es.crop_all(in_paths,
                           output_dir,
                           basic_geometry_gdf,
                           overwrite=True,
                           verbose=False)
    assert out_list is None
Example #5
0
def test_crop_all_returns_list_of_same_len(in_paths, output_dir,
                                           basic_geometry_gdf):
    """Test that crop all returns a list of the same length as the input list. """
    img_list = es.crop_all(in_paths,
                           output_dir,
                           basic_geometry_gdf,
                           overwrite=True)
    assert len(img_list) == len(in_paths)
Example #6
0
    def rasterize_labels(self):
        with rasterio.open(self.reference_file) as src0:
            meta = src0.meta
            meta['nodata'] = 0.0
            meta['dtype'] = 'uint16'

        class_array = utils.vector_to_raster(self.saving_path,
                                             self.vector_path,
                                             self.reference_file,
                                             self.ObjectID)
        class_array = self.binary_erosion(class_array)

        label_array = utils.vector_to_raster(self.saving_path,
                                             self.vector_path,
                                             self.reference_file, self.LabelID)
        label_array = self.binary_erosion(label_array)

        path_class = os.path.join(self.saving_path, self.ObjectID + '.tif')
        path_labels = os.path.join(self.saving_path, self.LabelID + '.tif')

        with rasterio.open(path_class, 'w', **meta) as dst:
            dst.write_band(1, class_array.astype(np.uint16))

        with rasterio.open(path_labels, 'w', **meta) as dst:
            dst.write_band(1, label_array.astype(np.uint16))

        paths_output = [path_class, path_labels]
        extent = gpd.read_file(self.extent_vector)

        es.crop_all(paths_output,
                    self.saving_path,
                    extent,
                    overwrite=True,
                    all_touched=True,
                    verbose=True)
        os.remove(path_class)
        os.remove(path_labels)
Example #7
0
    def crop_images(self):

        extent = gpd.read_file(self.extent_vector)

        paths_list = [
            os.path.join(self.saving_path, tif_file)
            for tif_file in list(os.listdir(self.saving_path))
            if tif_file.split('.')[-1] == 'tif'
            and ~np.any([x in tif_file for x in ['crop']])
        ]

        es.crop_all(paths_list,
                    self.saving_path,
                    extent,
                    overwrite=True,
                    all_touched=True,
                    verbose=True)  #

        for path in paths_list:
            try:
                os.remove(path)
            except:
                script = "sudo rm " + path
                subprocess.call(script, shell=True)
Example #8
0
def test_crop_all_with_geoms(in_paths, output_dir, basic_geometry):
    """Test crop all works with geoms instead of a gdf. """
    test = es.crop_all(in_paths, output_dir, [basic_geometry], overwrite=True)
    assert isinstance(test, list)
Example #9
0
def test_crop_all_fails_bad_dir(in_paths, basic_geometry_gdf):
    """Test crop all fails if user provides a bad directory path. """
    bad_path = "Bad/Path"
    with pytest.raises(ValueError, match="The output directo"):
        es.crop_all(in_paths, bad_path, basic_geometry_gdf, overwrite=True)
Example #10
0
def test_crop_all_fails_overwrite(in_paths, output_dir, basic_geometry_gdf):
    """Test that crop all fails when overwrite isn't set to True if files
    already exist. """
    with pytest.raises(ValueError, match="The file "):
        es.crop_all(in_paths, output_dir, basic_geometry_gdf)
    crop_bound_utm13N = crop_bound.to_crs(crop_raster_profile["crs"])

#############################################################################
# Crop Each Band
# --------------
# When you need to crop and stack a set of images, it is  most efficient to first
# crop each image, and then stack it.
# ``es.crop_all()`` is an efficient way to crop all bands in an image quickly.
# The function will write out cropped rasters to a
# directory and return a list of file paths that can then be used with
# ``es.stack()``.

os.chdir(os.path.join(et.io.HOME, "earth-analytics"))

band_paths_list = es.crop_all(
    stack_band_paths, output_dir, crop_bound_utm13N, overwrite=True
)

#############################################################################
# Stack All Bands
# ---------------
# Once the data are cropped, you are ready to create a new stack.

os.chdir(os.path.join(et.io.HOME, "earth-analytics"))

cropped_array, array_raster_profile = es.stack(band_paths_list, nodata=-9999)
crop_extent = plotting_extent(
    cropped_array[0], array_raster_profile["transform"]
)

# Plotting the cropped image