-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
252 lines (198 loc) · 8.64 KB
/
helper.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import sys
from datetime import datetime
from typing import Union
import time
from shapely import geometry
import os
def datetimef( date_time: str) -> list:
"""
convert date-time to the ISO format.
Args:
date_time(str) : date-time of entity to convert to ISO format
"""
try:
datetime_begin = date_time.split("/")[0]
datetime_end = date_time.split("/")[1]
datetime_begin_formatted = datetime.strptime(
datetime_begin, "%Y-%m-%dT%H:%M:%SZ"
)
datetime_end_formatted = datetime.strptime(
datetime_end, "%Y-%m-%dT%H:%M:%SZ"
)
return [datetime_begin_formatted, datetime_end_formatted]
except Exception as e:
print(f"Could not convert the date-time to the ISO format due to exception {e}")
return None
def geometryf( bbox: list, geometry_type: str) -> str:
"""
Convert the bbox to the GeoJSON format.
Args:
bbox (str) : bbox of item
geometry_type (str) : geometry of item
"""
if geometry_type is not None and bbox is not None:
if isinstance(geometry_type, str):
if geometry_type == "Point":
try:
return geometry.Point(bbox[0][0], bbox[0][1])
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
elif geometry_type == "Polygon":
try:
return geometry.Polygon(bbox)
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
elif geometry_type == "LineString":
try:
return geometry.LineString(bbox)
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
elif geometry_type == "MultiPoint":
bbox = [[point[0], point[1]] for point in bbox]
try:
return geometry.MultiPoint(bbox)
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
elif geometry_type == "MultiPolygon":
try:
return geometry.MultiPolygon(bbox)
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
elif geometry_type == "MultiLineString":
try:
return geometry.MultiLineString(bbox)
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
else:
print("Could not convert the bbox to the GeoJSON format. The geometry type is not valid.")
return None
elif isinstance(geometry_type, list):
if all(geometry == "Point" for geometry in geometry_type):
bbox = [[point[0], point[1]] for point in bbox]
try:
return geometry.MultiPoint(bbox)
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
elif all(geometry == "Polygon" for geometry in geometry_type):
try:
return geometry.MultiPolygon(bbox)
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
if all(geometry == "LineString" for geometry in geometry_type):
try:
return geometry.MultiLineString(bbox)
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
else:
print("Could not convert the bbox to the GeoJSON format. The geometry type is not valid.")
return None
try:
return geometry.MultiPolygon(bbox(bbox))
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
else:
print("This is not a format to convert the bbox to the GeoJSON format.")
return None
elif geometry_type is None and bbox is not None:
try:
point = geometry.Point(bbox)
return point
except Exception:
try:
polygon = geometry.Polygon(bbox)
return polygon
except Exception:
try:
linestring = geometry.LineString(bbox)
return linestring
except Exception as e:
print(f"Could not convert the bbox to the GeoJSON format. {e}")
return None
def bbox( bbox: list, geometry: Union[str, list]) -> list:
"""
Convert the bbox to the GeoJSON format.
Args:
bbox (list): A list representing the bounding box.
geometry (Union[str, list]): The geometry type(s) associated with the bbox.
Returns:
list: A modified bounding box in GeoJSON format.
"""
if geometry is not None and bbox is not None:
if isinstance(geometry, str):
if geometry == "Point":
bbox = [[bbox[0][0], bbox[0][1]]]
return bbox
else:
return bbox
elif isinstance(geometry, list):
if all(geometry == "Point" for geometry in geometry):
bbox = [[point[0], point[1]] for point in bbox]
else:
return bbox
elif geometry is None and bbox is not None:
return bbox
else:
print("Could not convert the bbox to the GeoJSON format. The geometry type is not valid.")
return None
def collectiondt( date_times_list: list) -> list:
"""
Process the items attributes to create the collection temporal extent.
Args:
date_times_list (list): A list of `datetime` objects representing the time range of fetched items.
Returns:
list: A datetime interval in list form containing two `datetime` objects representing the temporal extent of the collection
"""
date_times_list = sorted(date_times_list)
return [date_times_list[0], date_times_list[-1]]
def item( fetched_vars: dict):
"""
Process the items attributes to create the item's temporal extent.
Args:
fetched_vars (dict): A dictionary containing various attributes of the items
Returns:
dict: A dictionary with the updated `item_datetime` and `item_bbox` reflecting the processed temporal and spatial extents.
"""
all_item_datetime = []
if fetched_vars.get("item_datetime") is not None:
for item_datetime in fetched_vars["item_datetime"]:
print(item_datetime)
if item_datetime is not None:
list_start_end_datetime = datetimef(item_datetime)
if list_start_end_datetime is not None:
all_item_datetime.extend(list_start_end_datetime)
all_item_datetime = sorted(all_item_datetime)
fetched_vars["item_datetime"] = [
all_item_datetime[0],
all_item_datetime[-1],
]
if (
fetched_vars.get("item_bbox") is not None
and fetched_vars.get("item_geometry") is not None
):
fetched_vars["item_bbox"] = bbox(
fetched_vars["item_bbox"], fetched_vars["item_geometry"]
)
return fetched_vars
def check_existance(path_to_check: str) -> bool:
"""
Check existance of catalog or collection.
Args:
path_to_check (str) : path of catalog or collection to check existance
Returns:
bool : True or False depending on existance
"""
print(f"checking existance on path {path_to_check}")
if os.path.exists(path_to_check):
return True
else:
return False