Ejemplo n.º 1
0
def test_stack_invalid_out_paths_raise_errors():
    """ If users provide an output path that doesn't exist, raise error. """
    with pytest.raises(ValueError, match="not exist"):
        es.stack(
            band_paths=["fname1.tif", "fname2.tif"],
            out_path="nonexistent_directory/output.tif",
        )
def compositeImages(root_dir):
    # walk over directory to find scene packages
    regex = re.compile("^L.08")

    out_path_list = []

    directory_list = list()
    for root, dirs, files in os.walk(root_dir, topdown=False):
        for name in dirs:
            if regex.match(name):
                directory_list.append(os.path.join(root, name))

    image_path_list = []

    for folder in directory_list:
        image_path_list.append(natsort.natsorted(glob(
            os.path.join(folder, "*B*.TIF")
        )))

        composite_dir = folder + "/composite_folder"

        os.makedirs(composite_dir, exist_ok=True)

        out_raster_name = os.path.join(composite_dir, "composite_bands.tif")

        out_path_list.append(out_raster_name)

        for image in image_path_list:
            if str(image) not in out_raster_name:
                es.stack(image, out_raster_name)

    return out_path_list
Ejemplo n.º 3
0
def foret(inPathFeuillus, inPathConiferes, inPathInconnu, dir):
    # Création de Strings représentant les chemins vers de nouveaux fichiers créés.
    pathAll = os.path.join(dir, "NFI_MODIS250m_2011_kNN_SpeciesGroups_All_Spp_v1_resample_30.tif")
    pathClass = os.path.join(dir, "NFI_MODIS250m_2011_kNN_SpeciesGroups_Class_Spp_v1_resample_30.tif")

    # Si la fusion des trois rasters n'a pas déjà été faite.
    if not os.path.exists(pathAll):
        fileName = os.path.basename(pathAll)
        print("          Création du raster fusionné " + fileName + "...")

        # Création d'une liste des fichiers raster pertinents.
        list = [inPathFeuillus, inPathConiferes, inPathInconnu]

        # Fusionner les rasters contenus dans la liste.
        es.stack(list, pathAll)  # 2 = Feuillus, 1 = Conifères, 0 = Inconnu.

    # Si la classification n'a pas été faite.
    if not os.path.exists(pathClass):
        fileName = os.path.basename(pathClass)
        print("          Classification du raster " + fileName + "...")

        # Ouverture des données.
        rasterAll = gdal.Open(pathAll, gdal.GA_ReadOnly)
        rasterAllBand0 = rasterAll.GetRasterBand(1).ReadAsArray()
        rasterAllBand1 = rasterAll.GetRasterBand(2).ReadAsArray()
        rasterAllBand2 = rasterAll.GetRasterBand(3).ReadAsArray()

        for i in range(rasterAll.RasterYSize):
            for j in range(rasterAll.RasterXSize):
                if rasterAllBand0[i, j] == 0 and rasterAllBand1[i, j] == 0 and rasterAllBand2[i, j] == 0:  # Aucune forêt
                    rasterAllBand0[i, j] = 0

                elif rasterAllBand0[i, j] > 50:  # Feuillus
                    rasterAllBand0[i, j] = 1

                elif rasterAllBand1[i, j] > 50:  # Conifères
                    rasterAllBand0[i, j] = 2

                elif rasterAllBand2[i, j] > 50:  # Inconnu
                    rasterAllBand0[i, j] = 0

                else:  # Mixte
                    rasterAllBand0[i, j] = 3

        rasterClass = gdal.GetDriverByName("GTiff").Create(pathClass, rasterAll.RasterXSize, rasterAll.RasterYSize, 1)
        rasterClass.GetRasterBand(1).WriteArray(rasterAllBand0)

        rasterClass.SetProjection(rasterAll.GetProjection())
        rasterClass.SetGeoTransform(rasterAll.GetGeoTransform())
        rasterClass.FlushCache()

    return pathClass
