-
Notifications
You must be signed in to change notification settings - Fork 3
/
push-to-gee.py
133 lines (107 loc) · 4.42 KB
/
push-to-gee.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
# -*- coding: utf-8 -*-
from dotenv import load_dotenv
load_dotenv('.env')
import logging
logging.basicConfig(filename='logs/push-2-gee.log', level=logging.INFO)
import ast
import glob
import json
import os
import subprocess
from datetime import datetime
from dbio import *
scale_factor = 10000
output_path = os.getenv('OUTPUT_PATH')
final_output = os.getenv('POST_PROCESS_OUTPUT_PATH')
gdal_path = os.getenv('GDAL_PATH')
manifest_dir = os.getenv('MANIFESTS_PATH')
cloud_path = os.getenv('GCS_PATH')
gee_asset_path = os.getenv('GEE_ASSET_PATH')
calc = '{0}gdal_calc.py -A %s --calc="A*{1}" --outfile={2}%s --type=UInt16'.format(gdal_path, scale_factor, final_output)
_cp_to_gs = 'gsutil cp {0}%s {1}'.format(final_output, cloud_path)
_upload_to_gee = 'earthengine upload image --manifest "{0}%s.json"'.format(manifest_dir)
properties = ['acquisitiontype', 'lastorbitnumber', 'lastrelativeorbitnumber', 'missiondatatakeid', 'orbitdirection',
'orbitnumber', 'platformidentifier', 'polarisationmode', 'producttype', 'relativeorbitnumber',
'sensoroperationalmode', 'swathidentifier']
def get_processed_images(table_name):
query = "SELECT id, title, beginposition, endposition, {} FROM {} WHERE processed={} AND slave={} " \
"AND uploadedtogs={} ORDER BY title;".format(','.join(properties), table_name, True, False, False)
return get_query(query)
def main():
table_name = os.getenv('TABLE_NAME')
processed_images = get_processed_images(table_name)
for image in processed_images:
output_files = glob.glob(output_path + '*{}*.tif'.format(image[1]))
# push to gee
# name
manifest_name = '{}/{}'.format(gee_asset_path, image[1])
# tilesets and bands
manifest_tilesets = []
manifest_bands = []
for index, output in enumerate(output_files):
file_name = output.split(output_path)[1]
# convert to uint 16
calc_command = calc % (output, file_name)
result = subprocess.check_output(calc_command, shell=True)
print(result)
# push to gs
cp_to_gs = _cp_to_gs % (file_name)
result = subprocess.check_output(cp_to_gs, shell=True)
print(result)
if 'VH' in file_name:
tileset_id = 'tileset_for_band_VH_{}'.format(index)
band_id = 'VH'
elif 'VV' in file_name:
tileset_id = 'tileset_for_band_VV_{}'.format(index)
band_id = 'VV'
_tileset = {
'id': tileset_id,
'sources': [
{
'uris': [
'{}/{}'.format(cloud_path, file_name)
]
}
]
}
manifest_tilesets.append(_tileset)
_band = {
'id': band_id,
'tileset_id': tileset_id
}
manifest_bands.append(_band)
# properties
manifest_properties = {}
start_index = 4
for _property in properties:
manifest_properties[_property] = image[start_index]
start_index += 1
manifest_properties['scale_factor'] = scale_factor
# start time
manifest_start_time = {
'seconds': int(round((image[2] - datetime(1970, 1, 1)).total_seconds()))
}
# end time
manifest_end_time = {
'seconds': int(round((image[3] - datetime(1970, 1, 1)).total_seconds()))
}
final_manifest = {
'name': manifest_name,
'tilesets': manifest_tilesets,
'bands': manifest_bands,
'start_time': manifest_start_time,
'end_time': manifest_end_time,
'properties': manifest_properties
}
with open('{}{}.json'.format(manifest_dir, image[1]), 'w') as manifest_file:
json.dump(ast.literal_eval(json.dumps(final_manifest)), manifest_file, ensure_ascii=False, indent=4)
upload_to_gee = _upload_to_gee % (image[1])
result = subprocess.check_output(upload_to_gee, shell=True)
print(result)
if 'ID:' in result:
task_id = result.split("ID:")[1].strip()
# save the info
query = "UPDATE {} SET uploadedtogs=TRUE, ee_task_id='{}' WHERE id='{}'".format(table_name, task_id, image[0])
update_query(query)
if __name__ == '__main__':
main()