Exemple #1
0
def loadImage(folder, channels='ALL'):
    files = os.listdir(folder)
    metadatafile = [name for name in files if name.endswith("MTL.txt")][0]
    copyfile(os.path.join(folder, metadatafile),
             os.path.join(METAPATH, metadatafile))
    files = [
        name for name in files
        if name.endswith(tuple([".TIF", 'TIFF', 'jpg', ".tif"]))
    ]
    if channels == 'ALL':
        channels = [
            'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11',
            'BQA'
        ]
    elif type(channels) == str:
        channels = [channels]
    image = []
    for filename in files:
        file = str(filename.split('.')[0])
        Imname = '_'.join(file.split('_')[:-1])
        Bandname = file.split('_')[-1]
        if Bandname in channels:
            input = os.path.join(folder, filename)
            output = Imname + '.' + Bandname
            image.append(output)
            r.in_gdal(input=input, output=output, overwrite=True, flags='k')
    return sorted(image)
Exemple #2
0
def firsttimeGRASS(infiles, adminfile, maskfile):
    """
    Run a maxlikelihood unsupervised classification on the data
    nclasses: number of expected classes
    infiles: list of raster files to import and process
    firstime: if firsttime, it will import all files in GRASS
    """
    from grass_session import Session
    from grass.script import core as gcore
    from grass.pygrass.modules.shortcuts import raster as r
    from grass.pygrass.modules.shortcuts import vector as v
    from grass.pygrass.modules.shortcuts import general as g
    from grass.pygrass.modules.shortcuts import imagery as i
    # create a new location from EPSG code (can also be a GeoTIFF or SHP or ... file)
    with Session(gisdb="/tmp", location="loc", create_opts="EPSG:4326"):
        # First run, needs to import the files and create a mask
        # Import admin boundary
        #v.import_(input=adminfile,output="admin",quiet=True,superquiet=True)
        gcore.parse_command("v.import",
                            input=adminfile,
                            output="admin",
                            quiet=True)
        # Set computational region to admin boundary
        g.region(flags="s", vector="admin", quiet=True)
        # Keep only file name for output
        outmf = maskfile.split("/")[-1]
        # Import Mask file
        r.in_gdal(input=maskfile, output=outmf, quiet=True)
        # Apply Mask
        r.mask(raster=outmf, maskcats="0", quiet=True)
        # Set computational resolution to mask pixel size
        g.region(flags="s", raster=outmf, quiet=True)
        # Import files
        for f in infiles:
            # Keep only file name for output
            outf = f.split("/")[-1]
            # Landsat files not in Geo lat long needs reproj on import
            #r.import_(input=f,output=outf,quiet=True)
            gcore.parse_command("r.import", input=f, output=outf, quiet=True)
            # Create group
            i.group(group="l8", subgroup="l8", input=outf, quiet=True)