Ejemplo n.º 4
0
def test_stack_nodata(in_paths, out_path):
    """Test nodata parameter for masking stacked array."""

    stack_arr, stack_prof = es.stack(in_paths, out_path, nodata=0)

    # basic_image has 91 0 values, which should be masked
    assert 0 not in stack_arr and np.sum(stack_arr.mask) == 91 * len(in_paths)
Ejemplo n.º 5
0
def test_stack_return_array(in_paths):
    """ Test returning only array."""

    # Test valid use case of just getting back the array.
    n_bands = len(in_paths)
    stack_arr, stack_prof = es.stack(in_paths)
    assert stack_arr.shape[0] == n_bands and stack_prof["count"] == n_bands
    def viewhist(self):                         # Function to display the histogram in the frame and save it in the specified directory
        if self.display.winfo_exists():
            self.display.grid_forget()
            self.display.destroy()
        
        self.display = ttk.Frame(self)
        self.display.grid(row = 0, column = 4,rowspan=2, sticky = 'nwes')
        frame=self.display

        band_path=[]
        for file in self.histfiles:
            band_path.append(file)
        
        # dir = self.dir[0]
        # band_path = glob.glob(os.path.join(dir,"*.tif"))
        # band_path = glob.glob(os.path.join(dir,"*.TIF"))
        band_path.sort()
        band_stack, meta_data = es.stack(band_path, nodata=-9999)
        size = len(band_path)
        color_list = ["Indigo","Blue","Green","Yellow","Red","Maroon","Purple","Violet"]
        titles = []
        for i in range(size):
            titles.append("File"+str(i+1))
        self.figure,self.plot = ep.hist(band_stack, title = titles,figsize = (6,4))
        self.canvas = FigureCanvasTkAgg(self.figure,frame)
        self.toolbar = NavigationToolbar2Tk(self.canvas,frame)
        self.toolbar.update()
        self.canvas.get_tk_widget().pack()
Ejemplo n.º 7
0
def test_stack_outputfile(in_paths, out_path):
    """Test successful creation of output file."""

    # Test valid use case specifying output file.
    stack_arr, stack_prof = es.stack(in_paths, out_path)

    assert os.path.exists(out_path)
Ejemplo n.º 8
0
def test_stack_nodata_outfile(in_paths, out_path):
    """Test nodata parameter for masking stacked array and writing output file."""

    stack_arr, stack_prof = es.stack(in_paths, out_path, nodata=0)

    # basic_image has 91 0 values, which should be masked
    assert 0 not in stack_arr and np.sum(stack_arr.mask) == 91 * len(in_paths)
    assert os.path.exists(out_path)
Ejemplo n.º 9
0
def test_stack_out_format(in_paths):
    """Test that output format matches input format."""

    # Test that the output file format is same as inputs
    # This can be flexible but for now forcing the same format
    out_fi = "test_stack.jp2"
    with pytest.raises(ValueError, match="Source"):
        stack_arr, stack_prof = es.stack(in_paths, out_path=out_fi)
Ejemplo n.º 10
0
    def create_stacked_image(self, HOME_DIR, stacked_file_output, paths_list):
        '''
            Generates stacked image from all bands
            void
            '''

        stack_data, metadata = es.stack(paths_list[:7],
                                        HOME_DIR + stacked_file_output)
Ejemplo n.º 11
0
def test_stack_no_file_ext(in_paths):
    """Test for error raised when no file extension provided in output file."""

    # Test that out_path needs a file extension to be valid
    out_fi = "test_stack"
    with pytest.raises(ValueError,
                       match="Please specify a valid file name for output."):
        stack_arr, stack_prof = es.stack(in_paths, out_path=out_fi)
    def create_stacked_image(self, HOME_DIR, stacked_file_output, paths_list):
        '''
            Generates stacked image from all bands
            void
            '''
        logging.info("Generating stacked image from bands")

        if os.path.isfile(HOME_DIR + stacked_file_output):

            logging.info("Stacked image already exists. Skipping")
        else:
            stack_data, metadata = es.stack(paths_list[:7],
                                            HOME_DIR + stacked_file_output)
