I would like to download Sentinel-2 bands using Python and the Sentinel Hub API.
Below is my code, first the two functions, and then the request.
def download_function(start_date: datetime,
end_date: datetime,
save_folder: str,
eval_script: str,
aoi_box: tuple,
config,
time_step=2):
"""
DOC STRINGS
Definition:
Utilization of a while loop to define start/end dates (by default 2 week windows) in which to
look and ultimately return the least cloudy scene using the subsequent scene_request() function.
"""
time_step_week = timedelta(weeks = time_step)
current_date = start_date
while current_date <= end_date:
current_date_start = current_date
current_date_end = current_date + time_step_week
scene_request(cds = current_date_start,
cde = current_date_end,
save_folder=save_folder,
eval_script=eval_script,
aoi_box=aoi_box,
config=config)
current_date = current_date_end
print(f"Current date: {current_date}")
def scene_request(cds: datetime,
cde: datetime,
save_folder: str,
eval_script: str,
aoi_box: tuple,
config):
"""
DOC STRINGS
Definition:
Saves the least cloudy scene available using the parameters given (eval_script, aoi_box)
"""
aoi_bbox = BBox(bbox=aoi_box, crs=CRS.WGS84)
aoi_size = bbox_to_dimensions(aoi_bbox, resolution=10)
week = cde.strftime("%Y-%m-%d")
save_path = os.path.join(save_folder, week)
print(save_path)
scene = SentinelHubRequest(
data_folder=save_path,
evalscript=eval_script,
input_data==
SentinelHubRequest.input_data(
data_collection=DataCollection.SENTINEL2_L2A.define_from(
"s2l2a", service_url=config.sh_base_url
),
time_interval=(cds, cde),
mosaicking_order=MosaickingOrder.LEAST_CC,
maxcc=0.35,
)
],
responses==SentinelHubRequest.output_response("default", MimeType.TIFF)],
bbox=aoi_bbox,
size=aoi_size,
config=config,
)
scene.get_data(save_data=True)
Next is where I define parameters and call the two functions.
import fiona
# Parameter definitions
start_date, end_date = datetime(2020, 7, 1), datetime(2020, 7, 14)
save_dir = r"C:\Users\save_folder"
eval_script = """
//VERSION=3
function setup() {
return {
input: {
bands: "B02","B03","B04"],
units: "REFLECTANCE",
maxcc: 35
}],
output: {
bands: 3,
}
};
}
function evaluatePixel(sample) {
return sample.B02,
sample.B03,
sample.B04];}
"""
shp_file = r"C:\Users\spatial_extent.shp"
c = fiona.open(shp_file)
bounding_box = c.bounds
print(bounding_box)
bounding_box = (6.391419, 50.936364,
6.514317, 50.980479)
config = SHConfig("cdse")
download_function(start_date=start_date,
end_date=end_date,
save_folder=save_dir,
eval_script=eval_script,
aoi_box=bounding_box,
config=config)
If I enter the bounding box as given here, it returns scenes no problem. If I use the bounds of my shapefile: (6.12370823171227, 50.4297129912245, 7.729300891801812, 51.44399790755677)
This returns an error:
DownloadFailedException: Failed to download from: https://sh.dataspace.copernicus.eu/api/v1/process with HTTPError: 400 Client Error: Bad Request for url: https://sh.dataspace.copernicus.eu/api/v1/process Server response: "{"status": 400, "reason": "Bad Request", "message": "Invalid request", "code": "COMMON_BAD_PAYLOAD", "errors": {"parameter": "output->width", "invalidValue": 11597, "violation": "must be less than or equal to 2500", "description": "The request image width. Must be an integer between 1 and 2500.
Only one pair of parameters "width"/"height" or "resx"/"resy" must be set at the same time."}, {"parameter": "output->height", "invalidValue": 10950, "violation": "must be less than or equal to 2500", "description": "The request image height. Must be an integer between 1 and 2500.
Only one pair of parameters "width"/"height" or "resx"/"resy" must be set at the same time."}]}"
If my understanding is correct here, the bounding box is providing the height and width, and it appears as though the API has a restriction on how large of an area I can request.
Is that correct? And if so, short of running the same code over and over with slight shifts to the boundingbox space, and then meshing it all together after the fact, are there alternative open source options to access the free Sentinel-2 data?