Пример #1
0
def classify_image():
    run_subprocess(
        '{otb_bin_path}/otbcli_ImageClassifier -in {input} -model {model} -out {out}'
        .format(otb_bin_path=settings.OTB_BIN_PATH,
                input=os.path.join(RESULTS_DIR, 'features.tif'),
                model=MODEL_PATH,
                out=os.path.join(RESULTS_DIR, 'cover.tif')))
Пример #2
0
def superimpose(inm, inr):
    run_subprocess(
        '{otb_bin_path}/otbcli_Superimpose -inr {inr} -inm {inm} -out {out}'.
        format(otb_bin_path=settings.OTB_BIN_PATH,
               inr=os.path.join(RESULTS_SRC, "{}.tif".format(inr)),
               inm=os.path.join(RESULTS_SRC, "{}.tif".format(inm)),
               out=os.path.join(RESULTS_FEAT, "{}.tif".format(inm))))
Пример #3
0
def extract_haralick(name, band):
    run_subprocess(
        '{otb_bin_path}/otbcli_HaralickTextureExtraction -in {input} -channel {band} -texture simple -parameters.min 0 -parameters.max 0.3 -out {out}'
        .format(otb_bin_path=settings.OTB_BIN_PATH,
                input=os.path.join(RESULTS_FEAT, "{}.tif".format(name)),
                band=band,
                out=os.path.join(RESULTS_FEAT,
                                 'haralick_{}_{}.tif'.format(name, band))))
Пример #4
0
def extract_local_stats(name, band):
    run_subprocess(
        '{otb_bin_path}/otbcli_LocalStatisticExtraction -in {input} -channel {band} -radius 3 -out {out}'
        .format(otb_bin_path=settings.OTB_BIN_PATH,
                input=os.path.join(RESULTS_FEAT, "{}.tif".format(name)),
                band=band,
                out=os.path.join(RESULTS_FEAT,
                                 'local_stats_{}_{}.tif'.format(name, band))))
Пример #5
0
def concatenate_images():
    current_dir = os.getcwd()
    os.chdir(RESULTS_FEAT)
    run_subprocess(
        '{otb_bin_path}/otbcli_ConcatenateImages -il $(ls {il}) -out {out}'.
        format(otb_bin_path=settings.OTB_BIN_PATH,
               il=RESULTS_FEAT,
               out=os.path.join(RESULTS_DIR, 'features.tif')))
    os.chdir(current_dir)
Пример #6
0
def download_and_build_composite(date_from, date_to):
    from eo_sensors.utils import clip, rescale_byte, unzip
    from sentinelsat.sentinel import SentinelAPI, geojson_to_wkt, read_geojson

    period_s = '{dfrom}_{dto}'.format(dfrom=date_from.strftime("%Y%m%d"),
                                      dto=date_to.strftime("%Y%m%d"))
    proc_scene_dir = os.path.join(PROC_DIR, period_s)
    tci_path = os.path.join(proc_scene_dir, 'tci.tif')

    if os.path.exists(tci_path):
        logger.info("TCI file already generated at %s", tci_path)
        return tci_path

    if not settings.SCIHUB_USER or not settings.SCIHUB_PASS:
        raise "SCIHUB_USER and/or SCIHUB_PASS are not set. " + \
              "Please read the Configuration section on README."

    api = SentinelAPI(settings.SCIHUB_USER, settings.SCIHUB_PASS,
                      settings.SCIHUB_URL)

    extent = read_geojson(EXTENT_PATH)
    footprint = geojson_to_wkt(extent)
    logger.info(
        "Query S2MSI2A products with up to %d%% cloud cover from %s to %s",
        MAX_CLOUD_PERC, date_from, date_to)
    products = api.query(footprint,
                         date=(date_from, date_to),
                         platformname='Sentinel-2',
                         cloudcoverpercentage=(0, MAX_CLOUD_PERC),
                         producttype='S2MSI2A')
    logger.info("Found %d products", len(products))

    raw_dir = os.path.join(RAW_DIR, period_s)
    os.makedirs(raw_dir, exist_ok=True)

    # Filter already downloaded products
    products_to_download = {
        k: v
        for k, v in products.items()
        if not (os.path.exists(
            os.path.join(raw_dir, '{}.zip'.format(v['title']))) or os.path.
                exists(os.path.join(raw_dir, '{}.SAFE'.format(v['title']))))
    }

    # Download products
    if products_to_download:
        logger.info("Download all products (%d)", len(products_to_download))
        api.download_all(products_to_download, directory_path=raw_dir)

    # Unzip compressed files, if there are any
    for p in glob(os.path.join(raw_dir, '*.zip')):
        name, _ = os.path.splitext(os.path.basename(p))
        p_dir = os.path.join(raw_dir, f'{name}.SAFE')
        if not os.path.exists(p_dir):
            logger.info("Unzip %s", p)
            unzip(p, delete_zip=False)

    # Build mosaic
    mosaic_dir = os.path.join(proc_scene_dir, 'mosaic')
    os.makedirs(mosaic_dir, exist_ok=True)
    # FIXME: Read bounds from EXTENT_UTM_PATH
    xmin, ymin, xmax, ymax = [
        261215.0000000000000000, 8620583.0000000000000000,
        323691.8790999995544553, 8719912.0846999995410442
    ]
    cmd = f"python3 {settings.S2M_CLI_PATH}/mosaic.py " \
            f"-te {xmin} {ymin} {xmax} {ymax} " \
            f"-e 32718 -res 10 -v " \
            f"-p {settings.S2M_NUM_JOBS} " \
            f"-o {mosaic_dir} {raw_dir}"
    run_subprocess(cmd)

    # Get mosaic band rasters
    mosaic_rgb_paths = [
        glob(os.path.join(mosaic_dir, f'*_{band}.tif'))
        for band in ['B04', 'B03', 'B02']
    ]
    mosaic_rgb_paths = [p[0] for p in mosaic_rgb_paths if p]
    logger.info("RGB paths: %s", mosaic_rgb_paths)

    # Use gdalbuildvrt to concatenate RGB bands from mosaic
    vrt_path = os.path.join(mosaic_dir, 'tci.vrt')
    cmd = f"gdalbuildvrt -separate {vrt_path} {' '.join(mosaic_rgb_paths)}"
    run_subprocess(cmd)

    # Clip to extent and rescale virtual raster
    clipped_tci_path = os.path.join(mosaic_dir, 'tci.tif')
    clip(src=vrt_path, dst=clipped_tci_path, aoi=EXTENT_UTM_PATH)

    # Rescale image
    rescale_byte(src=clipped_tci_path, dst=tci_path, in_range=(100, 3000))

    return tci_path