-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstac-simple.py
executable file
·202 lines (178 loc) · 5.07 KB
/
stac-simple.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
202
#!/usr/bin/env python
import datetime
import json
import pathlib
import click
import jinja2
import pystac
import rasterio
from odc.index.stac import stac_transform
from pyproj import Transformer
from rio_cogeo.cogeo import cog_translate, cog_validate
from rio_cogeo.profiles import cog_profiles
output_profile = cog_profiles.get("deflate")
output_profile.update(dict(nodata=0), crs="epsg:28355")
template = jinja2.Template(
"""
name: {{platform}}
description: Auto-generated product example for {{platform}}
metadata_type: eo3
metadata:
product:
name: {{platform}}
measurements:
- name: '{{band_name}}'
units: '1'
dtype: '{{band_type}}'
nodata: {{band_nodata}}
...
"""
)
def get_datetime(raster):
file_name = raster.stem
date_string = file_name.split("_")[3]
date = datetime.datetime.strptime(date_string, "%Y%m%d%H%M")
return date.isoformat() + "Z"
def get_geometry(bbox, from_crs):
transformer = Transformer.from_crs(from_crs, 4326)
bbox_lonlat = [
[bbox.left, bbox.bottom],
[bbox.left, bbox.top],
[bbox.right, bbox.top],
[bbox.right, bbox.bottom],
[bbox.left, bbox.bottom],
]
geometry = {
"type": "Polygon",
"coordinates": [list(transformer.itransform(bbox_lonlat))],
}
return geometry, bbox_lonlat
def convert_to_cog(raster, validate=True):
out_path = str(raster.with_suffix(".tif")).replace(" ", "_")
assert raster != out_path, "Can't convert to files of the same name"
cog_translate(raster, out_path, output_profile, quiet=True)
if validate:
cog_validate(out_path)
return pathlib.Path(out_path)
def create_stac(raster, platform, band_name, default_date):
transform = None
shape = None
crs = None
with rasterio.open(raster) as dataset:
transform = dataset.transform
shape = dataset.shape
crs = dataset.crs.to_epsg()
bounds = dataset.bounds
date_string = default_date
if not date_string:
date_string = get_datetime(raster)
geometry, bbox = get_geometry(bounds, crs)
stac_dict = {
"id": raster.stem.replace(" ", "_"),
"type": "Feature",
"stac_version": "1.0.0-beta.2",
"stac_extensions": [
"proj"
],
"properties": {"platform": platform, "datetime": date_string, "proj:epsg": crs},
"bbox": bbox,
"geometry": geometry,
"assets": {
band_name: {
"title": f"Data file for {band_name}",
"type": "image/tiff; application=geotiff; profile=cloud-optimized",
"roles": ["data"],
"href": raster.stem + raster.suffix,
"proj:shape": shape,
"proj:transform": transform,
}
},
}
with open(raster.with_suffix(".json"), "w") as f:
json.dump(stac_dict, f, indent=2)
with open(raster.with_suffix(".odc-dataset.json"), "w") as f:
json.dump(stac_transform(stac_dict), f, indent=2)
return None
def process_rasters(rasters, platform, band_name, cog_convert, default_date):
for raster in rasters:
try:
if cog_convert:
raster = convert_to_cog(raster)
create_stac(raster, platform, band_name, default_date)
except Exception as e:
print(f"Failed to process {raster} with exception {e}")
@click.command("create-odc-stac")
@click.option(
"--extension",
default=".tif",
type=str,
help="Extension of files to work on.",
)
@click.option(
"--default-date",
default=None,
type=str,
help="A date for the file. Todo: work out how to make this magic.",
)
@click.option(
"--platform",
type=str,
required=True,
help="Platform name for the product",
)
@click.option(
"--band-name",
type=str,
required=True,
help="Band name for the asset/measurement",
)
@click.option(
"--band-type",
type=str,
default="uint8",
help="Band date type for the band",
)
@click.option(
"--band-nodata",
type=float,
default=0,
help="Band data type for the band",
)
@click.option(
"--create-product/--no-create-product",
is_flag=True,
default=True,
help=("Creates a basic EO3 product definition"),
)
@click.option(
"--cog-convert/--no-cog-convert",
is_flag=True,
default=False,
help=("Converts files to Cloud Optimised GeoTIFFs"),
)
@click.argument("directory", type=str, nargs=1)
def cli(
extension,
default_date,
platform,
create_product,
band_name,
band_type,
band_nodata,
cog_convert,
directory,
):
if create_product:
with open(pathlib.Path(directory) / f"{platform}.odc-product.yaml", "wt") as f:
f.write(
template.render(
platform=platform,
band_name=band_name,
band_type=band_type,
band_nodata=band_nodata,
)
)
rasters = pathlib.Path(directory).glob("**/*" + extension)
process_rasters(rasters, platform, band_name, cog_convert, default_date)
if __name__ == "__main__":
cli()