-
Notifications
You must be signed in to change notification settings - Fork 0
/
i.landsat.download.py
154 lines (131 loc) · 3.92 KB
/
i.landsat.download.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
#!/usr/bin/env python2
#
##############################################################################
#
# MODULE: landsat-8-toolkit
#
# AUTHOR(S): Marc Becker
#
# PURPOSE: Search and download of Landsat-8 scenes
#
# DATE: 2018-06-06
#
# COPYRIGHT: (C) 2018 by Marc Becker, and the GRASS development team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
##############################################################################
#%module
#% description: Search and download of Landsat-8 scenes
#% keyword: imagery
#% keyword: Landsat
#% keyword: download
#%end
#%option G_OPT_M_DIR
#% key: output
#% description: Name for output directory where to store downloaded Sentinel data
#% required: yes
#% guisection: Output
#%end
#%option
#% key: date_from
#% description: Start date ('YYYY-MM-DD')
#% answer: 2017-01-01
#% type: string
#% required: yes
#%end
#%option
#% key: date_to
#% description: End date ('YYYY-MM-DD')
#% answer: 2017-12-01
#% type: string
#% required: yes
#%end
#%option
#% key: clouds
#% type: integer
#% description: Maximum cloud cover percentage for Sentinel scene
#% answer: 30
#% required: no
#% guisection: Filter
#%end
#%option
#% key: file_key
#% description: Download files
#% multiple: no
#% options: B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,BQA,MTL,ANG,thumb
#% answer: thumb
#% guisection: Filter
#%end
#%option G_OPT_V_MAP
#% label: Name of input vector map to define Area of Interest (AOI)
#% description: If not given than current computational extent is used
#% required: yes
#% guisection: Region
#%end
#%flag
#% key: l
#% description: List filtered products and exist
#% guisection: Print
#%end
import sys
import os
import json
from grass.pygrass.modules import Module
from grass.script import parser, tempdir, message, error, fatal, run_command
try:
from satsearch.search import Search, SatSearchError
except ImportError as e:
fatal(("Module requieres satsearch library: {}".format(e)))
class LansatDownloader(object):
def __init__(self):
self._scenes = None
def search(self):
""" Search for Landsat-8 scenes """
# Convert AOI to GeoJSON
aoi_file = tempdir() + '/aoi_geojson.geojson'
Module('v.out.ogr',
overwrite=True,
input=options['map'],
format='GeoJSON',
output=aoi_file)
# Reproject to espg 4326
aoi_file_re = tempdir() + '/aoi_reprojected.geojson'
os.system("ogr2ogr -t_srs epsg:4326 " + aoi_file_re + " " + aoi_file)
# Search for scenes
with open(aoi_file_re) as f:
aoi = json.dumps(json.load(f))
search = Search(date_from=options['date_from'], date_to=options['date_to'], satellite_name='Landsat-8',
intersects=aoi, cloud_from=0, cloud_to=options['clouds'])
self._scenes = search.scenes()
os.remove(aoi_file)
def download(self):
""" Download Landsat-8 scenes """
for scene in self._scenes:
try:
fname = scene.download(key=options['file_key'], path=options['output'], overwrite=True)[options['file_key']]
message(str(fname) + "... Done")
except:
error(str(fname) + "... Failed")
def list(self):
"""" Create list of files """
for scene in self._scenes:
print("Date: {0} - Cloud: {1} - ID: {2}".format(
scene.date,
scene.metadata['cloud_coverage'],
scene.scene_id
))
def main():
downloader = LansatDownloader()
downloader.search()
if flags['l']:
downloader.list()
return
else:
downloader.download()
return
return 0
if __name__ == '__main__':
options, flags = parser()
sys.exit(main())