Skip to content

Commit 34304b0

Browse files
committed
Fixed a bug in the mosaic function, now it generates both single-date mosaics and data cubes correctly.
1 parent 84c7fd3 commit 34304b0

File tree

5 files changed

+60
-53
lines changed

5 files changed

+60
-53
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
Changes
2020
=======
2121

22+
0.2.2 (2025-10-15)
23+
------------------
24+
25+
* **Fix**: Fixed a bug in the ``mosaic`` function, now it generates both single-date mosaics and data cubes correctly.
26+
27+
2228
0.2.0 (2025-10-10)
2329
------------------
2430

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2
1+
0.2.2

notebooks/smosaic-data-cube.ipynb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@
4141
"</div>"
4242
]
4343
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": null,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"!pip3 install smosaic==0.2.2"
51+
]
52+
},
4453
{
4554
"cell_type": "code",
4655
"execution_count": null,

notebooks/smosaic-introduction.ipynb

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@
4343
},
4444
{
4545
"cell_type": "code",
46-
"execution_count": 1,
46+
"execution_count": null,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"!pip3 install smosaic==0.2.2"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
4756
"metadata": {},
4857
"outputs": [],
4958
"source": [
@@ -53,7 +62,7 @@
5362
},
5463
{
5564
"cell_type": "code",
56-
"execution_count": 2,
65+
"execution_count": null,
5766
"metadata": {},
5867
"outputs": [],
5968
"source": [
@@ -62,7 +71,7 @@
6271
},
6372
{
6473
"cell_type": "code",
65-
"execution_count": 3,
74+
"execution_count": null,
6675
"metadata": {},
6776
"outputs": [],
6877
"source": [
@@ -71,39 +80,9 @@
7180
},
7281
{
7382
"cell_type": "code",
74-
"execution_count": 4,
83+
"execution_count": null,
7584
"metadata": {},
76-
"outputs": [
77-
{
78-
"name": "stderr",
79-
"output_type": "stream",
80-
"text": [
81-
"Downloading... : 100%|██████████| 27/27 [23:43<00:00, 52.74s/ itens] \n"
82-
]
83-
},
84-
{
85-
"name": "stdout",
86-
"output_type": "stream",
87-
"text": [
88-
"Successfully download 27 files to S2_L2A-1\n",
89-
"Building B02 mosaic using 2 scenes from the S2_L2A-1.\n",
90-
"Successfully merged 22 files for 23LMG scene.\n",
91-
"Successfully merged 37 files for 23LLG scene.\n",
92-
"Successfully merged 2 files for luis-eduardo-magalhaes scene.\n",
93-
"Raster saved to: output/mosaic-s2_l2a-luis-eduardo-magalhaes-1m-b02-20250901_20250930_COG.tif\n",
94-
"Building B03 mosaic using 2 scenes from the S2_L2A-1.\n",
95-
"Successfully merged 22 files for 23LMG scene.\n",
96-
"Successfully merged 37 files for 23LLG scene.\n",
97-
"Successfully merged 2 files for luis-eduardo-magalhaes scene.\n",
98-
"Raster saved to: output/mosaic-s2_l2a-luis-eduardo-magalhaes-1m-b03-20250901_20250930_COG.tif\n",
99-
"Building B04 mosaic using 2 scenes from the S2_L2A-1.\n",
100-
"Successfully merged 22 files for 23LMG scene.\n",
101-
"Successfully merged 37 files for 23LLG scene.\n",
102-
"Successfully merged 2 files for luis-eduardo-magalhaes scene.\n",
103-
"Raster saved to: output/mosaic-s2_l2a-luis-eduardo-magalhaes-1m-b04-20250901_20250930_COG.tif\n"
104-
]
105-
}
106-
],
85+
"outputs": [],
10786
"source": [
10887
"result = mosaic(\n",
10988
" name=\"luis-eduardo-magalhaes\",\n",
@@ -138,7 +117,7 @@
138117
"name": "python",
139118
"nbconvert_exporter": "python",
140119
"pygments_lexer": "ipython3",
141-
"version": "3.10.12"
120+
"version": "3.12.3"
142121
}
143122
},
144123
"nbformat": 4,

smosaic/smosaic_core.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -607,35 +607,48 @@ def mosaic(name, data_dir, stac_url, collection, output_dir, start_year, start_m
607607
if collection not in ['S2_L2A-1']: #'S2-16D-2'
608608
return print(f"{collection['collection']} collection not yet supported.")
609609

610+
#geometry
611+
if (geom):
612+
bbox = geom.bounds
613+
610614
#bbox
611-
if (bbox != None):
615+
else:
612616
tuple_bbox = tuple(map(float, bbox.split(',')))
613617
geom = shapely.geometry.box(*tuple_bbox)
614618
bbox = geom.bounds
615619

616620
start_date = datetime.datetime.strptime(str(start_year)+'-'+str(start_month)+'-'+str(start_day), "%Y-%m-%d")
617621

618-
if (end_year != None and end_month != None and end_day != None ):
622+
end_date = None
623+
if all(v is not None for v in [end_year, end_month, end_day]):
619624
end_date = datetime.datetime.strptime(str(end_year)+'-'+str(end_month)+'-'+str(end_day), "%Y-%m-%d")
620-
elif (duration_months):
621-
end_date = str(add_months_to_date(start_date, duration_months-1))
622-
else:
623-
return print(f"Not provided with a valid time interval.")
625+
elif duration_months is not None:
626+
end_date = add_months_to_date(start_date,duration_months-1)
627+
elif duration_days is not None and not any([end_year, end_month, end_day, duration_months]):
628+
end_date = add_days_to_date(start_date,duration_days)
629+
630+
if end_date is None:
631+
return print("Not provided with a valid time interval.")
624632

625633
periods = []
626-
current_period_start = start_date
634+
current_start_date = start_date
627635

628-
while current_period_start <= end_date:
629-
current_period_end = add_days_to_date(current_period_start, duration_days - 1)
630-
if current_period_end > end_date:
631-
current_period_end = end_date
632-
if current_period_start.strftime("%Y-%m-%d") != current_period_end.strftime("%Y-%m-%d"):
636+
if duration_days:
637+
while current_start_date <= end_date:
638+
current_end_date = add_days_to_date(current_start_date,duration_days-1)
639+
if current_end_date > end_date:
640+
current_end_date = end_date
633641
periods.append({
634-
'start': current_period_start.strftime("%Y-%m-%d"),
635-
'end': current_period_end.strftime("%Y-%m-%d")
642+
'start': current_start_date.strftime("%Y-%m-%d"),
643+
'end': current_end_date.strftime("%Y-%m-%d")
636644
})
637-
current_period_start = add_days_to_date(current_period_start, duration_days)
638-
645+
current_start_date += datetime.timedelta(days=duration_days)
646+
else:
647+
periods.append({
648+
'start': start_date.strftime("%Y-%m-%d"),
649+
'end': end_date.strftime("%Y-%m-%d")
650+
})
651+
639652
dict_collection=collection_query(
640653
collection=collection,
641654
start_date=start_date.strftime("%Y-%m-%d"),

0 commit comments

Comments
 (0)