3
3
# Landsat Util
4
4
# License: CC0 1.0 Universal
5
5
6
- import sys
7
6
import argparse
8
7
import textwrap
9
8
import json
16
15
import pycurl
17
16
from boto .exception import NoAuthHandlerFound
18
17
19
- from downloader import Downloader , IncorrectSceneId
18
+ from downloader import Downloader , IncorrectSceneId , RemoteFileDoesntExist , USGSInventoryAccessMissing
20
19
from search import Search
21
20
from uploader import Uploader
22
21
from utils import reformat_date , convert_to_integer_list , timer , exit , get_file , convert_to_float_list
62
61
63
62
--json Returns a bare JSON response
64
63
64
+ --geojson Returns a geojson response
65
+
65
66
-h, --help Show this help message and exit
66
67
67
68
Download:
108
109
109
110
--force-unzip Force unzip tar file
110
111
112
+ --username USGS Eros account Username (only works if the account has special
113
+ inventory access). Username and password as a fallback if the image
114
+ is not found on AWS S3 or Google Storage
115
+
116
+ --password USGS Eros account Password
117
+
111
118
Process:
112
119
landsat.py process path [-h] [-b --bands] [-p --pansharpen]
113
120
@@ -193,6 +200,7 @@ def args_options():
193
200
parser_search .add_argument ('--lon' , type = float , help = 'The longitude' )
194
201
parser_search .add_argument ('--address' , type = str , help = 'The address' )
195
202
parser_search .add_argument ('--json' , action = 'store_true' , help = 'Returns a bare JSON response' )
203
+ parser_search .add_argument ('--geojson' , action = 'store_true' , help = 'Returns a geojson response' )
196
204
197
205
parser_download = subparsers .add_parser ('download' ,
198
206
help = 'Download images from Google Storage' )
@@ -218,6 +226,10 @@ def args_options():
218
226
'50.2682767372753' )
219
227
parser_download .add_argument ('-u' , '--upload' , action = 'store_true' ,
220
228
help = 'Upload to S3 after the image processing completed' )
229
+ parser_download .add_argument ('--username' , help = 'USGS Eros account Username (only works if the account has' +
230
+ ' special inventory access). Username and password as a fallback if the image' +
231
+ 'is not found on AWS S3 or Google Storage' )
232
+ parser_download .add_argument ('--password' , help = 'USGS Eros username, used as a fallback' )
221
233
parser_download .add_argument ('--key' , help = 'Amazon S3 Access Key (You can also be set AWS_ACCESS_KEY_ID as '
222
234
'Environment Variables)' )
223
235
parser_download .add_argument ('--secret' , help = 'Amazon S3 Secret Key (You can also be set AWS_SECRET_ACCESS_KEY '
@@ -328,38 +340,45 @@ def main(args):
328
340
limit = args .limit ,
329
341
start_date = args .start ,
330
342
end_date = args .end ,
331
- cloud_max = args .cloud )
343
+ cloud_max = args .cloud ,
344
+ geojson = args .geojson )
332
345
333
- if result ['status' ] == 'SUCCESS' :
334
- if args .json :
335
- return json .dumps (result )
346
+ if 'status' in result :
336
347
337
- if args .latest > 0 :
338
- datelist = []
339
- for i in range (0 , result ['total_returned' ]):
340
- datelist .append ((result ['results' ][i ]['date' ], result ['results' ][i ]))
348
+ if result ['status' ] == 'SUCCESS' :
349
+ if args .json :
350
+ return json .dumps (result )
341
351
342
- datelist .sort (key = lambda tup : tup [0 ], reverse = True )
343
- datelist = datelist [:args .latest ]
352
+ if args .latest > 0 :
353
+ datelist = []
354
+ for i in range (0 , result ['total_returned' ]):
355
+ datelist .append ((result ['results' ][i ]['date' ], result ['results' ][i ]))
344
356
345
- result ['results' ] = []
346
- for i in range (0 , len (datelist )):
347
- result ['results' ].append (datelist [i ][1 ])
348
- result ['total_returned' ] = len (datelist )
357
+ datelist .sort (key = lambda tup : tup [0 ], reverse = True )
358
+ datelist = datelist [:args .latest ]
349
359
350
- else :
351
- v .output ('%s items were found' % result ['total' ], normal = True , arrow = True )
360
+ result ['results' ] = []
361
+ for i in range (0 , len (datelist )):
362
+ result ['results' ].append (datelist [i ][1 ])
363
+ result ['total_returned' ] = len (datelist )
352
364
353
- if result ['total' ] > 100 :
354
- return ['Over 100 results. Please narrow your search' , 1 ]
355
- else :
356
- v .output (json .dumps (result , sort_keys = True , indent = 4 ), normal = True , color = 'green' )
357
- return ['Search completed!' ]
365
+ else :
366
+ v .output ('%s items were found' % result ['total' ], normal = True , arrow = True )
367
+
368
+ if result ['total' ] > 100 :
369
+ return ['Over 100 results. Please narrow your search' , 1 ]
370
+ else :
371
+ v .output (json .dumps (result , sort_keys = True , indent = 4 ), normal = True , color = 'green' )
372
+ return ['Search completed!' ]
373
+
374
+ elif result ['status' ] == 'error' :
375
+ return [result ['message' ], 1 ]
376
+
377
+ if args .geojson :
378
+ return json .dumps (result )
358
379
359
- elif result ['status' ] == 'error' :
360
- return [result ['message' ], 1 ]
361
380
elif args .subs == 'download' :
362
- d = Downloader (download_dir = args .dest )
381
+ d = Downloader (download_dir = args .dest , usgs_user = args . username , usgs_pass = args . password )
363
382
try :
364
383
bands = convert_to_integer_list (args .bands )
365
384
@@ -406,6 +425,8 @@ def main(args):
406
425
return ['Download Completed' , 0 ]
407
426
except IncorrectSceneId :
408
427
return ['The SceneID provided was incorrect' , 1 ]
428
+ except (RemoteFileDoesntExist , USGSInventoryAccessMissing ) as e :
429
+ return [e .message , 1 ]
409
430
410
431
411
432
def process_image (path , bands = None , verbose = False , pansharpen = False , ndvi = False , force_unzip = None ,
@@ -459,10 +480,8 @@ def __main__():
459
480
global parser
460
481
parser = args_options ()
461
482
args = parser .parse_args ()
462
- if args .subs == 'search' :
463
- if args .json :
464
- print main (args )
465
- sys .exit (0 )
483
+ if args .subs == 'search' and (hasattr (args , 'json' ) or hasattr (args , 'geojson' )):
484
+ print (main (args ))
466
485
else :
467
486
with timer ():
468
487
exit (* main (args ))
0 commit comments