-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwarp_to_template.py
47 lines (39 loc) · 1.6 KB
/
warp_to_template.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
import click
import tempfile
import os
import gdal_utils as gu
import snappy_utils as su
@click.command()
@click.option('--source', required=True, type=click.Path(dir_okay=False, exists=True))
@click.option('--template', required=True, type=click.Path(dir_okay=False, exists=True))
@click.option('--resample_algorithm', required=True, default='cubicspline')
@click.option('--output', required=True, type=click.Path(dir_okay=False, exists=False))
def main(source, template, output, resample_algorithm):
# Save source and template to GeoTIFF becasue it will need to be read by GDAL
temp_file = tempfile.NamedTemporaryFile(suffix=".tif", delete=False)
temp_source_path = temp_file.name
temp_file.close()
su.copy_bands_to_file(source, temp_source_path)
temp_file = tempfile.NamedTemporaryFile(suffix=".tif", delete=False)
temp_template_path = temp_file.name
temp_file.close()
su.copy_bands_to_file(template, temp_template_path)
# Wrap the source based on tamplate
wraped = gu.resample_with_gdalwarp(temp_source_path, temp_template_path, resample_algorithm)
# Save with snappy
name, geo_coding = su.get_product_info(template)[0:2]
bands = su.get_bands_info(source)
for i, band in enumerate(bands):
band['band_data'] = wraped.GetRasterBand(i+1).ReadAsArray()
su.write_snappy_product(output, bands, name, geo_coding)
# Clean up
try:
os.remove(temp_source_path)
os.remove(temp_template_path)
except Exception:
pass
if __name__ == "__main__":
try:
main()
except Exception as e:
print("ERROR:" + str(e))