Ejemplo n.º 13
0
    def create_raster_stack(self):
        """Stacks all provided raster files by the GIS object (gh) into a single raster stack, where each band
        corresponds to one environmental variable.

        :param self: a class instance of RasterStack

        :return: None. Does not return value or object, instead writes away the raster stack to a new (.tif) file.
        """

        for _ in tqdm.tqdm([0], desc='Creating raster stack' +
                           (29 * ' ')) if self.verbose else [0]:
            if not os.path.isdir(self.gh.stack):
                os.makedirs(self.gh.stack, exist_ok=True)

            # WARNING: this can cause errors if raster layers with different affine transformations / spatial extent or
            # resolutions are detected and used by the model, THIS ALSO INCLUDES THE FILE 'empty_land_map.tif'. This
            # means that the input raster files should match the spatial extent (Longitude_max = 180, Longitude_
            # min = -180, Latitude_max = 90, Latitude_min = -60) of the already existing 'empty_land_map.tif' included
            # in the repository. Alternatively the included 'empty_land_map.tif' can be edited to match the affine
            # transformation of the users raster files.

            es.stack(self.gh.variables,
                     self.gh.stack + '/stacked_env_variables.tif')
Ejemplo n.º 14
0
def test_stack_raster(basic_image_tif):
    """Unit tests for raster stacking with es.stack()."""

    # Create list of 4 basic_image_tif files (filepaths)
    band_files = [basic_image_tif] * 4

    # Test that out_path needs a file extension to be valid
    out_fi = "test_stack"
    with pytest.raises(ValueError,
                       match="Please specify a valid file name for output."):
        stack_arr, stack_prof = es.stack(band_files, out_path=out_fi)

    # Test that out_path needs a file extension to be valid
    out_fi = "test_stack.tif"
    with pytest.raises(ValueError, match="The list of"):
        stack_arr, stack_prof = es.stack([], out_path=out_fi)

    # Test that the output file format is same as inputs
    # This can be flexible but for now forcing the same format
    out_fi = "test_stack.jp2"
    with pytest.raises(ValueError, match="Source"):
        stack_arr, stack_prof = es.stack(band_files, out_path=out_fi)

    # Test valid use case specifying output file.
    # Make sure the output file exists and then clean it up
    out_fi = "test_stack.tif"
    stack_arr, stack_prof = es.stack(band_files, out_path=out_fi)

    assert os.path.exists(out_fi)
    if os.path.exists(out_fi):
        os.remove(out_fi)

    # Test valid use case of just getting back the array.
    stack_arr, stack_prof = es.stack(band_files)

    assert stack_arr.shape[0] == len(band_files)
    assert stack_prof["count"] == len(band_files)
Ejemplo n.º 15
0
 def showImage(self):  # Function to display the image in frame
     # directory = os.getcwd()
     # now = datetime.now()
     # dt_string = now.strftime("%d_%m_%Y_%H:%M:%S")
     # outdir = directory + '/img_STACK' + dt_string + '.tiff'
     outdir = 'OutputImages/' + self.out_name.get() + '.tiff'
     frame = self.frame
     self.band_fnames = [self.file1, self.file2, self.file3]
     arr_st, meta = es.stack(self.band_fnames, out_path=outdir, nodata=0)
     self.figure = Figure(figsize=(10, 6), dpi=100)
     self.plot = self.figure.add_subplot(1, 1, 1)
     ep.plot_rgb((arr_st), stretch=True, str_clip=0.5, ax=self.plot)
     self.canvas = FigureCanvasTkAgg(self.figure, frame)
     self.toolbar = NavigationToolbar2Tk(self.canvas, frame)
     self.toolbar.update()
     self.canvas.get_tk_widget().pack()
