Beispiel #1
0
def Sentinel2_3a_2018(return_dates=False, return_random_sample=False):
    """
    Sentinel2 sample dataset on Bouconne Forest (France, near Toulouse).
    Bands are ordered this way : '2','3','4','8','5','6','7','8','8A','11','12'.

    Parameters
    -----------
    return_dates : bool, default False
        If True, will return list of dates.
    return_random_sample : bool, default False
        If False, will return the path of the raster
        If True, will return a random block of the image

    Returns
    -------
    raster : str or array
        If get_only_sample is False, returns path of raster.
        If get_only_sample is True, returns array where each line is a pixel.
    dates : list
        List of integer


    Examples
    --------
    >>> raster,dates = Sentinel2_3a_2018(return_dates=True)
    >>> print(raster)
    /mnt/bigone/lib/MuseoPheno/museopheno/datasets/2018_3A_Theia_Bouconne.tif
    >>> print(dates)
    [20180429, 20180513, 20180708, 20180815, 20180915, 20181015, 20181115]
    >>> Sentinel2_3a_2018(get_only_sample=True)
    Total number of blocks : 246
    array([[ 122,  320,  109, ..., 2107, 1530,  751],
       [ 140,  370,  122, ..., 2107, 1530,  751],
       [ 148,  388,  117, ..., 2102, 1557,  761],
       ...,
       [ 167,  459,  195, ..., 2251, 1482,  664],
       [ 154,  470,  185, ..., 2251, 1482,  664],
       [ 184,  494,  213, ..., 2429, 1507,  670]], dtype=int16)

    References
    -----------
    This dataset is built using level 3A Sentinel-2 month syntheses :
    https://labo.obs-mip.fr/multitemp/sentinel-2-level-3a-products-syntheses-of-composites/
    """

    raster = os.path.join(__pathFile, '2018_3A_Theia_Bouconne.tif')
    dates = [
        20180429, 20180513, 20180708, 20180815, 20180915, 20181015, 20181115
    ]
    if return_random_sample is True:
        from museotoolbox.processing import RasterMath
        raster = RasterMath(raster).get_random_block()
    if return_dates:
        return raster, dates
    else:
        return raster
Beispiel #2
0
# -------------------------------------------

from museotoolbox.processing import RasterMath
from museotoolbox import datasets
import numpy as np
##############################################################################
# Load HistoricalMap dataset
# -------------------------------------------

raster, vector = datasets.load_historical_data()

##############################################################################
# Initialize rasterMath with raster
# ------------------------------------

rM = RasterMath(raster)

print(rM.get_random_block())

##########################
# Let's suppose you want compute the difference between blue and green band.
# I suggest you to define type in numpy array to save space while creating the raster!

X = rM.get_random_block()

sub = lambda X: np.array((X[:, 0] - X[:, 1])).astype(np.int16)

rM.add_function(sub, out_image='/tmp/sub_lambda.tif')
###########################################################
# Use a python function to use arguments
# ----------------------------------------
    X1 = S2.generate_index(X,
                           S2.get_index_expression('LChloC'),
                           multiply_by=100,
                           dtype=np.int16)
    X2 = S2.generate_index(X,
                           S2.get_index_expression('ACORVI'),
                           multiply_by=100,
                           dtype=np.int16)

    return np.hstack((X, X1, X2)).astype(np.int16)


#########################################
# Use rasterMath to read and write block per block the raster according to a function

rM = RasterMath(raster)

X = rM.get_random_block()
print('Block has {} pixels and {} bands'.format(X.shape[0], X.shape[1]))

################################
# Now we can try our function

XwithIndex = createSITSplusIndex(X)
print('Raster+index produced has {} pixels and {} bands'.format(
    XwithIndex.shape[0], XwithIndex.shape[1]))

##################################
# Now we add our function as the test was a success
rM.add_function(createSITSplusIndex, '/tmp/SITSwithIndex.tif')
Beispiel #4
0
    def __init__(self, in_image, in_image_mask=False,
                 lag=1, weights=False):
        """
        Compute Moran's I for raster.

        Parameters
        ----------
        in_image : str.
            A filename or path of a raster file.
            It could be any file that GDAL can open.
        lag : int, optional (default=1)
            The distance from the cell.
        weights :False or array-like, optional (default=False).
            Weights with the same shape as the square size.
        """

        self.scores = dict(lag=[], I=[], band=[], EI=[])
        self.lags = []

        rM = RasterMath(
            in_image,
            in_image_mask=in_image_mask,
            return_3d=True,
            verbose=False)

        for band, arr in enumerate(rM.read_band_per_band()):
            if isinstance(lag, int):
                lag = [lag]
            for l in lag:
                squareSize = l * 2 + 1
                footprint = np.ones((squareSize, squareSize))
                weights = np.zeros((footprint.shape[0], footprint.shape[1]))

                weights[:, 0] = 1
                weights[0, :] = 1
                weights[-1, :] = 1
                weights[:, -1] = 1

                n = arr.count()
                arr = arr.astype(np.float64)
                # convert masked to nan for generic_filter
                np.where(arr.mask, np.nan, arr.data)

                x_ = np.nanmean(arr)

                num = generic_filter(
                    arr,
                    self._compute_view_for_global_moran,
                    footprint=footprint,
                    mode='constant',
                    cval=np.nan,
                    extra_keywords=dict(
                        x_=x_,
                        footprint=footprint,
                        weights=weights,
                        transform='r'))

                den = (arr - x_)**2
                self.z = arr - x_

                # need to mask z/den/num/neighbors
                den[arr.mask] = np.nan
