operations.py 6.58 KB
Newer Older
Gaetano Raffaele's avatar
Gaetano Raffaele committed
import os.path
import OBIA.segmentation
import VHR.vhrbase
from TimeSeries import s2theia, s2planetary, s1base, s1planetary, planet_mosaics, landsat_planetary
Gaetano Raffaele's avatar
Gaetano Raffaele committed
from Common import demtools
from Common.geometry import apply_shift_using_points
from Common.imagetools import vhr_to_basemap
Gaetano Raffaele's avatar
Gaetano Raffaele committed

def run_segmentation(img, threshold, cw, sw , out_seg,
                     n_first_iter, margin, roi, n_proc, memory=None,
                     remove_graph=True, force_parallel=False, light=False):
    if not os.path.exists(os.path.dirname(out_seg)):
        os.makedirs(os.path.dirname(out_seg))
    params = OBIA.segmentation.LSGRMParams(threshold, cw, sw, n_first_iter, margin)
    if light:
        OBIA.segmentation.lsgrm_light(img, params, out_seg, n_proc, memory, roi, force_parallel, remove_graph)
Gaetano Raffaele's avatar
Gaetano Raffaele committed
    else:
        OBIA.segmentation.lsgrm(img, params, out_seg, n_proc, memory, roi, remove_graph, force_parallel)
    return out_seg

def preprocess_spot67(in_fld, out_fld, dem_fld, geoid_file, skip_ps, compress,
                      clip, align_to=None, align_to_band=3, align_using_band=1):
    sp = VHR.vhrbase.SPOT67RasterPipeline(in_fld)
    sp.to_toa()
    sp.orthorectify(dem_fld, geoid_file)
    if clip is not None and os.path.exists(clip):
        sp.clip(clip)
    if not skip_ps:
        sp.write_outputs(out_fld, update_pipe=True, compress=compress)
        sp.pansharp()
    if align_to is not None and os.path.exists(align_to):
        sp.rigid_align(align_to, this_band=align_using_band-1, ref_band=align_to_band-1)
    return sp.write_outputs(out_fld, compress=compress)

def preprocess_pleiades(in_fld, out_fld, skip_ps, compress, clip, 
                        align_to=None, align_to_band=3, align_using_band=1):
    sp = VHR.vhrbase.PleiadesOrthoPipeline(in_fld)
    sp.to_toa()
    if clip is not None and os.path.exists(clip):
        sp.clip(clip)
    if not skip_ps:
        sp.write_outputs(out_fld, update_pipe=True, compress=compress)
        sp.pansharp()
    if align_to is not None and os.path.exists(align_to):
        sp.rigid_align(align_to, this_band=align_using_band-1, ref_band=align_to_band-1)
    return sp.write_outputs(out_fld, compress=compress)

def preprocess_pleiades_mosaic(in_fld, out_fld, pattern):
    sp = VHR.vhrbase.PleiadesOrthoAutoMosaicPipeline(in_fld, pattern, out_fld)
    sp.process_scenes()
    return sp.generate_mosaic()

Gaetano Raffaele's avatar
Gaetano Raffaele committed
def preprocess_s2(in_fld, out_fld, output_dates_file=None, roi=None,
                  align_to=None, align_to_band=3, align_using_band=3, provider='theia'):
    S2Processor = None
    if provider == 'theia':
        S2Processor = s2theia.S2TheiaPipeline
    elif provider == 'planetary':
        S2Processor = s2planetary.S2PlaneteryPipeline
    else:
        raise ValueError("Unsupported/non-valid provider")

    s2 = S2Processor(in_fld, temp_fld=out_fld, roi=roi)
    #if roi is not None:
    #    s2.set_roi(roi)
Gaetano Raffaele's avatar
Gaetano Raffaele committed
    if output_dates_file is not None:
        s2.set_output_dates_by_file(output_dates_file)
    else:
        raise ValueError("Please provide path to a text file containing output dates.")

    align = align_to is not None
    if (align_to == 'self'):
        align_to = None

    return s2.extract_feature_set(out_fld, store_gapfill=True, mosaicking='vrt', align=align,
                           align_to=align_to, align_to_band=align_to_band, align_using_band=align_using_band)