Ejemplo n.º 16
0
def test_stack_yes_file_ext(out_path):
    """Test for valid file extension."""

    # Test that out_path needs a file extension to be valid
    with pytest.raises(ValueError, match="The list of"):
        stack_arr, stack_prof = es.stack([], out_path=out_path)
Ejemplo n.º 17
0
import earthpy.plot as ep
import gdal
import sys
get_ipython().system('{sys.executable} -m pip install fastai')
import glob
from arcgis.gis import GIS
from arcgis.raster import ImageryLayer
from sentinelhub import SHConfig, MimeType, CRS, BBox, SentinelHubRequest, SentinelHubDownloadClient, DataSource, bbox_to_dimensions, DownloadRequest, BBoxSplitter, OsmSplitter, TileSplitter, CustomGridSplitter, UtmZoneSplitter, UtmGridSplitter
import itertools

# In[3]:

epaths = glob.glob("Sentinel/*.jp2")
epaths.sort()
epath = ("Sentinel/T11SMT_20200717T182921_B01_60m.jp2")
arr_stack, metadata = es.stack(epaths)
ep.plot_rgb(arr_stack, rgb=[4, 3, 1], stretch=True, figsize=(20, 20))
plt.savefig('edata')

# In[2]:

#sentinel2 processing
config = SHConfig()
CLIENT_SECRET = 'm*JW}?-76bBH)PjZp:-sW,3ISibK)mfh0GPc])n^'
CLIENT_ID = 'edb4f750-7cb2-475c-b190-3406e33de291'
config.sh_client_id = CLIENT_ID
config.sh_client_secret = CLIENT_SECRET

usa_bbox = -118.572693, 34.002581, -118.446350, 34.057211
resolution = 60
bbox = BBox(bbox=usa_bbox, crs=CRS.WGS84)
Ejemplo n.º 18
0
# -------------------
#
# To get started, make sure your directory is set. Then, create a stack from all of
# the Landsat .tif files (one per band). The nodata value for Landsat 8 is
# ``-9999`` so you can use the ``nodata=`` parameter when you call the
# ``stack()`` function.

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

# Stack the Landsat 8 bands
# This creates a numpy array with each "layer" representing a single band
landsat_path = glob(
    "data/vignette-landsat/LC08_L1TP_034032_20160621_20170221_01_T1_sr_band*_crop.tif"
)
landsat_path.sort()
arr_st, meta = es.stack(landsat_path, nodata=-9999)


###############################################################################
# Calculate Normalized Difference Vegetation Index (NDVI)
# -------------------------------------------------------
#
# You can calculate NDVI for your dataset using the
# ``normalized_diff`` function from the ``earthpy.spatial`` module.
# Math will be calculated (b1-b2) / (b1 + b2).

# Landsat 8 red band is band 4 at [3]
# Landsat 8 near-infrared band is band 5 at [4]
ndvi = es.normalized_diff(arr_st[4], arr_st[3])

Ejemplo n.º 19
0
raster_out_path = os.path.join(output_dir, "raster.tiff")

####################################################################################
# Stack the Bands
# ---------------------------
# The stack function has an optional output argument, where you can write the raster
# to a tiff file in a folder. If you want to use this functionality, make sure there
# is a folder to write your tiff file to.
# The Stack function also returns two object, an array and a RasterIO profile. Make
# sure to be catch both in variables.

# Stack Landsat bands

os.chdir(os.path.join(et.io.HOME, "earth-analytics"))
array, raster_prof = es.stack(stack_band_paths, out_path=raster_out_path)

####################################################################################
# Create Extent Object
# --------------------------------
# To get the raster extent, use the ``plotting_extent`` function on the
# array from ``es.stack()`` and the Rasterio profile or metadata object. The function
# needs a single
# layer of a numpy array, which is why we use ``arr[0]``. The function also
# needs the spatial transformation for the Rasterio object, which can be acquired by accessing
# the ``"transform"`` key within the Rasterio Profile.

