izisat 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
izisat/__init__.py ADDED
File without changes
izisat/izisentinel.py ADDED
@@ -0,0 +1,161 @@
1
+
2
+ from izisat.misc.raster_processing import RasterProcessing
3
+ from izisat.misc.connections import Connections
4
+ from izisat.misc.utils import Utils
5
+ from izisat.misc.files import Files
6
+ from loguru import logger
7
+ import geopandas as gpd
8
+ from datetime import datetime, timedelta
9
+ from shapely.geometry import box
10
+
11
+
12
+ class IZISentinel:
13
+ def __init__(self, output_base_path, username, password):
14
+ """Initializes the Sentinel2_Band_Downloader instance."""
15
+ self.output_base_path = output_base_path
16
+ self.access_token, self.refresh_token, self.dt_access_token = self.connect_to_api(username, password)
17
+
18
+ def connect_to_api(self, username, password):
19
+ """Connects to the Sentinel API and obtains an access token."""
20
+ connections = Connections()
21
+ access_token, refresh_token, dt_access_token = connections.access_token(username, password)
22
+ return access_token, refresh_token, dt_access_token
23
+
24
+ def construct_query(self, geodataframe, end_date, cloud_cover_percentage, type, platform_name):
25
+ """Constructs a query for retrieving Sentinel-2 products based on specified parameters."""
26
+ utils = Utils()
27
+ start_date = (end_date - timedelta(days=1)).strftime('%Y-%m-%d')
28
+ end_date = end_date.strftime('%Y-%m-%d')
29
+ projected_gdf = gdf.to_crs(geodataframe.estimate_utm_crs('SIRGAS 2000'))
30
+ geodataframe = projected_gdf.buffer(1000)
31
+ geodataframe = geodataframe.to_crs(4674)
32
+ minx, miny, maxx, maxy = geodataframe.total_bounds
33
+ bbox_geom = box(minx, miny, maxx, maxy)
34
+ bbox_gdf = gpd.GeoDataFrame(geometry=[bbox_geom], crs=geodataframe.crs)
35
+ footprint = bbox_gdf.iloc[0].geometry.wkt
36
+ query = utils.construct_query_for_sentinel2_products(footprint, start_date, end_date, cloud_cover_percentage, type, platform_name)
37
+ return query
38
+
39
+ def products_from_sentinel_2(self, params):
40
+ """Retrieves Sentinel-2 products based on the provided query parameters."""
41
+ connections = Connections()
42
+ products = connections.retrieve_sent_prod_from_query(params)
43
+ return products
44
+
45
+ def get_products_info(self, products):
46
+ """Retrieves information about Sentinel-2 products."""
47
+ utils = Utils()
48
+ products_info = utils.retrieve_products_info(products)
49
+ return products_info
50
+
51
+ def output_folder(self, products_info, bands_dict):
52
+ """Creates output folders to save downloaded bands."""
53
+ files = Files()
54
+ directory_paths = files.create_output_folders(self.output_base_path, products_info, bands_dict)
55
+ return directory_paths
56
+
57
+ def get_bands_links(self, products_info, bands_dict):
58
+ """Retrieves links to bands for Sentinel-2 products."""
59
+ connections = Connections()
60
+ bands_links = connections.retrieve_bands_links(self.access_token, products_info, bands_dict)
61
+ return bands_links
62
+
63
+ def download_band(self, products_info, bands_link, base_dir, tile):
64
+ """Downloads bands for Sentinel-2 products based on the provided links."""
65
+ connections = Connections()
66
+ connections.download_bands(self.access_token, products_info, bands_link, base_dir, self.dt_access_token, self.refresh_token, tile)
67
+
68
+ def download_sentinel2_bands(self, products, bands_dict, tile):
69
+ """Orchestrates the download process for Sentinel-2 bands."""
70
+ utils = Utils()
71
+ if products is None:
72
+ logger.warning("Stopping further execution.")
73
+ else:
74
+ products_info = self.get_products_info(products)
75
+ self.output_folder(products_info, bands_dict)
76
+ links = self.get_bands_links(products_info, bands_dict)
77
+ products=self.download_band(products_info, links, self.output_base_path, tile)
78
+
79
+ downloaded_dict = utils.generate_sentinel2_band_paths_nested_by_tile(self.output_base_path, products_info)
80
+ return downloaded_dict
81
+
82
+ def get_composite_data(self, gdf, downloaded_dict, output_rgb=None, output_ndvi=None, output_evi=None):
83
+ raster_processing = RasterProcessing()
84
+ first_key = next(iter(downloaded_dict))
85
+ first_entry = downloaded_dict[first_key]
86
+ if len(first_entry) == 1:
87
+ band_paths = first_entry[0]
88
+ b02_array, b03_array, b04_array, b08_array, profile = raster_processing.get_cropped_bands_no_merge(gdf, band_paths)
89
+ elif len(first_entry) > 1:
90
+ b02_array, b03_array, b04_array, b08_array, profile = raster_processing.get_cropped_bands_with_merge(gdf, first_entry)
91
+
92
+ if output_rgb is not None:
93
+ raster_processing.create_rgb(b02_array, b03_array, b04_array, profile, output_rgb)
94
+
95
+ if output_ndvi is not None:
96
+ raster_processing.create_ndvi(b04_array, b08_array, profile, output_ndvi)
97
+
98
+ if output_evi is not None:
99
+ raster_processing.create_evi(b04_array, b04_array, b08_array, profile, output_evi)
100
+
101
+ def get_satellite_image(self, geodataframe, end_date, cloud_cover_percentage, type='L2A', platform_name='SENTINEL-2', bands_dict={"L2A":{"10m": ["B02", "B03","B04", "B08"]}}, output_rgb=None, output_ndvi=None, output_evi=None):
102
+ query = self.construct_query(geodataframe, end_date, cloud_cover_percentage, type, platform_name)
103
+ products = self.products_from_sentinel_2(query)
104
+ if products == None:
105
+ return
106
+ downloaded_dict = self.download_sentinel2_bands(products, bands_dict, None)
107
+ self.get_composite_data(geodataframe, downloaded_dict, output_rgb, output_ndvi, output_evi)
108
+
109
+
110
+ if __name__ == '__main__':
111
+ DOWNLOAD_FOLDER = '/home/ppz/Documentos/coding/izisat/auxiliary'
112
+ MAX_CLOUD_COVER = 99
113
+ BANDS_DICT = {"L2A":{"10m": ["B02", "B03","B04", "B08"]}}
114
+ PLATFORM_NAME = "SENTINEL-2"
115
+ SATELLITE_TYPE = 'L2A'
116
+ COPERNICUS_USER=''
117
+ COPERNICUS_PASSWD='!'
118
+
119
+ zona_unica=False
120
+
121
+ ###TESTE ZONA UNICA###
122
+ downloader = IZISentinel(output_base_path=DOWNLOAD_FOLDER, username=COPERNICUS_USER, password=COPERNICUS_PASSWD)
123
+ if zona_unica is True:
124
+ today = datetime(2025, 4, 14, 1, 0, 0)
125
+ gdf = gpd.read_file('/home/ppz/Documentos/coding/izisat/auxiliary/base_ldcelulose/base_cadastral/FULL_20250709_VIEW.shp')
126
+ gdf = gdf.loc[(gdf['TALHAO'] == '0112') & (gdf['ID_PROJETO'] == '5822')]
127
+
128
+ output_rgb = '/home/ppz/Documentos/coding/izisat/auxiliary/raster_lake/rgb/teste_rgb.tif'
129
+ output_ndvi = '/home/ppz/Documentos/coding/izisat/auxiliary/raster_lake/ndvi/teste_ndvi.tif'
130
+ downloader.get_satellite_image(
131
+ geodataframe=gdf,
132
+ end_date=today,
133
+ cloud_cover_percentage=MAX_CLOUD_COVER,
134
+ type=SATELLITE_TYPE,
135
+ platform_name=PLATFORM_NAME,
136
+ bands_dict=BANDS_DICT,
137
+ output_rgb=output_rgb,
138
+ output_ndvi=output_ndvi,
139
+ )
140
+
141
+ elif zona_unica is not True:
142
+ today = datetime(2025, 4, 9, 1, 0, 0)
143
+ gdf = gpd.read_file('/home/ppz/Documentos/coding/izisat/auxiliary/base_ldcelulose/base_filtrada.geojson')
144
+ gdf = gdf.dissolve(by='PROJETO').reset_index()
145
+ #gdf = gpd.read_file('/home/ppz/Documentos/coding/izisat/testing.geojson')
146
+ for index, row in gdf.iterrows():
147
+ print(f'linha - {index} de {len(gdf)} -- TALHAO: {row.TALHAO}')
148
+ row_gdf = gdf.iloc[[index]]
149
+ output_rgb = f'/home/ppz/Documentos/coding/izisat/auxiliary/raster_lake/rgb/teste_rgb_{row.TALHAO}.tif'
150
+ output_ndvi = f'/home/ppz/Documentos/coding/izisat/auxiliary/raster_lake/ndvi/teste_ndvi_{row.TALHAO}.tif'
151
+ output_evi = f'/home/ppz/Documentos/coding/izisat/auxiliary/raster_lake/evi/teste_evi_{row.TALHAO}.tif'
152
+ downloader.get_satellite_image(
153
+ geodataframe=row_gdf,
154
+ end_date=today,
155
+ cloud_cover_percentage=MAX_CLOUD_COVER,
156
+ type=SATELLITE_TYPE,
157
+ platform_name=PLATFORM_NAME,
158
+ bands_dict=BANDS_DICT,
159
+ output_rgb=output_rgb,
160
+ output_ndvi=output_ndvi,
161
+ output_evi=output_evi)
File without changes