| from sentinelsat import SentinelAPI, read_geojson, geojson_to_wkt |
| from datetime import datetime, timedelta,date |
| import zipfile |
| import rasterio |
| from rasterio.plot import show |
| from PIL import Image |
| import matplotlib.pyplot as plt |
| import numpy as np |
| import pandas as pd |
| import os |
| from glob import glob |
| from tqdm import tqdm |
| |
| |
| import xmltodict |
| import json |
| import warnings |
| import shutil |
| from shapely.geometry import Point |
| from shapely.geometry.polygon import Polygon |
| warnings.filterwarnings('ignore') |
|
|
| |
| def unzip(): |
| files = glob('*.zip') |
| for file in files: |
| with zipfile.ZipFile(file, 'r') as zip_ref: |
| zip_ref.extractall() |
|
|
|
|
| |
| def select_best_cloud_coverage_tile(): |
| tile_names = {} |
| cld_prob = [] |
| folders = glob('*.SAFE') |
| for fold in folders: |
| metadata_path = fold+"/MTD_MSIL2A.xml" |
| xml_file=open(metadata_path,"r") |
| xml_string=xml_file.read() |
| python_dict=xmltodict.parse(xml_string) |
| cld = float(python_dict["n1:Level-2A_User_Product"]["n1:Quality_Indicators_Info"]["Cloud_Coverage_Assessment"]) |
| tile_names[cld] = fold |
| cld_prob.append(cld) |
| name = tile_names[min(cld_prob)] |
| dates = name.split('_')[2][:8] |
| acquisition_date = datetime.strptime(dates, "%Y%m%d") |
| today = datetime.now() |
| delta = (today - acquisition_date) |
| days_ago = delta.days |
| return name,min(cld_prob),days_ago |
|
|
| |
| def find_good_tile(df, point): |
| for row in tqdm(df.itertuples(),total=len(df)): |
| tile_name = row.tiles |
| coordinate = row.coords |
| x = coordinate.replace(']','') |
| x = x.replace('[','') |
| x = x.replace("'",'') |
| tab = [[float(x.split(",")[i]),float(x.split(",")[i+1])] for i in range(0,len(x.split(",")),2)] |
| polygon = Polygon(tab) |
| result = polygon.contains(point) |
| if result: |
| print(tile_name) |
| return "S2B_MSIL2A_20230104T103329_N0509_R108_T30NUL_20230104T132032.SAFE.zip" |
| |
| return "S2B_MSIL2A_20230104T103329_N0509_R108_T30NUL_20230104T132032.SAFE.zip" |
|
|
| |
| def delete_tiles(): |
| files = glob('*.zip') |
| folders1 = glob('*.SAFE') |
| folders2 = glob("*.tmp") |
| for f in files: |
| os.remove(f) |
| for fold in folders1: |
| shutil.rmtree(fold, ignore_errors=True) |
|
|
| for fold in folders2: |
| shutil.rmtree(fold, ignore_errors=True) |
|
|
|
|
|
|
|
|
|
|