#                local = np.nansum(num) / np.nansum(den)
                l1 = np.where(arr.mask, np.nan, num)
                l2 = np.where(arr.mask, np.nan, den)
                local = np.nansum(l1) / np.nansum(l2)

                self.I = local

                self.EI = -1 / (n - 1)
                self.lags.append(l)
                self.scores['lag'].append(l)
                self.scores['band'].append(band + 1)
                self.scores['I'].append(self.I)
                self.scores['EI'].append(self.EI)
Beispiel #5
0
##############################################################################
# Load HistoricalMap dataset
# -------------------------------------------

raster, vector = datasets.load_historical_data()

##############################################################################
# Initialize rasterMath with raster
# ------------------------------------

# Set return_3d to True to have full block size (not one pixel per row)
# Create raster mask to only keep pixel inside polygons.

image_mask_from_vector(vector, raster, '/tmp/mask.tif', invert=False)

rM = RasterMath(raster, in_image_mask='/tmp/mask.tif', return_3d=True)
#rM.addInputRaster('/tmp/mask.tif')
print(rM.get_random_block().shape)

#######################
# Plot blocks
x = rM.get_random_block()

rM.add_function(np.mean, '/tmp/mean.tif', axis=2, out_np_dt=np.int16)

rM.run()

from osgeo import gdal
dst = gdal.Open('/tmp/mean.tif')
arr = dst.GetRasterBand(1).ReadAsArray()
plt.imshow(np.ma.masked_where(arr == rM._outputs[0]['nodata'], arr))
Beispiel #6
0
print(S2.available_indices.keys())

###########################################################
# Write metadata in each band (date + band name)
# ------------------------------------------------------

###########################################################
# This is useful to show date in band number in Qgis

S2.set_description_metadata(raster, dates)

###########################################################
# Produce NDVI time series from and to a raster
# ----------------------------------------------
S2.generate_raster(input_raster=raster,
                   output_raster='/tmp/index.tif',
                   expression=S2.get_index_expression('NDVI'),
                   dtype=np.float32)

##############################
# Plot NDVI index
from museotoolbox.processing import RasterMath
from matplotlib import pyplot as plt

rM = RasterMath('/tmp/index.tif')
NDVI = rM.get_random_block()  #randomly select a block
from datetime import datetime
dateToDatetime = [datetime.strptime(str(date), '%Y%m%d') for date in dates]
plt.plot_date(dateToDatetime, NDVI[:10, :].T, '-o')
plt.ylabel('NDVI')
from museotoolbox import datasets
from matplotlib import pyplot as plt

##############################################################################
# Load HistoricalMap dataset
# -------------------------------------------

raster, vector = datasets.load_historical_data()

##############################################################################
# Initialize rasterMath with raster
# ------------------------------------

# Set return3d to True to have full block size (not one pixel per row)

rM = RasterMath(raster, return_3d=True)

print(rM.get_random_block().shape)

##############################################################################
# Comparing different block size (%, fixed, full block)
# -------------------------------------------------------

#######################
# You can define block by percentage of the whole width/height

rM.custom_block_size(1 / 2, 1 / 2)
print(rM.get_random_block().shape)

#######################
# Or by fixed window
Beispiel #8
0
def load_pottoks(return_X_y=True, return_target=True, return_only_path=False):
    """
    Load two images of pottoks.

    Parameters
    -----------
    return_X_y : bool, optional (default=True)
        If True, will return the array in 2d where there are labels (X) and the labels (y)
        If False, will return only X in 3d (the array of the image)
    return_target : bool, optional (default=True)
        If True, will return two arrays in a list
        If False, will return only the source (a brown pottok)

    Examples
    --------
    >>> brown_pottok_arr, brown_pottok_label = load_pottoks(return_target=False)
    >>> brown_pottok_arr.shape
    (4610, 3)
    >>> brown_pottok_label.shape
    (4610,)
    """
    brown_pottok_uri = os.path.join(
        __pathFile,
        'brownpottok')
    black_pottok_uri = os.path.join(
        __pathFile,
        'blackpottok')

    to_return = []
    
    if return_only_path :
        to_return.extend([brown_pottok_uri + '.tif', brown_pottok_uri + '.gpkg'])  
        if return_target : 
            to_return.extend([black_pottok_uri + '.tif', black_pottok_uri + '.gpkg'])  

        
        
    elif return_X_y:
        Xs, ys = extract_ROI(brown_pottok_uri + '.tif',
                             brown_pottok_uri + '.gpkg', 'level')
        to_return.extend([Xs, ys])
        


        if return_target:
            Xt, yt = extract_ROI(
                black_pottok_uri + '.tif', black_pottok_uri + '.gpkg', 'level')
            to_return.extend([Xt, yt])

    elif return_X_y is False:
        brown_pottok_arr = RasterMath(
            brown_pottok_uri + '.tif',
            return_3d=True,
            verbose=False).get_image_as_array()

        to_return.append(brown_pottok_arr)
        if return_target:
            black_pottok_arr = RasterMath(
                black_pottok_uri + '.tif',
                return_3d=True,
                verbose=False).get_image_as_array()
            to_return.append(black_pottok_arr)

    return to_return