File size: 1,980 Bytes
2cdce84
 
 
 
 
 
 
 
74b951c
 
 
 
2cdce84
 
 
46c0e18
2cdce84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46c0e18
2cdce84
 
 
 
 
 
 
 
 
46c0e18
2cdce84
 
 
 
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
import os
import utils
import rioxarray as rx
from senHub import SenHub
from sentinelhub import  SHConfig, MimeType


config = SHConfig()
config.instance_id       = '352670fb-2edf-4abd-90c8-437485a2403e'
config.sh_client_id      = 'ca95f10f-443c-4c60-9a36-98950292bb9b'
config.sh_client_secret  = 'rNFGRxGNiNFrXJfGyHIkVRyGOrdWNsfI'
config.sh_timesfm_IP = "34.121.141.161"

def Download_image_in_given_date(clientName, metric, df, field, date, mime_type = MimeType.TIFF):
    sen_obj = SenHub(config, mime_type = mime_type)
    download_path = f'./data/{clientName}/raw/{metric}/{date}/field_{field}/'
    bbox = utils.calculate_bbox(df, field)
    evalscript = utils.Scripts[metric]
    sen_obj.set_dir(download_path)
    sen_obj.make_bbox(bbox)
    sen_obj.make_request(evalscript, date)
    data = sen_obj.download_data()
    return data

def mask_downladed_image(clientName, metric, df, field, date):
    download_path = utils.get_downloaded_location_img_path(clientName, metric, date, field)
    im = rx.open_rasterio(download_path)
    field_vals = df.loc[df['name'] == field]
    field_geom = field_vals.geometry 
    crs = field_vals.crs
    clipped = im.rio.clip(field_geom, crs, drop=True)
    save_dir_path = f'./data/{clientName}/processed/{metric}/{date}/field_{field}/'
    os.makedirs(save_dir_path, exist_ok=True)
    save_tiff_path = save_dir_path + 'masked.tiff'
    clipped.rio.to_raster(save_tiff_path)
    return save_tiff_path

def convert_maske_image_to_geodataframe(clientName, metric, df, field, date, crs):
    imagePath = utils.get_masked_location_img_path(clientName, metric, date, field)
    im = rx.open_rasterio(imagePath)
    gdf = utils.tiff_to_geodataframe(im, metric, date, crs)
    save_dir_path = f'./data/{clientName}/curated/{metric}/{date}/field_{field}/'
    os.makedirs(save_dir_path, exist_ok=True)
    save_geojson_path = save_dir_path + 'masked.geojson'
    gdf.to_file(save_geojson_path, driver='GeoJSON')
    return save_geojson_path