Exemplo n.º 1
0
def start():
    user_input = input('Enter file path for first_band: ')
    # Open First image and get its only band.
    # first_tiff = Open(r'51/AST_L1B_00308132001191022_20110122163617_24451_ImageData1_b30de2ff.tif')
    first_tiff = Open(user_input)
    first_band = first_tiff.GetRasterBand(1)

    user_input = input('Enter file path for second_band of same image: ')
    # Open Second image and get its only band.
    # second_tiff = Open(r'51/AST_L1B_00308132001191022_20110122163617_24451_ImageData2_b30de2ff.tif')
    second_tiff = Open(user_input)
    second_band = second_tiff.GetRasterBand(1)

    # Release from memory
    user_input = None

    # Get the rows and cols from one of the images (both should always be the same)
    rows, cols, geotransform = first_tiff.RasterYSize, first_tiff.RasterXSize, first_tiff.GetGeoTransform()
    print(geotransform)

    # Set an output for a 16-bit unsigned integer (0-255)
    out_tiff_int16 = r'TEST-RESULTS/NDVI_INT16.tif'

    # Set the output for a 32-bit floating point (-1 to 1)
    out_tiff_float32 = r'TEST-RESULTS/NDVI_FLOAT32.tif'

    # Run the function for unsigned 16-bit integer
    ndwi(first_band, second_band, rows, cols, geotransform, out_tiff_int16, gdal.GDT_UInt16)

    # Run the function for 32-bit floating point
    ndwi(first_band, second_band, rows, cols, geotransform, out_tiff_float32, gdal.GDT_Float32)

    print('done')
Exemplo n.º 2
0
def main():

    if len(sys.argv) < 6:
        print "Few arguments"
        sys.exit()

    elif len(sys.argv) > 6:
        print "Too many arguments"
        sys.exit()

    #Tiff paths
    red_path = str(sys.argv[1])
    nir_path = str(sys.argv[2])
    bqa_path = str(sys.argv[3])
    mtl_path = str(sys.argv[4])
    output_path = str(sys.argv[5])
    d_sun_earth_path = 'd_sun_earth'

    # Open red image and get its only band.
    red_tiff = Open(red_path)
    red_band = red_tiff.GetRasterBand(1)

    # Open NIR image and get its only band.
    nir_tiff = Open(nir_path)
    nir_band = nir_tiff.GetRasterBand(1)

    # Open bqa image and get its only band.
    bqa_tiff = Open(bqa_path)
    bqa_band = bqa_tiff.GetRasterBand(1)

    # Get the rows and cols from one of the images (both should always be the same)
    row, col, geotransform = nir_tiff.RasterYSize, nir_tiff.RasterXSize, nir_tiff.GetGeoTransform(
    )

    # Reading metadata
    parameters = metaReader.readParameters(mtl_path, d_sun_earth_path)

    number_sensor = int(parameters[0])
    julian_day = int(parameters[1])

    sun_elevation = float(parameters[2])
    dist_sun_earth = float(parameters[3])

    mask = get_mask(number_sensor)

    if shadow_check(bqa_band, (col, row), mask):
        print "Invalid inputs. Lots of cloud in tiff images"
        sys.exit()

    ndviC = NDVI(sun_elevation, red_band, nir_band, bqa_band, (col, row),
                 geotransform)
    ndviC.processNDVI(number_sensor, dist_sun_earth, output_path)
Exemplo n.º 3
0
def apply_valid_range(input_data_set_path: str, file_path: str) -> str:
    """Apply Valid Range -10000 -> 10000.

    Args:
        input_data_set_path (str) - Path to the input data set
        file_path (str) - Target data set filename
    Returns:
        Path to valid_range_image
    """
    src_ds = GDALOpen(input_data_set_path, GA_ReadOnly)

    if src_ds is None:
        raise ValueError(
            'Could not open data set "{}"'.format(input_data_set_path))

    driver = GetDriverByName('MEM')

    src_band = src_ds.GetRasterBand(1)
    data_set = driver.Create('', src_ds.RasterXSize, src_ds.RasterYSize, 1,
                             src_band.DataType)
    data_set.SetGeoTransform(src_ds.GetGeoTransform())
    data_set.SetProjection(src_ds.GetProjection())

    data_set_band = data_set.GetRasterBand(1)

    data_set_band.WriteArray(src_band.ReadAsArray())

    band_array = data_set_band.ReadAsArray()
    dummy = -9999
    data_set_band.SetNoDataValue(dummy)
    band_array[band_array <= -10000] = dummy
    band_array[band_array >= 10000] = dummy
    driver = GetDriverByName('GTiff')
    data_set_band.WriteArray(band_array)

    dst_ds = driver.CreateCopy(file_path, data_set, options=["COMPRESS=LZW"])

    del dst_ds
    del src_ds
    del data_set

    return file_path
