-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
324 lines (256 loc) · 10 KB
/
main.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import os
import json
import boto
import urllib2
from boto.s3.key import Key
import boto3
import click
import logging
from copy import copy
from collections import OrderedDict
from datetime import date, timedelta
from elasticsearch import Elasticsearch, RequestError, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
import requests
from reader import csv_reader
logger = logging.getLogger('landsat8.meta')
bucket_name = os.getenv('BUCKETNAME', 'landsat8-meta')
thumbs_bucket_name = os.getenv('THUMBS_BUCKETNAME', 'ad-thumbnails')
s3 = boto3.resource('s3')
es_index = 'sat-api'
es_type = 'landsat8'
def get_credentials():
obj = get_instance_metadata()
return obj['iam']['security-credentials'].values()[0]
def connection_to_es(es_host, es_port, aws=False):
args = {}
if aws:
cred = get_credentials()
access_key = cred['AccessKeyId']
secret_access = cred['SecretAccessKey']
token = cred['Token']
region = os.getenv('AWS_DEFAULT_REGION', 'us-east-1')
awsauth = AWS4Auth(access_key, secret_access, region, 'es',
session_token=token)
args = {
'http_auth': awsauth,
'use_ssl': True,
'verify_certs': True,
'connection_class': RequestsHttpConnection
}
es = Elasticsearch(hosts=[{
'host': es_host,
'port': es_port
}], **args)
return es
def create_index(index_name, doc_type, es_host, es_port, **kwargs):
body = {
doc_type: {
'properties': {
'scene_id': {'type': 'string', 'index': 'not_analyzed'},
'product_id': {'type': 'string'},
'satellite_name': {'type': 'string'},
'cloud_coverage': {'type': 'float'},
'date': {'type': 'date'},
'data_geometry': {
'type': 'geo_shape',
'tree': 'quadtree',
'precision': '5mi'}
}
}
}
es = connection_to_es(es_host, es_port, kwargs['aws'])
es.indices.create(index=index_name, ignore=400)
es.indices.put_mapping(
doc_type=doc_type,
body=body,
index=index_name
)
def meta_constructor(metadata):
internal_meta = copy(metadata)
data_geometry = {
'type': 'Polygon',
'crs': {
'type': 'name',
'properties': {
'name': 'urn:ogc:def:crs:EPSG:8.9:4326'
}
},
'coordinates': [[
[metadata.get('upperRightCornerLongitude'), metadata.get('upperRightCornerLatitude')],
[metadata.get('upperLeftCornerLongitude'), metadata.get('upperLeftCornerLatitude')],
[metadata.get('lowerLeftCornerLongitude'), metadata.get('lowerLeftCornerLatitude')],
[metadata.get('lowerRightCornerLongitude'), metadata.get('lowerRightCornerLatitude')],
[metadata.get('upperRightCornerLongitude'), metadata.get('upperRightCornerLatitude')]
]]
}
body = OrderedDict([
('scene_id', metadata.get('sceneID')),
('product_id', metadata.get('LANDSAT_PRODUCT_ID')),
('satellite_name', 'landsat-8'),
('cloud_coverage', metadata.get('cloudCoverFull', 100)),
('date', metadata.get('acquisitionDate')),
('thumbnail', metadata.get('browseURL')),
('data_geometry', data_geometry)
])
body.update(internal_meta)
return body
def elasticsearch_updater(product_dir, metadata, **kwargs):
try:
es = connection_to_es(kwargs['es_host'], kwargs['es_port'], kwargs['aws'])
body = meta_constructor(metadata)
logger.info('Pushing to Elasticsearch')
try:
es.index(index=es_index, doc_type=es_type, id=body['scene_id'],
body=body)
except RequestError as e:
body['data_geometry'] = None
es.index(index=es_index, doc_type=es_type, id=body['scene_id'],
body=body)
except Exception as e:
logger.error('Unhandled error occured while writing to elasticsearch')
logger.error('Details: %s' % e.__str__())
def dynamodb_updater(product_dir, metadata, **kwargs):
client = boto3.client('dynamodb', region_name='us-east-1')
client.put_item(
TableName='landsat',
Item={
'scene_id': {
'S': metadata['sceneID']
},
'body': {
'S': json.dumps(meta_constructor(metadata))
}
},
ReturnValues='NONE',
ReturnConsumedCapacity='NONE',
ReturnItemCollectionMetrics='NONE'
)
print('Posted %s to DynamoDB' % metadata['sceneID'])
def thumbnail_writer(product_dir, metadata, **kwargs):
"""
Extra function to download images from USGS, then upload to S3 and call
the ES metadata writer afterwards.
"""
# Download original thumbnail
output_file = metadata['sceneID'] + '.jpg'
thumbnail = 'https://' + thumbs_bucket_name + '.s3.amazonaws.com/' + output_file
new_thumb = requests.get(thumbnail)
if new_thumb.status_code != 200:
# Upload thumbnail to S3
thumbs_bucket_name2 = os.getenv('THUMBS_BUCKETNAME', 'ad-thumbnails')
try:
print('uploading %s' % output_file)
c = boto.connect_s3()
b = c.get_bucket(thumbs_bucket_name2)
k = Key(b, name=output_file)
k.set_metadata('Content-Type', 'image/jpeg')
r = requests.get(metadata['browseURL'])
k.set_contents_from_string(r.content, policy='public-read')
except Exception as e:
print(e)
# Update metadata record
metadata['thumbnail'] = thumbnail
dynamodb_updater(product_dir, meta_constructor(metadata), **kwargs)
elasticsearch_updater(product_dir, metadata, **kwargs)
return
def file_writer(product_dir, metadata):
body = meta_constructor(metadata)
if not os.path.exists(product_dir):
os.makedirs(product_dir)
f = open(os.path.join(product_dir, body['scene_id'] + '.json'), 'w')
f.write(json.dumps(body))
logger.info('saving to disk at %s' % product_dir)
f.close()
def s3_writer(product_dir, metadata):
# make sure product_dir doesn't start with slash (/) or dot (.)
if product_dir.startswith('.'):
product_dir = product_dir[1:]
if product_dir.startswith('/'):
product_dir = product_dir[1:]
body = meta_constructor(metadata)
key = os.path.join(product_dir, body['scene_id'] + '.json')
s3.Object(bucket_name, key).put(Body=json.dumps(body), ACL='public-read', ContentType='application/json')
logger.info('saving to s3 at %s')
def last_updated(today):
""" Gets the latest time a product added to Elasticsearch """
bucket = s3.Bucket(bucket_name)
start_day = today.day
start_month = today.month
yr_counter = 0
while True:
m_counter = 0
year = today.year - yr_counter
if year < 2015:
break
while True:
month = start_month - m_counter
if month == 0:
start_month = 12
break
d_counter = 0
while True:
day = start_day - d_counter
if day == 0:
start_day = 31
break
path = os.path.join(str(year), str(month), str(day))
logger.info('checking %s' % path)
objs = bucket.objects.filter(Prefix=path).limit(1)
if list(objs):
return date(year, month, day)
d_counter += 1
m_counter += 1
yr_counter += 1
return None
@click.command()
@click.argument('ops', metavar='<operations: choices: s3 | es | disk | thumbs>', nargs=-1)
@click.option('--start', default=None, help='Start Date. Format: YYYY-MM-DD')
@click.option('--end', default=None, help='End Date. Format: YYYY-MM-DD')
@click.option('--es-host', default='localhost', help='Elasticsearch host address')
@click.option('--es-port', default=9200, type=int, help='Elasticsearch port number')
@click.option('--folder', default='.', help='Destination folder if is written to disk')
@click.option('--download', is_flag=True,
help='Sets the updater to download the metadata file first instead of streaming it')
@click.option('--aws', is_flag=True, default=False,
help='Uses AWS STS to obtain aws credentials for accessing the various services')
@click.option('--download-folder', default=None,
help='The folder to save the downloaded metadata to. Defaults to a temp folder')
@click.option('-v', '--verbose', is_flag=True)
@click.option('--concurrency', default=20, type=int, help='Process concurrency. Default=20')
def main(ops, start, end, es_host, es_port, folder, download,
aws, download_folder, verbose, concurrency):
if not ops:
raise click.UsageError('No Argument provided. Use --help if you need help')
accepted_args = {
'es': elasticsearch_updater,
's3': s3_writer,
'disk': file_writer,
'thumbs': thumbnail_writer,
'db': dynamodb_updater
}
writers = []
for op in ops:
if op in accepted_args.keys():
writers.append(accepted_args[op])
else:
raise click.UsageError('Operation (%s) is not supported' % op)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
if verbose:
ch.setLevel(logging.INFO)
else:
ch.setLevel(logging.ERROR)
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
if 'es' in ops or 'thumbs' in ops:
create_index(es_index, es_type, es_host, es_port, aws=aws)
if not start and not end:
delta = timedelta(days=3)
start = date.today() - delta
start = '{0}-{1}-{2}'.format(start.year, start.month, start.day)
csv_reader(folder, writers, start_date=start, end_date=end, download=download, download_path=download_folder,
num_worker_threads=concurrency, es_host=es_host, es_port=es_port, aws=aws)
if __name__ == '__main__':
main()