-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
201 lines (155 loc) · 4.98 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os
import click
import pystac
import rasterio
from skimage.filters import threshold_otsu
from rasterio.mask import mask
from pyproj import Transformer
from shapely import box
from loguru import logger
import rasterio
import pystac
import shutil
import rio_stac
import numpy as np
np.seterr(divide="ignore", invalid="ignore")
def crop(asset: pystac.Asset, bbox, epsg):
"""_summary_
Args:
asset (_type_): _description_
bbox (_type_): _description_
epsg (_type_): _description_
Returns:
_type_: _description_
"""
with rasterio.open(asset.get_absolute_href()) as src:
transformer = Transformer.from_crs(epsg, src.crs, always_xy=True)
minx, miny = transformer.transform(bbox[0], bbox[1])
maxx, maxy = transformer.transform(bbox[2], bbox[3])
transformed_bbox = box(minx, miny, maxx, maxy)
logger.info(f"Crop {asset.get_absolute_href()}")
out_image, out_transform = rasterio.mask.mask(
src, [transformed_bbox], crop=True
)
out_meta = src.meta.copy()
out_meta.update(
{
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform,
}
)
return out_image.astype(np.float32), out_meta
def threshold(data):
"""Returns the Otsu threshold of a numpy array"""
return data > threshold_otsu(data[np.isfinite(data)])
def normalized_difference(array1, array2):
"""Returns the normalized difference of two numpy arrays"""
return (array1 - array2) / (array1 + array2)
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_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
eo_asset = pystac.extensions.eo.AssetEOExtension(asset)
if not eo_asset.bands:
continue
for b in eo_asset.bands:
if (
"common_name" in b.properties.keys()
and b.properties["common_name"] == common_name
):
return asset
@click.command(
short_help="Crop",
help="Crops a STAC Item asset defined with its common band name",
)
@click.option(
"--input-item",
"item_url",
help="STAC Item URL or staged STAC catalog",
required=True,
)
@click.option(
"--aoi",
"aoi",
help="Area of interest expressed as a bounding box",
required=True,
)
@click.option(
"--epsg",
"epsg",
help="EPSG code",
required=True,
)
@click.option(
"--band",
"bands",
help="Common band name",
required=True,
multiple=True,
)
def main(item_url, aoi, bands, epsg):
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)
logger.info(f"Read {item.id} from {item.get_self_href()}")
cropped_assets = {}
for band in bands:
asset = get_asset(item, band)
logger.info(f"Read asset {band} from {asset.get_absolute_href()}")
if not asset:
msg = f"Common band name {band} not found in the assets"
logger.error(msg)
raise ValueError(msg)
bbox = aoi2box(aoi)
out_image, out_meta = crop(asset, bbox, epsg)
cropped_assets[band] = out_image[0]
nd = normalized_difference(cropped_assets[bands[0]], cropped_assets[bands[1]])
water_bodies = threshold(nd)
out_meta.update(
{
"dtype": "uint8",
"driver": "COG",
"tiled": True,
"compress": "lzw",
"blockxsize": 256,
"blockysize": 256,
}
)
water_body = "otsu.tif"
with rasterio.open(water_body, "w", **out_meta) as dst_dataset:
logger.info(f"Write otsu.tif")
dst_dataset.write(water_bodies, indexes=1)
logger.info(f"Creating a STAC Catalog")
cat = pystac.Catalog(id="catalog", description="water-bodies")
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)
os.makedirs(item.id, exist_ok=True)
shutil.copy(water_body, item.id)
out_item = rio_stac.stac.create_stac_item(
source=water_body,
input_datetime=item.datetime,
id=item.id,
asset_roles=["data", "visual"],
asset_href=os.path.basename(water_body),
asset_name="data",
with_proj=True,
with_raster=False,
)
cat.add_items([out_item])
cat.normalize_and_save(
root_href="./", catalog_type=pystac.CatalogType.SELF_CONTAINED
)
os.remove(water_body)
logger.info("Done!")
if __name__ == "__main__":
main()