extent = plotting_extent(array[0], raster_prof["transform"])

################################################################################
# Plot Un-cropped Data
# Next, you will load and plot landsat data. If you are completing the earth analytics course, you have worked with these data already in your homework.
#

# +
landsat_paths_pre_path = os.path.join(
    "data", "cold-springs-fire", "landsat_collect",
    "LC080340322016070701T1-SC20180214145604", "crop", "*band*.tif")

landsat_paths_pre = glob(landsat_paths_pre_path)
landsat_paths_pre.sort()

# Stack the Landsat pre fire data
landsat_pre_st_path = os.path.join("data", "cold-springs-fire", "outputs",
                                   "landsat_pre_st.tif")

es.stack(landsat_paths_pre, landsat_pre_st_path)

# Read landsat pre fire data
with rio.open(landsat_pre_st_path) as landsat_pre_src:
    landsat_pre = landsat_pre_src.read(masked=True)
    landsat_extent = plotting_extent(landsat_pre_src)

ep.plot_rgb(
    landsat_pre,
    rgb=[3, 2, 1],
    extent=landsat_extent,
    title=
    "Landsat True Color Composite Image | 30 meters \n Post Cold Springs Fire \n July 8, 2016"
)

plt.show()
Ejemplo n.º 21
0
###############################################################################
# Import Example Data
# ------------------------------
# To get started, make sure your directory is set. Create a stack from all of the
# Landsat .tif files (one per band) and import the ``landsat_qa`` layer which provides
# the locations of cloudy and shadowed pixels in the scene.

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

# Stack the landsat bands
# This creates a numpy array with each "layer" representing a single band
landsat_paths_pre = glob(
    "data/cold-springs-fire/landsat_collect/LC080340322016070701T1-SC20180214145604/crop/*band*.tif"
)
landsat_paths_pre.sort()
arr_st, meta = es.stack(landsat_paths_pre)

# Import the landsat qa layer
with rio.open(
        "data/cold-springs-fire/landsat_collect/LC080340322016070701T1-SC20180214145604/crop/LC08_L1TP_034032_20160707_20170221_01_T1_pixel_qa_crop.tif"
) as landsat_pre_cl:
    landsat_qa = landsat_pre_cl.read(1)
    landsat_ext = plotting_extent(landsat_pre_cl)

###############################################################################
# Plot Histogram of Each Band in Your Data
# ----------------------------------------
# You can view a histogram for each band in your dataset by using the
# ``hist()`` function from the ``earthpy.plot`` module.

ep.hist(arr_st)
Ejemplo n.º 22
0
def test_stack_array_size_mismatch(in_paths_mismatch):
    """ Test for error raised when array dimensions (nrows, ncols) are not all equal. """

    with pytest.raises(ValueError, match="same dimensions"):
        es.stack(in_paths_mismatch)
Ejemplo n.º 23
0
#
# To get started, make sure your directory is set. Then, create a stack from all
# of the Landsat .tif files (one per band).

# Get data for example
data = et.data.get_data("vignette-landsat")

# Set working directory
os.chdir(os.path.join(et.io.HOME, "earth-analytics"))

# Stack the Landsat 8 bands
# This creates a numpy array with each "layer" representing a single band
# You can use the nodata parameter to mask nodata values
landsat_path = glob(os.path.join("data", "vignette-landsat", "*band*.tif"))
landsat_path.sort()
array_stack, meta_data = es.stack(landsat_path, nodata=-9999)

###############################################################################
# Plot All Histograms in a Stack With Custom Titles and Colors
# -------------------------------------------------------------
#
# You can create histograms for each band with unique colors and titles by
# first creating a list of colors and titles that will be provided to the
# ep.hist() function. The list for titles must contain the same number of
# strings as there are bands in the stack. You can also modify the colors for
# each image with a list of Matplotlib colors. If one color is provided in
# the list, every band will inherit that color. Otherwise, the list must
# contain the same number of strings as there are bands in the stack.

