-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_images.py
88 lines (64 loc) · 2.5 KB
/
get_images.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
import ee
import os
import geojson
import requests
import time
from PIL import Image
import argparse
ee.Initialize()
def get_images(location, start_date, end_date):
#Program starts
if os.path.exists("Images/") is False:
os.mkdir("Images/")
start = time.time()
filename = location + ".geojson"
with open("Locations\\" + filename) as f:
gj = geojson.load(f)
coords = gj['features'][0]['geometry']['coordinates']
aoi = ee.Geometry.Polygon(coords)
ffa_db = (ee.ImageCollection('COPERNICUS/S1_GRD')
.filter(ee.Filter.eq('instrumentMode', 'IW'))
.filterBounds(aoi)
.filterDate(ee.Date(start_date), ee.Date(end_date))
.sort('system:time_start'))
im_list = ffa_db.toList(ffa_db.size())
size = ffa_db.size().getInfo()
mode = "VH"
if mode == "VV":
min_vision = -25
max_vision = 10
elif mode == "VH":
min_vision = -20
max_vision = 0
acq_times = ffa_db.aggregate_array('system:time_start').getInfo()
dates = [time.strftime("-%Y%m%d-%H%M%S", time.gmtime(acq_time/1000)) for acq_time in acq_times]
images_path = r"Images"
if os.path.exists("Images/" + location) is False:
os.mkdir("Images/" + location)
for i,date in enumerate(dates):
url = ee.Image(im_list.get(i)).select(mode).clip(aoi).getThumbURL({'min': min_vision, 'max': max_vision})
img_data = requests.get(url).content
info_img = str(i+1) + date+ '.png'
img_name = os.path.join(images_path, location, f"{location}_{info_img}" )
#img_name = r'Images/Shenzhen bay/Shenzhen_' + str(i+1) + date+ '.png'
with open(img_name, 'wb') as handler:
handler.write(img_data)
RGB = Image.open(img_name).convert('RGB').save(img_name)
end = time.time()
minutes = (end -start)/60
format_minutes = "{:.2f}".format(minutes)
print(f"The program finished in {format_minutes} minutes")
def main():
# Create the parser
parser = argparse.ArgumentParser()
parser.add_argument('--location', type=str, required=True) #YYYY-MM-DD
parser.add_argument('--start_date', type=str, required=True)
parser.add_argument('--end_date', type=str, required=True)
# Parse the argument
args = parser.parse_args()
location = args.location
start_date = args.start_date
end_date = args.end_date
get_images(location, start_date, end_date)
if __name__ == "__main__":
main()