Exemplo n.º 4
0
import gdal
from gdal import Open
from ndvi import ndvi


# Abrindo banda NIR
nir_tiff = Open(r'NIR_IMAGE.tif')
#nir_tiff = Open(r'nir.tif')
nir_band = nir_tiff.GetRasterBand(1)

# Abrindo banda RED.
red_tiff = Open(r'RED_IMAGE.tif')
#red_tiff = Open(r'red.tif') 
red_band = red_tiff.GetRasterBand(1)

# Get the rows and cols from one of the images (both should always be the same)
rows, cols, geotransform = nir_tiff.RasterYSize, nir_tiff.RasterXSize, nir_tiff.GetGeoTransform()
print(geotransform)

# Arquivo de saida 16-bit (0-255)
out_tiff_int16 = r'NDVI_16.tif'

# Arquivo de saida 32-bit floating point (-1 to 1)
out_tiff_float32 = r'NDVI_32.tif'

# Gerando imagem  NDVI 16-bit integer
ndvi(nir_band, red_band, rows, cols, geotransform, out_tiff_int16, gdal.GDT_UInt16)

# Gerando imagem NDVI 32-bit floating point
ndvi(nir_band, red_band, rows, cols, geotransform, out_tiff_float32, gdal.GDT_Float32)
Exemplo n.º 5
0
import sys
import os
from keras import backend as K
import tensorflow as tf


# ========== INPUT ==========
# Define input image dimensions
img_rows, img_cols = 32, 32
# The images are RGB.
img_channels = 3

# Use GDAL OPEN to read base RGB WorldView(WV02) map and corresponding reference label
wv = Open('mapMul.tif')
land = np.zeros((wv.RasterYSize,wv.RasterXSize,3)).astype('uint16')  #np.zeros_like(wv).astype('int16')
land[:,:,0] = wv.GetRasterBand(5).ReadAsArray()  # swaping the band and re-arrange the band order into 1-R, 2-G and 3-B
land[:,:,1] = wv.GetRasterBand(3).ReadAsArray()
land[:,:,2] = wv.GetRasterBand(2).ReadAsArray()
del wv  # delete variables to save memory resources
label = imread("labelMul.tif")

# Convert to 0-255 valued UINT8 image
land = cv2.convertScaleAbs(land,alpha = 255.0/(np.max(land)))
slum = (label > 0).astype('uint8')
del label  # delete variables to save memory resources
#plt.imshow(slum, 'gray')
land_slum = np.dstack((land, slum))  # stack the map and label for drawing training patches