# Create the list of color names for each band
colors_list = [
Ejemplo n.º 24
0
def test_stack_Affine_mismatch(in_paths_Affine_mismatch):
    """ Test for error raised when raster Affine transform are not all equal. """

    with pytest.raises(ValueError, match="same affine transform"):
        es.stack(in_paths_Affine_mismatch)
Ejemplo n.º 25
0
def test_stack_CRS_mismatch(in_paths_CRS_mismatch):
    """ Test for error raised when raster CRS are not all equal. """

    with pytest.raises(ValueError, match="same CRS"):
        es.stack(in_paths_CRS_mismatch)
Ejemplo n.º 26
0
#       If you are running this script on a Windows system, there is a
#       known bug with ``.to_crs()``, which is used in this script. If an error
#       occurs, you have to reset your os environment with the command
#       ``os.environ["PROJ_LIB"] = r"path-to-share-folder-in-environment"``.

# Get sample data from EarthPy and set working directory
data_path = et.data.get_data("vignette-landsat")
os.chdir(os.path.join(et.io.HOME, "earth-analytics"))

# Get list of bands and sort by ascending band number
landsat_bands_data_path = "data/vignette-landsat/LC08_L1TP_034032_20160621_20170221_01_T1_sr_band*[1-7]*.tif"
stack_band_paths = glob(landsat_bands_data_path)
stack_band_paths.sort()

# Create image stack and apply nodata value for Landsat
arr_st, meta = es.stack(stack_band_paths, nodata=-9999)

###############################################################################
# Plot RGB Composite Image
# --------------------------
# You can use the ``plot_rgb()`` function from the ``earthpy.plot`` module to quickly
# plot three band composite images. For RGB composite images, you will plot the red,
# green, and blue bands, which are bands 4, 3, and 2, respectively, in the image
# stack you created. Python uses a zero-based index system, so you need to subtract
# a value of 1 from each index. Thus, the index for the red band is 3, green is 2,
# and blue is 1. These index values are provided to the ``rgb`` argument to identify
# the bands for the composite image.

# Create figure with one plot
fig, ax = plt.subplots(figsize=(12, 12))
    'C:/Users/Hadi Askari/Desktop/Sentinel 2/S2A_MSIL1C_20200301T054741_N0209_R048_T43SCT_20200301T085541.SAFE/GRANULE/L1C_T43SCT_A024498_20200301T055331/IMG_DATA/FaiziGreen.tif',
    'C:/Users/Hadi Askari/Desktop/Sentinel 2/S2A_MSIL1C_20200301T054741_N0209_R048_T43SCT_20200301T085541.SAFE/GRANULE/L1C_T43SCT_A024498_20200301T055331/IMG_DATA/FaiziBlue.tif'
]

croppedbands = [rgbjp2s[0], rgbjp2s[1], rgbjp2s[2]]

outputstackpath = 'C:/Users/Hadi Askari/Desktop/Sentinel 2/S2A_MSIL1C_20200301T054741_N0209_R048_T43SCT_20200301T085541.SAFE/GRANULE/L1C_T43SCT_A024498_20200301T055331/IMG_DATA/stack.tif'

with rio.open(rgbjp2s[0]) as f:
    checkred = f.read(1, masked=True)
with rio.open(rgbjp2s[1]) as f:
    checkgreen = f.read(1, masked=True)
with rio.open(rgbjp2s[2]) as f:
    checkblue = f.read(1, masked=True)

croppedbands_stack, croppedbands_meta = es.stack(croppedbands, outputstackpath)
print(croppedbands_stack.shape)

imgred = cv2.normalize(checkred,
                       0,
                       0,
                       255,
                       norm_type=cv2.NORM_MINMAX,
                       dtype=cv2.CV_8UC1)
equred = cv2.equalizeHist(imgred)

imggreen = cv2.normalize(checkgreen,
                         0,
                         0,
                         255,
                         norm_type=cv2.NORM_MINMAX,