def preprocess_landsat(in_fld, out_fld, output_dates_file=None, roi=None, which='landsat89'):
    LandsatProcessor = None
    if which == 'landsat89':
        LandsatProcessor = landsat_planetary.Landsat89PlanetaryPipeline
    elif which == 'landsat7':
        LandsatProcessor = landsat_planetary.Landsat7PlanetaryPipeline
    elif which == 'landsat45':
        LandsatProcessor = landsat_planetary.Landsat45PlanetaryPipeline
    else:
        raise ValueError("Unknown Landsat mission.")
    
    ls = LandsatProcessor(in_fld, temp_fld=out_fld, roi=roi)
    if output_dates_file is not None:
        ls.set_output_dates_by_file(output_dates_file)
    else:
        raise ValueError("Please provide path to a text file containing output dates.")
    
    return ls.extract_feature_set(out_fld, store_gapfill=True, mosaicking='vrt')

def preprocess_planet(in_fld, out_fld):
    pl = planet_mosaics.PlanetMosaicPipeline(in_fld)
    pl.compute_features()
    return pl.write_outputs(out_fld)

Gaetano Raffaele's avatar
Gaetano Raffaele committed
def preprocess_s1(in_fld, roi, out_fld, dem_fld=None, geoid=None, direction=None, satellite=None,
                  skip_despeckle=False, provider='native'):
    S1processor = None
    if provider == 'native':
        S1Processor = s1base.S1GRDPipeline
    elif provider == 'planetary':
        S1Processor = s1planetary.S1RTCPipeline
    else:
        raise ValueError("Unsupported/non-valid provider")

    s1 = S1Processor(in_fld, roi, out_fld, direction=direction, satellite=satellite)
    if provider == 'native':
        assert(os.path.exists(dem_fld) and os.path.exists(geoid))
        s1.calibrate()
        s1.orthorectify(dem_fld, geoid)
    elif provider == 'planetary':
        s1.superimpose()
Gaetano Raffaele's avatar
Gaetano Raffaele committed
    s1.stitch()
    if not skip_despeckle:
        s1.multitemp_speckle_filter()
    s1.compute_features()
    return s1.write_outputs(out_fld)

def fetch(imagery, shp, out_fld, dt=None, auth=None, only_tiles=None):
    assert(imagery in ['s2theia', 's2planetary', 's1grd', 's1rtc', 'planetmosaics', 'cop-dem-glo-30', 'nasadem', 'landsatplanetary'])
    # Seems that Planetary don't need api _keys anymore
    #if imagery not in ['s2planetary', 'cop-dem-glo-30', 'nasadem'] and auth is None:
    #    raise ValueError("Please provide authentication information.")
Gaetano Raffaele's avatar
Gaetano Raffaele committed
    if imagery not in ['cop-dem-glo-30', 'nasadem'] and dt is None:
        raise ValueError("Please provide date range option.")
    if imagery == 's2theia':
        #temporarily switch to eodag since theia_picker is unusable
        s2theia.fetch_eodag(shp, dt, out_fld, auth, only_tiles.split(';'))
Gaetano Raffaele's avatar
Gaetano Raffaele committed
    elif imagery == 's2planetary':
        s2planetary.fetch(shp, dt, out_fld, auth)
Gaetano Raffaele's avatar
Gaetano Raffaele committed
    elif imagery == 's1grd':
        s1base.fetch(shp, dt, out_fld, auth)
    elif imagery == 's1rtc':
        s1planetary.fetch(shp, dt, out_fld, auth)
    elif imagery == 'planetmosaics':
        planet_mosaics.fetch(shp, dt, out_fld, auth)
    elif imagery in ['cop-dem-glo-30', 'nasadem']:
        demtools.fetch(shp, out_fld, product=imagery, auth=auth)
    elif imagery == 'landsatplanetary':
        landsat_planetary.fetch(shp, dt, out_fld, auth)
Gaetano Raffaele's avatar
Gaetano Raffaele committed
    return

def apply_shift(in_img, in_points):
    apply_shift_using_points(in_img, in_points)
    return

def create_basemap(img, out, quantiles=[2,2], bands=[1,2,3], quality=75):
    vhr_to_basemap(img, out, quantiles, bands, quality)
    return