# Sample from slum blocks according to the size of connected slum blocks
slum_block = measure.label(slum, background = False, connectivity = 1)  # extract and label connected slum blocks
regions = measure.regionprops(slum_block)  # get the properties of labelled slum blocks
Exemplo n.º 6
0
def publish(collection_item: CollectionItem, scene: RadcorActivity):
    """Publish Landsat collection.

    It works with both Digital Number (DN) and Surface Reflectance (SR).

    Args:
        collection_item - Collection Item
        scene - Current Activity
    """
    identifier = scene.sceneid
    cc = identifier.split('_')
    pathrow = cc[2]
    date = cc[3]
    yyyymm = '{}-{}'.format(date[0:4], date[4:6])

    productdir = scene.args.get('file')

    logging.warning('Publish {} - {} (id={})'.format(scene.collection_id,
                                                     productdir, scene.id))

    if productdir and productdir.endswith('.gz'):
        target_dir = Path(
            Config.DATA_DIR) / 'Repository/Archive/{}/{}/{}'.format(
                collection_item.collection_id, yyyymm, pathrow)
        makedirs(target_dir, exist_ok=True)

        productdir = uncompress(productdir, str(target_dir))

    collection = Collection.query().filter(
        Collection.id == collection_item.collection_id).one()
    quicklook = collection.bands_quicklook.split(
        ',') if collection.bands_quicklook else DEFAULT_QUICK_LOOK_BANDS

    files = {}
    qlfiles = {}

    if collection.id == 'LC8DN':
        bands = BAND_MAP_DN
    elif collection.id == 'LC8NBAR':
        bands = BAND_MAP_NBAR
    else:
        bands = BAND_MAP_SR

    for gband, band in bands.items():
        template = productdir + '/LC08_*_{}_{}_*_{}.*'.format(
            pathrow, date, band)
        fs = glob.glob(template)

        if not fs:
            continue

        for f in fs:
            if f.lower().endswith('.tif'):
                files[gband] = f
                if gband in quicklook:
                    qlfiles[gband] = f

    # Skip EVI/NDVI generation for Surface Reflectance
    # since the espa-science already done
    if collection.id == 'LC8DN' or collection.id == 'LC8NBAR':
        generate_vi(productdir, files)

    # Apply valid range and Cog files
    for band, file_path in files.items():
        if collection.id == 'LC8SR':
            _ = apply_valid_range(file_path, file_path)
        # Set destination of COG file
        files[band] = generate_cogs(file_path, file_path)
        if not is_valid_tif(file_path):
            raise RuntimeError('Not Valid {}'.format(file_path))

    # Extract basic scene information and build the quicklook
    pngname = productdir + '/{}.png'.format(identifier)

    dataset = GDALOpen(qlfiles['nir'], GA_ReadOnly)
    numlin = 768
    numcol = int(
        float(dataset.RasterXSize) / float(dataset.RasterYSize) * numlin)
    image = numpy.zeros((
        numlin,
        numcol,
        len(qlfiles),
    ), dtype=numpy.uint8)

    del dataset

    nb = 0
    for band in quicklook:
        template = qlfiles[band]
        dataset = GDALOpen(template, GA_ReadOnly)
        raster = dataset.GetRasterBand(1).ReadAsArray(0, 0,
                                                      dataset.RasterXSize,
                                                      dataset.RasterYSize)

        del dataset

        raster = resize(raster, (numlin, numcol), order=1, preserve_range=True)
        nodata = raster == -9999
        # Evaluate minimum and maximum values
        a = numpy.array(raster.flatten())
        p1, p99 = numpy.percentile(a[a > 0], (1, 99))
        # Convert minimum and maximum values to 1,255 - 0 is nodata
        raster = exposure.rescale_intensity(raster,
                                            in_range=(p1, p99),
                                            out_range=(1, 255)).astype(
                                                numpy.uint8)
        image[:, :, nb] = raster.astype(numpy.uint8) * numpy.invert(nodata)
        nb += 1

    write_png(pngname, image, transparent=(0, 0, 0))

    productdir = productdir.replace(Config.DATA_DIR, '')

    assets_to_upload = {
        'quicklook':
        dict(file=pngname, asset=productdir.replace('/Repository/Archive', ''))
    }

    for instance in ['local', 'aws']:
        engine_instance = {'local': db, 'aws': db_aws}
        engine = engine_instance[instance]

        # Skip catalog on aws for digital number
        if collection_item.collection_id == 'LC8DN' and instance == 'aws':
            continue

        if instance == 'aws':
            asset_url = productdir.replace('/Repository/Archive',
                                           Config.AWS_BUCKET_NAME)
        else:
            asset_url = productdir

        pngname = resource_path.join(asset_url, Path(pngname).name)

        assets_to_upload['quicklook']['asset'] = pngname

        with engine.session.begin_nested():
            with engine.session.no_autoflush:
                # Add collection item to the session if not present
                if collection_item not in engine.session:
                    item = engine.session.query(CollectionItem).filter(
                        CollectionItem.id == collection_item.id).first()

                    if not item:
                        cloned_properties = CollectionItemForm().dump(
                            collection_item)
                        collection_item = CollectionItem(**cloned_properties)
                        engine.session.add(collection_item)

                collection_item.quicklook = pngname

                collection_bands = engine.session.query(Band).filter(
                    Band.collection_id == collection_item.collection_id).all()

                # Inserting data into Product table
                for band in files:
                    template = resource_path.join(asset_url,
                                                  Path(files[band]).name)

                    dataset = GDALOpen(files[band], GA_ReadOnly)
                    asset_band = dataset.GetRasterBand(1)

                    chunk_x, chunk_y = asset_band.GetBlockSize()

                    band_model = next(
                        filter(lambda b: band == b.common_name,
                               collection_bands), None)

                    if not band_model:
                        logging.warning(
                            'Band {} of collection {} not found in database. Skipping...'
                            .format(band, collection_item.collection_id))
                        continue

                    defaults = dict(url=template,
                                    source=cc[0],
                                    raster_size_x=dataset.RasterXSize,
                                    raster_size_y=dataset.RasterYSize,
                                    raster_size_t=1,
                                    chunk_size_t=1,
                                    chunk_size_x=chunk_x,
                                    chunk_size_y=chunk_y)

                    asset, _ = get_or_create_model(
                        Asset,
                        engine=engine,
                        defaults=defaults,
                        collection_id=scene.collection_id,
                        band_id=band_model.id,
                        grs_schema_id=scene.collection.grs_schema_id,
                        tile_id=collection_item.tile_id,
                        collection_item_id=collection_item.id,
                    )
                    asset.url = defaults['url']

                    assets_to_upload[band] = dict(file=files[band],
                                                  asset=asset.url)

                    # Add into scope of local and remote database
                    add_instance(engine, asset)

            # Persist database
        commit(engine)

    return assets_to_upload
