From b2a63c50a6f5a58c689d67bf9776a6743db7d875 Mon Sep 17 00:00:00 2001 From: Fabrice Brito <fabrice.brito@terradue.com> Date: Thu, 19 Oct 2023 16:37:12 +0000 Subject: [PATCH] tidy python --- water-bodies/command-line-tools/crop/app.py | 33 ++++++++++--------- .../command-line-tools/norm_diff/app.py | 6 ++-- water-bodies/command-line-tools/otsu/app.py | 9 ++--- water-bodies/command-line-tools/stac/app.py | 14 +++----- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/water-bodies/command-line-tools/crop/app.py b/water-bodies/command-line-tools/crop/app.py index 411287c..14a3294 100644 --- a/water-bodies/command-line-tools/crop/app.py +++ b/water-bodies/command-line-tools/crop/app.py @@ -1,20 +1,21 @@ +"""Crop a STAC Item asset defined with its common band name""" import os -import click from urllib.parse import urlparse +import click from osgeo import gdal import pystac gdal.UseExceptions() -settings = None +SETTINGS = None def aoi2box(aoi): - + """Converts an area of interest expressed as a bounding box to a list of floats""" return [float(c) for c in aoi.split(",")] def get_common_name(asset): - + """Returns the common band name of a STAC Item asset""" if "eo:bands" in asset.to_dict().keys(): if "common_name" in asset.to_dict()["eo:bands"][0].keys(): return asset.to_dict()["eo:bands"][0]["common_name"] @@ -23,9 +24,8 @@ def get_common_name(asset): def get_asset(item, common_name): - + """Returns the asset of a STAC Item defined with its common band name""" for _, asset in item.get_assets().items(): - if not "data" in asset.to_dict()["roles"]: continue @@ -41,20 +41,21 @@ def get_asset(item, common_name): def vsi_href(uri): - + """Returns the VSI path of a URI""" parsed = urlparse(uri) if parsed.scheme.startswith("http"): - return "/vsicurl/{}".format(uri) - elif parsed.scheme.startswith("file"): + return f"/vsicurl/{uri}" + if parsed.scheme.startswith("file"): return uri.replace("file://", "") - elif parsed.scheme.startswith("s3"): - if settings: - for key, value in settings._asdict().items(): + + if parsed.scheme.startswith("s3"): + if SETTINGS: + for key, value in SETTINGS._asdict().items(): gdal.SetConfigOption(key, value) - return "/vsis3/{}".format(uri.strip("s3://")) - else: - return uri + return f"/vsis3/{uri.strip('s3://')}" + + return uri @click.command( @@ -86,7 +87,7 @@ def vsi_href(uri): required=True, ) def crop(item_url, aoi, band, epsg): - + """Crops a STAC Item asset defined with its common band name""" if os.path.isdir(item_url): catalog = pystac.read_file(os.path.join(item_url, "catalog.json")) item = next(catalog.get_items()) diff --git a/water-bodies/command-line-tools/norm_diff/app.py b/water-bodies/command-line-tools/norm_diff/app.py index 5511e0c..a764a9f 100644 --- a/water-bodies/command-line-tools/norm_diff/app.py +++ b/water-bodies/command-line-tools/norm_diff/app.py @@ -1,7 +1,9 @@ +"""Normalized difference""" import click from osgeo import gdal import numpy as np +gdal.UseExceptions() @click.command( short_help="Normalized difference", @@ -9,7 +11,8 @@ ) @click.argument("rasters", nargs=2) def normalized_difference(rasters): - + """Performs a normalized difference""" + # Allow division by zero np.seterr(divide="ignore", invalid="ignore") @@ -40,6 +43,5 @@ def normalized_difference(rasters): dst_ds = None ds1 = ds2 = None - if __name__ == "__main__": normalized_difference() diff --git a/water-bodies/command-line-tools/otsu/app.py b/water-bodies/command-line-tools/otsu/app.py index 0253704..a55069a 100644 --- a/water-bodies/command-line-tools/otsu/app.py +++ b/water-bodies/command-line-tools/otsu/app.py @@ -1,11 +1,12 @@ -from skimage.filters import threshold_otsu +"""Command line tool to apply the Otsu threshold to a raster""" import click -from osgeo import gdal import numpy as np +from osgeo import gdal +from skimage.filters import threshold_otsu def threshold(data): - + """Returns the Otsu threshold of a numpy array""" return data > threshold_otsu(data[np.isfinite(data)]) @@ -15,7 +16,7 @@ def threshold(data): ) @click.argument("raster", nargs=1) def otsu(raster): - + """Applies the Otsu threshold""" ds = gdal.Open(raster) driver = gdal.GetDriverByName("GTiff") diff --git a/water-bodies/command-line-tools/stac/app.py b/water-bodies/command-line-tools/stac/app.py index 4896fe4..26e4c69 100644 --- a/water-bodies/command-line-tools/stac/app.py +++ b/water-bodies/command-line-tools/stac/app.py @@ -1,8 +1,9 @@ +"""Creates a STAC catalog with the detected water bodies""" "" +import os +import shutil import click import pystac import rio_stac -import shutil -import os @click.command( @@ -24,18 +25,16 @@ multiple=True, ) def to_stac(item_urls, water_bodies): - + """Creates a STAC catalog with the detected water bodies""" cat = pystac.Catalog(id="catalog", description="water-bodies") for index, item_url in enumerate(item_urls): - if os.path.isdir(item_url): catalog = pystac.read_file(os.path.join(item_url, "catalog.json")) item = next(catalog.get_items()) else: item = pystac.read_file(item_url) - water_body = water_bodies[index] os.mkdir(item.id) @@ -58,11 +57,6 @@ def to_stac(item_urls, water_bodies): root_href="./", catalog_type=pystac.CatalogType.SELF_CONTAINED ) - # for index, water_body in enumerate(water_bodies): - # item = pystac.read_file(item_urls[index]) - - # shutil.copy(water_body, item.id) - if __name__ == "__main__": to_stac()