Upload request_multiple_images_linux.py
Browse filesFile modified to get images 16-bits deep
- request_multiple_images_linux.py +215 -0
request_multiple_images_linux.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
'''
|
| 4 |
+
Author: Sebastian Cajas
|
| 5 |
+
|
| 6 |
+
We recommend to use eo-learn for more complex cases where you need multiple timestamps or high-resolution data for larger areas.
|
| 7 |
+
https://github.com/sentinel-hub/eo-learn
|
| 8 |
+
|
| 9 |
+
!pip install epiweeks
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
'''
|
| 13 |
+
|
| 14 |
+
from sentinelhub import SHConfig
|
| 15 |
+
import os
|
| 16 |
+
import datetime
|
| 17 |
+
import numpy as np
|
| 18 |
+
import matplotlib.pyplot as plt
|
| 19 |
+
import sys
|
| 20 |
+
from datetime import timedelta
|
| 21 |
+
sys.path.insert(0,'..')
|
| 22 |
+
from sentinelhub import MimeType, CRS, BBox, SentinelHubRequest, SentinelHubDownloadClient, DataCollection, bbox_to_dimensions, DownloadRequest
|
| 23 |
+
#from rename_linux import *
|
| 24 |
+
from epiweeks import Week, Year
|
| 25 |
+
from datetime import date
|
| 26 |
+
import glob, shutil
|
| 27 |
+
|
| 28 |
+
def cleaners(dataset, root_images):
|
| 29 |
+
for root, dirs, files in os.walk(dataset, topdown=True):
|
| 30 |
+
for name in files:
|
| 31 |
+
path = os.path.join(root, name)
|
| 32 |
+
if ".tiff" in path:
|
| 33 |
+
os.remove(path)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
return print("Ready")
|
| 37 |
+
|
| 38 |
+
def get_epi_weeks(start):
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
tdelta = timedelta(days = 7)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
edges = [(start + i*tdelta) for i in range(2)]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
return [(edges[i], edges[i+1]) for i in range(len(edges)-1)]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def plot_image(image, factor=1.0, clip_range = None, **kwargs):
|
| 51 |
+
"""
|
| 52 |
+
Utility function for plotting RGB images.
|
| 53 |
+
"""
|
| 54 |
+
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))
|
| 55 |
+
if clip_range is not None:
|
| 56 |
+
ax.imshow(np.clip(image * factor, *clip_range), **kwargs)
|
| 57 |
+
else:
|
| 58 |
+
ax.imshow(image * factor, **kwargs)
|
| 59 |
+
ax.set_xticks([])
|
| 60 |
+
ax.set_yticks([])
|
| 61 |
+
|
| 62 |
+
def download_multiple_images(coordinates, start, year, CLIENT_ID, CLIENT_SECRET):
|
| 63 |
+
|
| 64 |
+
# put here your credentials
|
| 65 |
+
CLIENT_ID = CLIENT_ID
|
| 66 |
+
CLIENT_SECRET = CLIENT_SECRET
|
| 67 |
+
|
| 68 |
+
config = SHConfig()
|
| 69 |
+
|
| 70 |
+
if CLIENT_ID and CLIENT_SECRET:
|
| 71 |
+
config.sh_client_id = CLIENT_ID
|
| 72 |
+
config.sh_client_secret=CLIENT_SECRET
|
| 73 |
+
|
| 74 |
+
if not config.sh_client_id or not config.sh_client_secret:
|
| 75 |
+
print("Warning! To use Process API, please provide the credentials (OAuth client ID and client secret).")
|
| 76 |
+
|
| 77 |
+
resolution = 10
|
| 78 |
+
bbox = BBox(bbox=coordinates, crs=CRS.WGS84)
|
| 79 |
+
bbox_size = bbox_to_dimensions(bbox, resolution=resolution)
|
| 80 |
+
|
| 81 |
+
#print(f'Image shape at {resolution} m resolution: {bbox_size} pixels')
|
| 82 |
+
|
| 83 |
+
slots = get_epi_weeks(start)
|
| 84 |
+
|
| 85 |
+
# Visualization purposes
|
| 86 |
+
d_init = slots[0][0].strftime('%m/%d/%Y')
|
| 87 |
+
d_end = slots[0][1].strftime('%m/%d/%Y')
|
| 88 |
+
print(f"Requested week slot: {d_init} - {d_end} ")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
script_Medellin = """
|
| 92 |
+
|
| 93 |
+
//VERSION=3
|
| 94 |
+
function setup() {
|
| 95 |
+
return {
|
| 96 |
+
input: [{
|
| 97 |
+
bands: ["B01", "B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B09", "B10", "B11", "B12"],
|
| 98 |
+
units: "DN"
|
| 99 |
+
}],
|
| 100 |
+
output: {
|
| 101 |
+
id: "default",
|
| 102 |
+
bands: 12,
|
| 103 |
+
sampleType: SampleType.UINT16
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
function evaluatePixel(sample) {
|
| 109 |
+
return [ sample.B01, sample.B02, sample.B03, sample.B04, sample.B05, sample.B06, sample.B07, sample.B08, sample.B8A, sample.B09, sample.B10, sample.B11, sample.B12]
|
| 110 |
+
}
|
| 111 |
+
"""
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
'''Check if directory exists, if not, create it'''
|
| 115 |
+
|
| 116 |
+
# You should change 'test' to your preferred folder.
|
| 117 |
+
# If folder doesn't exist, then create it.
|
| 118 |
+
|
| 119 |
+
# if windows:
|
| 120 |
+
path = os.path.abspath(os.getcwd()) + "//data" + "//" + year
|
| 121 |
+
# if linux: path = os.path.abspath(os.getcwd()) + "/data" + "/" year
|
| 122 |
+
|
| 123 |
+
if not os.path.isdir(path):
|
| 124 |
+
os.makedirs(path)
|
| 125 |
+
#print("created folder : ", path)
|
| 126 |
+
else:
|
| 127 |
+
#print(path, "Folder already exists. Rewriting*")
|
| 128 |
+
pass
|
| 129 |
+
|
| 130 |
+
def get_true_color_request(time_interval):
|
| 131 |
+
return SentinelHubRequest(
|
| 132 |
+
data_folder= 'data' + "/" + year,
|
| 133 |
+
evalscript=script_Medellin,
|
| 134 |
+
input_data=[
|
| 135 |
+
SentinelHubRequest.input_data(
|
| 136 |
+
data_collection=DataCollection.SENTINEL2_L1C,
|
| 137 |
+
#time_interval=('2015-11-1', '2015-11-20'), #time_interval,
|
| 138 |
+
time_interval=time_interval,
|
| 139 |
+
mosaicking_order='leastCC'
|
| 140 |
+
)
|
| 141 |
+
],
|
| 142 |
+
responses=[
|
| 143 |
+
SentinelHubRequest.output_response('default', MimeType.TIFF)
|
| 144 |
+
],
|
| 145 |
+
bbox=bbox,
|
| 146 |
+
size=bbox_size,
|
| 147 |
+
config=config
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# create a list of requests
|
| 151 |
+
list_of_requests = [get_true_color_request(slot) for slot in slots]
|
| 152 |
+
list_of_requests = [request.download_list[0] for request in list_of_requests]
|
| 153 |
+
|
| 154 |
+
image = SentinelHubDownloadClient(config=config).download(list_of_requests, max_threads=5)
|
| 155 |
+
print("Ended downloading")
|
| 156 |
+
|
| 157 |
+
# Verify if images are empty, if not, correct
|
| 158 |
+
img = np.array(image)
|
| 159 |
+
#with open('readme.txt', 'w') as f:
|
| 160 |
+
# f.write(img.shape)
|
| 161 |
+
|
| 162 |
+
print(img.shape)
|
| 163 |
+
print(img.max())
|
| 164 |
+
|
| 165 |
+
# Verify if images are empty, if not, correct
|
| 166 |
+
if img.sum() == 0:
|
| 167 |
+
print("Image empty: ", img.sum())
|
| 168 |
+
# Initialize
|
| 169 |
+
end = slots[0][1]
|
| 170 |
+
# Add delay by 1 day step
|
| 171 |
+
while img.sum() == 0: # tune
|
| 172 |
+
tdelta = timedelta(days = 1)
|
| 173 |
+
init = slots[0][0]
|
| 174 |
+
end = end + tdelta
|
| 175 |
+
corrected_slot = [(init, end)]
|
| 176 |
+
#print("Slot corrected to: ", corrected_slot)
|
| 177 |
+
list_of_requests = [get_true_color_request(slot) for slot in corrected_slot]
|
| 178 |
+
|
| 179 |
+
list_of_requests = [request.download_list[0] for request in list_of_requests]
|
| 180 |
+
image = SentinelHubDownloadClient(config=config).download(list_of_requests, max_threads=5)
|
| 181 |
+
img = np.array(image)
|
| 182 |
+
print("This image is being replaced..")
|
| 183 |
+
print("Slot corrected to: ", corrected_slot)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
else:
|
| 187 |
+
print(f"Pixel values: ", np.unique(img))
|
| 188 |
+
pass
|
| 189 |
+
|
| 190 |
+
'''
|
| 191 |
+
# Remove extra dimension
|
| 192 |
+
img = np.squeeze(image, axis = 0)
|
| 193 |
+
|
| 194 |
+
ncols = 4
|
| 195 |
+
nrows = 3
|
| 196 |
+
|
| 197 |
+
aspect_ratio = bbox_size[0] / bbox_size[1]
|
| 198 |
+
subplot_kw = {'xticks': [], 'yticks': [], 'frame_on': False}
|
| 199 |
+
|
| 200 |
+
plot_image(img[:,:,1], factor=1.0, cmap=plt.cm.Greys_r, vmin=0, vmax=120)
|
| 201 |
+
'''
|
| 202 |
+
return img
|
| 203 |
+
|
| 204 |
+
def get_folder_ID(root_images, img_format):
|
| 205 |
+
|
| 206 |
+
walker = "." + root_images
|
| 207 |
+
|
| 208 |
+
for root, dirs, files in os.walk(walker, topdown=True):
|
| 209 |
+
for name in files:
|
| 210 |
+
path = os.path.join(root, name)
|
| 211 |
+
if "response" in path:
|
| 212 |
+
|
| 213 |
+
folder_path = path.replace("/" + "response." + img_format, "")
|
| 214 |
+
|
| 215 |
+
return folder_path
|