Exemple #3
0
def import_geotiffs(
    scene,
    band_filenames,
    mapset,
    memory,
    override_projection=False,
    prefix=None,
    link_geotiffs=False,
    skip_import=True,
    single_mapset=False,
    list_bands=False,
    list_timestamps=False,
    tgis_output=None,
    timestamp=None,
    force_timestamp=False,
    do_not_timestamp=False,
    skip_microseconds=False,
    copy_mtl=True,
):
    """
    Imports all bands (GeoTIF format) of a Landsat scene be it Landsat 5,
    7 or 8.  All known naming conventions are respected, such as "VCID" and
    "QA" found in newer Landsat scenes for temperature channels and Quality
    respectively.

    Parameters
    ----------
    scene :
        Input scene name string

    band_filenames :
        Bands to import

    mapset :
        Name of mapset to import to

    memory :
        See options for r.in.gdal

    prefix :
        Input scene name prefix string

    single_mapset :
        Import all scenes in a single Mapset

    list_bands :
        Boolean True or False

    list_timestamps :
        Boolean True or False
    """
    if not single_mapset:
        mapset = os.path.basename(scene)

    message = str()
    if not any(x for x in (list_bands, list_timestamps)):
        message = f'Date\t\tTime\t\tTimezone\n{simple_timestamp(timestamp)}\n\n'
        message += f'Target Mapset\n@{mapset}\n\n'

    if not list_timestamps:
        message += 'Band\tFilename\n'
        g.message(message, flags='v')

    # loop over files inside a "Landsat" directory
    # sort band numerals, source: https://stackoverflow.com/a/2669523/1172302
    for filename in band_filenames:

        # if not GeoTIFF, keep on working
        if os.path.splitext(filename)[-1] != GEOTIFF_EXTENSION:
            continue

        # use the full path name to the file
        name, band = get_name_band(scene, filename, single_mapset)
        band_title = f'band {band}'

        if not list_timestamps:
            message_overwriting = '\t [ Exists, overwriting]'
            # communicate input band and source file name
            message = f'{band}\t{filename}'
            if skip_import:
                # message for skipping import
                message_skipping = '\t [ Exists, skipping ]'

        if not any(x for x in (list_bands, list_timestamps)):
            absolute_filename = os.path.join(scene, filename)
            # sort import parameters
            parameters = dict(
                input=absolute_filename,
                output=name,
                flags='',
                title=band_title,
                quiet=True,
            )
            if override_projection:
                parameters['flags'] += 'o'

            # create Mapset of interest, if it doesn't exist
            devnull = open(os.devnull, 'w')
            run(
                'g.mapset',
                flags='c',
                mapset=mapset,
                stderr=devnull,
            )
            if (skip_import and find_existing_band(name)
                    and not grass.overwrite()):

                if force_timestamp:
                    set_timestamp(name, timestamp)
                    g.message(f'   >>> Force-stamp {timestamp} @ band {name}')

                message_skipping = message + message_skipping
                g.message(message_skipping, flags='v')
                pass

            else:
                if (grass.overwrite() and find_existing_band(name)):
                    if force_timestamp:
                        set_timestamp(name, timestamp)
                        g.message(
                            f'   >>> Force-stamp {timestamp} @ band {name}')

                    message_overwriting = message + message_overwriting
                    g.message(message_overwriting, flags='v')
                    pass

                if (skip_import and not find_existing_band(name)):
                    # FIXME
                    # communicate input band and source file name
                    message = f'{band}\t{filename}'
                    g.message(message, flags='v')

                if link_geotiffs:
                    # What happens with the '--overwrite' flag?
                    # Check if it can be retrieved.
                    r.external(**parameters)

                else:
                    if memory:
                        parameters['memory'] = memory
                    # try:
                    r.in_gdal(**parameters)

                    # except CalledModuleError:
                    # grass.fatal(_("Unable to read GDAL dataset {s}".format(s=scene)))

                if not do_not_timestamp:
                    set_timestamp(name, timestamp)

        else:
            pass

    # copy MTL
    if not list_bands and not list_timestamps:
        copy_mtl_in_cell_misc(scene, mapset, list_timestamps, single_mapset,
                              copy_mtl)
Exemple #4
0
MODFLOW_grid = 'grid'  # MODFLOW grid vector
slope = 'slope'  # Topographic slope
aspect = 'aspect'  # Topographic aspect
HRUs = 'HRUs'  # Hydrologic response units
gravity_reservoirs = 'gravity_reservoirs'  # Connect HRUs to MODFLOW grid
basin_mask = 'basin_mask'  # Mask out non-study-basin cells
pour_point = 'pour_point'  # Outlet pour point
bc_cell = 'bc_cell'  # Grid cell for MODFLOW b.c.

