-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.py
77 lines (67 loc) · 3.74 KB
/
script.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
from sentinelsat.sentinel import SentinelAPI, read_geojson, geojson_to_wkt
from osgeo import gdal
import argparse
import zipfile
import string
import json
import os
def extracter_zip(file_output,title,P_directory_name):
dirlist = os.listdir(str(file_output+P_directory_name))
P_extract=False
for i_dirlist in dirlist :
if(str(title+'.SAFE')==i_dirlist):
P_extract=True
if P_extract==False:
print('pleasse attend to extract product ...')
with zipfile.ZipFile(file_output+P_directory_name+'/'+title+'.zip', 'r') as zip_ref:
zip_ref.extractall(file_output+P_directory_name)
def createDirectory(file_output,name):
# Create target Directory if don't exist
dirName = file_output+name
if not os.path.exists(dirName):
os.mkdir(dirName)
def show_product():
#show the List of data found
P_list=api.to_geodataframe(products).uuid
for i in range(len(P_list)):
print('*id: '+"{:0>3d}".format(i)+' *Title: '+ api.to_geodataframe(products).title[i]+' *'+ (api.to_geodataframe(products).summary[i])[-15:-1]+"B *cloud%: {:.3f}".format(api.to_geodataframe(products).cloudcoverpercentage[i]))
def save_FeatureCollection() :
# GeoJSON FeatureCollection containing footprints and metadata of the scenes
data=api.to_geojson(products)
with open('./FeatureCollection/data.json', 'w') as outfile:
json.dump(data, outfile)
parser = argparse.ArgumentParser(usage='This script allows you to Search Sentinel-2 imagery using Sentinelsat API')
parser.add_argument('-u', metavar='user', type=str, help='a string for your username',dest='user')
parser.add_argument('-p', metavar='password', type=str,help='a string for your password',dest='password')
parser.add_argument('-s', metavar='Date', type=str,dest='start_date', help='a string for start date searching for images [YYYYMMDD]')
parser.add_argument('-e', metavar='Date', type=str,dest='end_date', help='a string for end date searching for images [YYYYMMDD]')
parser.add_argument('-plat', metavar='platform', type=str,default='Sentinel-2',help='a string for your platform name (default: Sentinel-2)',dest='platform_name')
parser.add_argument('-l', metavar='Level', type=str,default='Level-2A',help='a string for your processing level (default: Level-2A)',dest='processing_level')
parser.add_argument('-in', metavar='File', type=str,default='./Geojson/polygone.geojson',help='The path of your geojson file',dest='file_input')
parser.add_argument('-out', metavar='folder', type=str,dest='file_output',default='./products/', help='The path of directory where to save downloaded files')
args = parser.parse_args()
#read geojson file
footprint = geojson_to_wkt(read_geojson(args.file_input))
# connect to the API
api = SentinelAPI(args.user,args.password,'https://scihub.copernicus.eu/dhus')
# search by polygon
print ("The coordinates of our geometry: \n" + footprint)
products = api.query(area=footprint,initial_date=args.start_date,end_date=args.end_date,platformname=args.platform_name,processinglevel=args.processing_level)
#number of products found
print("-- The number of data found: "+str(len(products))+" --")
# download and extract single scene by known product id
P_download=input("do you want to download a product(T/F): ")
while (P_download=='T'):
save_FeatureCollection()
show_product()
try:
P_id= input("Enter product id: ")
P_directory_name=str(api.to_geodataframe(products).beginposition[int(P_id)])
P_directory_name=P_directory_name.replace(" ","_")[:19]
createDirectory(args.file_output,P_directory_name)
print("-"+P_directory_name+"-")
api.download(api.to_geodataframe(products).uuid[int(P_id)], directory_path=args.file_output+P_directory_name)
title=api.to_geodataframe(products).title[int(P_id)]
extracter_zip(args.file_output,title,P_directory_name)
except :
P_download=input("do you want to download a product(T/F): ")