Skip to content

Commit

Permalink
tidy python
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricebrito committed Oct 19, 2023
1 parent 2935a49 commit b2a63c5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
33 changes: 17 additions & 16 deletions water-bodies/command-line-tools/crop/app.py
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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

Expand All @@ -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(
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 4 additions & 2 deletions water-bodies/command-line-tools/norm_diff/app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"""Normalized difference"""
import click
from osgeo import gdal
import numpy as np

gdal.UseExceptions()

@click.command(
short_help="Normalized difference",
help="Performs a normalized difference",
)
@click.argument("rasters", nargs=2)
def normalized_difference(rasters):

"""Performs a normalized difference"""

# Allow division by zero
np.seterr(divide="ignore", invalid="ignore")

Expand Down Expand Up @@ -40,6 +43,5 @@ def normalized_difference(rasters):
dst_ds = None
ds1 = ds2 = None


if __name__ == "__main__":
normalized_difference()
9 changes: 5 additions & 4 deletions water-bodies/command-line-tools/otsu/app.py
Original file line number Diff line number Diff line change
@@ -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)])


Expand All @@ -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")
Expand Down
14 changes: 4 additions & 10 deletions water-bodies/command-line-tools/stac/app.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
Expand All @@ -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()

0 comments on commit b2a63c5

Please sign in to comment.