# Import DEM if required
# And perform the standard starting tasks.
# These take time, so skip if not needed
if Settings.DEM_input != '':
    # Import DEM and set region
    r.in_gdal(input=Settings.DEM_input,
              output=DEM_original_import,
              overwrite=True)
    g.region(raster=DEM_original_import)
    # Build flow accumulation with only fully on-map flow
    # Cell areas
    r.cell_area(output=cellArea_meters2, units='m2', overwrite=True)
    # Flow weights (e.g., precipitation
    # Test first if it is an existing raster; if not, import
    rastersAll = np.array(
        list(set(list(gscript.parse_command('g.list', type='raster')))))
    if Settings.flow_weights in rastersAll:
        # NOTE: Here, this might not necessarily be called "flow_weights"
        r.mapcalc(flow + ' = ' + cellArea_meters2 * Settings.flow_weights,
                  overwrite=True)
    else:
        r.in_gdal(input=Settings.flow_weights,
Exemple #5
0
slope = 'slope'  # Topographic slope
aspect = 'aspect'  # Topographic aspect
HRUs = 'HRUs'  # Hydrologic response units
gravity_reservoirs = 'gravity_reservoirs'  # Connect HRUs to MODFLOW grid
basin_mask = 'basin_mask'  # Mask out non-study-basin cells
pour_point = 'pour_point'  # Outlet pour point
bc_cell = 'bc_cell'  # Grid cell for MODFLOW b.c.

# Import DEM if required
# And perform the standard starting tasks.
# These take time, so skip if not needed
if Settings.DEM_input != '':
    print "Importing DEM and generating hydrologic correction"
    # Import DEM and set region
    r.in_gdal(input=Settings.DEM_input,
              output=DEM_original_import,
              overwrite=True)
    g.region(raster=DEM_original_import)
    # Build flow accumulation with only fully on-map flow
    # Cell areas
    r.cell_area(output=cellArea_meters2, units='m2', overwrite=True)
    # Flow weights (e.g., precipitation
    # Test first if it is an existing raster; if not, import
    rastersAll = np.array(
        list(set(list(gscript.parse_command('g.list', type='raster')))))
    if Settings.flow_weights in rastersAll:
        # NOTE: Here, this might not necessarily be called "flow_weights"
        r.mapcalc(flow + ' = ' + cellArea_meters2 * Settings.flow_weights,
                  overwrite=True)
    elif Settings.flow_weights is not '':
        r.in_gdal(input=Settings.flow_weights,
def import_geotiffs(scene, bands, mapset, memory, list_bands, tgis = False):
    """
    Imports all bands (GeoTIF format) of a Landsat scene be it Landsat 5,
    7 or 8.  All known naming conventions are respected, such as "VCID" and
    "QA" found in newer Landsat scenes for temperature channels and Quality
    respectively.

    Parameters
    ----------
    scene :
        Input scene name string

    bands :
        Bands to import

    mapset :
        Name of mapset to import to

    memory :
        See options for r.in.gdal

    list_bands :
        Boolean True or False

    tgis :
        Boolean True or False
    """

    timestamp = get_timestamp(scene, tgis)
    print_timestamp(os.path.basename(scene), timestamp, tgis)

    if not one_mapset:
        # set mapset from scene name
        mapset = os.path.basename(scene)

    message = str()  # a string holder

    # verbosity: target Mapset
    if not any(x for x in (list_bands, tgis)):
        message = 'Target Mapset\n@{mapset}\n\n'.format(mapset=mapset)

    # communicate input band name
    if not tgis:
        message += 'Band\tFilename\n'
        g.message(_(message))

    # loop over files inside a "Landsat" directory
    # sort band numerals, source: https://stackoverflow.com/a/2669523/1172302

    if bands == 'all':
        filenames = sort_list_of_bands(os.listdir(scene))
        # filenames = sorted(os.listdir(scene), key=lambda item:
        #         (int(item.partition('_B')[2].partition('.')[0])
        #             if item.partition('_B')[2].partition('.')[0].isdigit()
        #             else float('inf'), item))

    else:
        filenames = sort_list_of_bands(bands)

    for filename in filenames:

        # if not GeoTIFF, keep on working
        if os.path.splitext(filename)[-1] != GEOTIFF_EXTENSION:
            continue

        # use the full path name to the file
        name, band = get_name_band(scene, filename)
        band_title = 'band {band}'.format(band = band)

        if not tgis:

            message_overwriting = '\t [ Exists, overwriting]'

            # communicate input band and source file name
            message = '{band}'.format(band = band)
            message += '\t{filename}'.format(filename = filename)
            if not skip_import:
                g.message(_(message))

            else:
                # message for skipping import
                message_skipping = '\t [ Exists, skipping ]'

        if not any(x for x in (list_bands, tgis)):

            # get absolute filename
            absolute_filename = os.path.join(scene, filename)

            # srt import parameters
            parameters = dict(input = absolute_filename,
                    output = name,
                    flags = '',
                    title = band_title,
                    quiet = True)

            if override_projection:
                parameters['flags'] += 'o'

            # create Mapset of interest, if it doesn't exist
            devnull = open(os.devnull, 'w')
            run('g.mapset',
                    flags='c',
                    mapset=mapset,
                    stderr = devnull)
            # g.mapset(flags='c', mapset=mapset)

            if (skip_import
                    and find_existing_band(name)
                    and not grass.overwrite()):

                if force_timestamp:
                    set_timestamp(name, timestamp)
                    g.message(_('   >>> Forced timestamping for {b}'.format(b=name)))

                message_skipping = message + message_skipping
                g.message(_(message_skipping))
                pass

            else:
                if (grass.overwrite() and find_existing_band(name)):
                    if force_timestamp:
                        set_timestamp(name, timestamp)
                        g.message(_('   >>> Forced timestamping for {b}'.format(b=name)))

                    message_overwriting = message + message_overwriting
                    g.message(_(message_overwriting))
                    pass

                if (skip_import and not find_existing_band(name)):
                    # FIXME
                    # communicate input band and source file name
                    message = '{band}'.format(band = band)
                    message += '\t{filename}'.format(filename = filename)
                    grass.message(_(message))

                if link_geotiffs:
                    # What happens with the '--overwrite' flag?
                    # Check if it can be retrieved.

                    r.external(**parameters)

                else:
                    if memory:
                        parameters['memory'] = memory
                    # try:
                    r.in_gdal(**parameters)

                    # except CalledModuleError:
                        # grass.fatal(_("Unable to read GDAL dataset {s}".format(s=scene)))

                if not do_not_timestamp:
                    set_timestamp(name, timestamp)

        else:
            pass

    # copy MTL
    if not list_bands and not tgis:
        copy_mtl_in_cell_misc(scene, mapset, tgis, copy_mtl)
Exemple #7
0
reaches = 'reaches'  # Stream reaches (for MODFLOW)
MODFLOW_grid = 'grid'  # MODFLOW grid vector
slope = 'slope'  # Topographic slope
aspect = 'aspect'  # Topographic aspect
HRUs = 'HRUs'  # Hydrologic response units
gravity_reservoirs = 'gravity_reservoirs'  # Connect HRUs to MODFLOW grid
basin_mask = 'basin_mask'  # Mask out non-study-basin cells
pour_point = 'pour_point'  # Outlet pour point
bc_cell = 'bc_cell'  # Grid cell for MODFLOW b.c.

# Import DEM if required
# And perform the standard starting tasks.
# These take time, so skip if not needed
if DEM_input != '':
    # Import DEM and set region
    r.in_gdal(input=DEM_input, output=DEM_original_import)
    g.region(raster=DEM_original_import)
    # Build flow accumulation with only fully on-map flow
    # Cell areas
    r.cell_area(output=cellArea_meters2, units='m2', overwrite=True)
    # Hydrologic correction
    r.hydrodem(input=DEM_original_import,
               output=DEM,
               flags='a',
               overwrite=True)
    # No offmap flow
    r.watershed(elevation=DEM,
                flow=cellArea_meters2,
                accumulation=accumulation,
                flags='m',
                overwrite=True)