Exemplo n.º 7
0
import gdal
from gdal import Open
from ndvi import ndvi

# Open a before image and get its only band.
before_ndvi = Open(r'x')  #input desired image file name into x
before_ndvi_band = before_ndvi.GetRasterBand(1)

# Open a after image and get its only band.
after_ndvi = Open(r'y')  #input desired image file name into y
after_ndvi_band = after_ndvi.GetRasterBand(1)

# Get the rows and cols from one of the images (both should always be the same)
rows, cols = before_ndvi.RasterYSize, before_ndvi.RasterXSize

# Run the function for 32-bit floating point
comparison(before_ndvi_band, after_ndvi_band, rows, cols)

print('done')
Exemplo n.º 8
0
def publish(collection_item: CollectionItem, scene: RadcorActivity):
    """Publish Landsat collection.

    It works with both Digital Number (DN) and Surface Reflectance (SR).

    Args:
        collection_item - Collection Item
        scene - Current Activity
    """
    identifier = scene.sceneid

    # Get collection level to publish. Default is l1
    collection_level = scene.args.get('level') or 1

    landsat_scene = factory.get_from_sceneid(identifier,
                                             level=collection_level)

    productdir = scene.args.get('file')

    logging.warning('Publish {} - {} (id={})'.format(scene.collection_id,
                                                     productdir, scene.id))

    if productdir and productdir.endswith('.gz'):
        target_dir = landsat_scene.path()
        makedirs(target_dir, exist_ok=True)

        productdir = uncompress(productdir, str(target_dir))

    collection = Collection.query().filter(
        Collection.id == collection_item.collection_id).one()
    quicklook = collection.bands_quicklook.split(
        ',') if collection.bands_quicklook else DEFAULT_QUICK_LOOK_BANDS

    files = {}
    qlfiles = {}

    bands = landsat_scene.get_band_map()

    for gband, band in bands.items():
        fs = landsat_scene.get_files()

        if not fs:
            continue

        for f in fs:
            if f.stem.endswith(band) and f.suffix.lower().endswith('.tif'):
                files[gband] = f
                if gband in quicklook:
                    qlfiles[gband] = str(f)

    # Generate Vegetation Index files
    generate_vi(productdir, files)

    # Apply valid range and Cog files
    for band, file_path in files.items():
        tif_file = str(file_path)

        if landsat_scene.level == 2:
            _ = apply_valid_range(tif_file, tif_file)

        # Set destination of COG file
        files[band] = generate_cogs(tif_file, tif_file)
        if not is_valid_tif(tif_file):
            raise RuntimeError('Not Valid {}'.format(tif_file))

    # Extract basic scene information and build the quicklook
    pngname = productdir + '/{}.png'.format(identifier)

    dataset = GDALOpen(qlfiles['nir'], GA_ReadOnly)
    numlin = 768
    numcol = int(
        float(dataset.RasterXSize) / float(dataset.RasterYSize) * numlin)
    del dataset

    create_quick_look(pngname,
                      [qlfiles[band] for band in quicklook if band in qlfiles],
                      rows=numlin,
                      cols=numcol)

    productdir = productdir.replace(Config.DATA_DIR, '')

    assets_to_upload = {
        'quicklook':
        dict(file=pngname, asset=productdir.replace('/Repository/Archive', ''))
    }

    for instance in ['local', 'aws']:
        engine_instance = {'local': db, 'aws': db_aws}
        engine = engine_instance[instance]

        # Skip catalog on aws for digital number
        if landsat_scene.level == 1 and instance == 'aws':
            continue

        if instance == 'aws':
            asset_url = productdir.replace('/Repository/Archive',
                                           Config.AWS_BUCKET_NAME)
        else:
            asset_url = productdir

        pngname = resource_path.join(asset_url, Path(pngname).name)

        assets_to_upload['quicklook']['asset'] = pngname

        with engine.session.begin_nested():
            with engine.session.no_autoflush:
                # Add collection item to the session if not present
                if collection_item not in engine.session:
                    item = engine.session.query(CollectionItem).filter(
                        CollectionItem.id == collection_item.id).first()

                    if not item:
                        cloned_properties = CollectionItemForm().dump(
                            collection_item)
                        collection_item = CollectionItem(**cloned_properties)
                        engine.session.add(collection_item)

                collection_item.quicklook = pngname

                collection_bands = engine.session.query(Band)\
                    .filter(Band.collection_id == collection_item.collection_id)\
                    .all()

                # Inserting data into Product table
                for band in files:
                    template = resource_path.join(asset_url,
                                                  Path(files[band]).name)

                    dataset = GDALOpen(files[band], GA_ReadOnly)
                    asset_band = dataset.GetRasterBand(1)

                    chunk_x, chunk_y = asset_band.GetBlockSize()

                    band_model = next(
                        filter(lambda b: band == b.common_name,
                               collection_bands), None)

                    if not band_model:
                        logging.warning(
                            'Band {} of collection {} not found in database. Skipping...'
                            .format(band, collection_item.collection_id))
                        continue

                    defaults = dict(url=template,
                                    source=landsat_scene.source(),
                                    raster_size_x=dataset.RasterXSize,
                                    raster_size_y=dataset.RasterYSize,
                                    raster_size_t=1,
                                    chunk_size_t=1,
                                    chunk_size_x=chunk_x,
                                    chunk_size_y=chunk_y)

                    asset, _ = get_or_create_model(
                        Asset,
                        engine=engine,
                        defaults=defaults,
                        collection_id=scene.collection_id,
                        band_id=band_model.id,
                        grs_schema_id=scene.collection.grs_schema_id,
                        tile_id=collection_item.tile_id,
                        collection_item_id=collection_item.id,
                    )
                    asset.url = defaults['url']

                    assets_to_upload[band] = dict(file=files[band],
                                                  asset=asset.url)

                    # Add into scope of local and remote database
                    add_instance(engine, asset)

        # Persist database
        commit(engine)

    return assets_to_upload
Exemplo n.º 9
0
import gdal
from gdal import Open
from ndvi import ndvi
from ndvi import evi
from ndvi import dvi
from ndvi import ndwi
from ndvi import mndwi
from ndvi import ndbi
from ndvi import savi
from ndvi import ndbi
from ndvi import ndmi
import xlrd
import openpyxl

nir_tiff = Open(r'NIR_IMAGE.tif')
nir_band = nir_tiff.GetRasterBand(1)
print(nir_band)

red_tiff = Open(r'RED_IMAGE.tif')
red_band = red_tiff.GetRasterBand(1)
print(red_band)
blue_tiff = Open(r'BLUE_IMAGE.tif')
blue_band = blue_tiff.GetRasterBand(1)

green_tiff = Open(r'GREEN_IMAGE.tif')
green_band = green_tiff.GetRasterBand(1)

swir_tiff = Open(r'SWIR_IMAGE.tif')
swir_band = swir_tiff.GetRasterBand(1)

mir_tiff = Open(r'MIR_IMAGE.tif')