stac-fastapi.core 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.
@@ -0,0 +1,983 @@
1
+ """Item crud client."""
2
+ import logging
3
+ import re
4
+ from base64 import urlsafe_b64encode
5
+ from datetime import datetime as datetime_type
6
+ from datetime import timezone
7
+ from typing import Any, Dict, List, Optional, Set, Type, Union
8
+ from urllib.parse import unquote_plus, urljoin
9
+
10
+ import attr
11
+ import orjson
12
+ import stac_pydantic
13
+ from fastapi import HTTPException, Request
14
+ from overrides import overrides
15
+ from pydantic import ValidationError
16
+ from pygeofilter.backends.cql2_json import to_cql2
17
+ from pygeofilter.parsers.cql2_text import parse as parse_cql2_text
18
+ from stac_pydantic.links import Relations
19
+ from stac_pydantic.shared import MimeTypes
20
+ from stac_pydantic.version import STAC_VERSION
21
+
22
+ from stac_fastapi.core.base_database_logic import BaseDatabaseLogic
23
+ from stac_fastapi.core.base_settings import ApiBaseSettings
24
+ from stac_fastapi.core.models.links import PagingLinks
25
+ from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
26
+ from stac_fastapi.core.session import Session
27
+ from stac_fastapi.core.types.core import (
28
+ AsyncBaseCoreClient,
29
+ AsyncBaseFiltersClient,
30
+ AsyncBaseTransactionsClient,
31
+ )
32
+ from stac_fastapi.extensions.third_party.bulk_transactions import (
33
+ BaseBulkTransactionsClient,
34
+ BulkTransactionMethod,
35
+ Items,
36
+ )
37
+ from stac_fastapi.types import stac as stac_types
38
+ from stac_fastapi.types.config import Settings
39
+ from stac_fastapi.types.conformance import BASE_CONFORMANCE_CLASSES
40
+ from stac_fastapi.types.extension import ApiExtension
41
+ from stac_fastapi.types.links import CollectionLinks
42
+ from stac_fastapi.types.requests import get_base_url
43
+ from stac_fastapi.types.search import BaseSearchPostRequest
44
+ from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection
45
+
46
+ logger = logging.getLogger(__name__)
47
+
48
+ NumType = Union[float, int]
49
+
50
+
51
+ @attr.s
52
+ class CoreClient(AsyncBaseCoreClient):
53
+ """Client for core endpoints defined by the STAC specification.
54
+
55
+ This class is a implementation of `AsyncBaseCoreClient` that implements the core endpoints
56
+ defined by the STAC specification. It uses the `DatabaseLogic` class to interact with the
57
+ database, and `ItemSerializer` and `CollectionSerializer` to convert between STAC objects and
58
+ database records.
59
+
60
+ Attributes:
61
+ session (Session): A requests session instance to be used for all HTTP requests.
62
+ item_serializer (Type[serializers.ItemSerializer]): A serializer class to be used to convert
63
+ between STAC items and database records.
64
+ collection_serializer (Type[serializers.CollectionSerializer]): A serializer class to be
65
+ used to convert between STAC collections and database records.
66
+ database (DatabaseLogic): An instance of the `DatabaseLogic` class that is used to interact
67
+ with the database.
68
+ """
69
+
70
+ database: BaseDatabaseLogic = attr.ib()
71
+ base_conformance_classes: List[str] = attr.ib(
72
+ factory=lambda: BASE_CONFORMANCE_CLASSES
73
+ )
74
+ extensions: List[ApiExtension] = attr.ib(default=attr.Factory(list))
75
+
76
+ session: Session = attr.ib(default=attr.Factory(Session.create_from_env))
77
+ item_serializer: Type[ItemSerializer] = attr.ib(default=ItemSerializer)
78
+ collection_serializer: Type[CollectionSerializer] = attr.ib(
79
+ default=CollectionSerializer
80
+ )
81
+ post_request_model = attr.ib(default=BaseSearchPostRequest)
82
+ stac_version: str = attr.ib(default=STAC_VERSION)
83
+ landing_page_id: str = attr.ib(default="stac-fastapi")
84
+ title: str = attr.ib(default="stac-fastapi")
85
+ description: str = attr.ib(default="stac-fastapi")
86
+
87
+ def _landing_page(
88
+ self,
89
+ base_url: str,
90
+ conformance_classes: List[str],
91
+ extension_schemas: List[str],
92
+ ) -> stac_types.LandingPage:
93
+ landing_page = stac_types.LandingPage(
94
+ type="Catalog",
95
+ id=self.landing_page_id,
96
+ title=self.title,
97
+ description=self.description,
98
+ stac_version=self.stac_version,
99
+ conformsTo=conformance_classes,
100
+ links=[
101
+ {
102
+ "rel": Relations.self.value,
103
+ "type": MimeTypes.json,
104
+ "href": base_url,
105
+ },
106
+ {
107
+ "rel": Relations.root.value,
108
+ "type": MimeTypes.json,
109
+ "href": base_url,
110
+ },
111
+ {
112
+ "rel": "data",
113
+ "type": MimeTypes.json,
114
+ "href": urljoin(base_url, "collections"),
115
+ },
116
+ {
117
+ "rel": Relations.conformance.value,
118
+ "type": MimeTypes.json,
119
+ "title": "STAC/WFS3 conformance classes implemented by this server",
120
+ "href": urljoin(base_url, "conformance"),
121
+ },
122
+ {
123
+ "rel": Relations.search.value,
124
+ "type": MimeTypes.geojson,
125
+ "title": "STAC search",
126
+ "href": urljoin(base_url, "search"),
127
+ "method": "GET",
128
+ },
129
+ {
130
+ "rel": Relations.search.value,
131
+ "type": MimeTypes.geojson,
132
+ "title": "STAC search",
133
+ "href": urljoin(base_url, "search"),
134
+ "method": "POST",
135
+ },
136
+ ],
137
+ stac_extensions=extension_schemas,
138
+ )
139
+ return landing_page
140
+
141
+ async def landing_page(self, **kwargs) -> stac_types.LandingPage:
142
+ """Landing page.
143
+
144
+ Called with `GET /`.
145
+
146
+ Returns:
147
+ API landing page, serving as an entry point to the API.
148
+ """
149
+ request: Request = kwargs["request"]
150
+ base_url = get_base_url(request)
151
+ landing_page = self._landing_page(
152
+ base_url=base_url,
153
+ conformance_classes=self.conformance_classes(),
154
+ extension_schemas=[],
155
+ )
156
+ collections = await self.all_collections(request=kwargs["request"])
157
+ for collection in collections["collections"]:
158
+ landing_page["links"].append(
159
+ {
160
+ "rel": Relations.child.value,
161
+ "type": MimeTypes.json.value,
162
+ "title": collection.get("title") or collection.get("id"),
163
+ "href": urljoin(base_url, f"collections/{collection['id']}"),
164
+ }
165
+ )
166
+
167
+ # Add OpenAPI URL
168
+ landing_page["links"].append(
169
+ {
170
+ "rel": "service-desc",
171
+ "type": "application/vnd.oai.openapi+json;version=3.0",
172
+ "title": "OpenAPI service description",
173
+ "href": urljoin(
174
+ str(request.base_url), request.app.openapi_url.lstrip("/")
175
+ ),
176
+ }
177
+ )
178
+
179
+ # Add human readable service-doc
180
+ landing_page["links"].append(
181
+ {
182
+ "rel": "service-doc",
183
+ "type": "text/html",
184
+ "title": "OpenAPI service documentation",
185
+ "href": urljoin(
186
+ str(request.base_url), request.app.docs_url.lstrip("/")
187
+ ),
188
+ }
189
+ )
190
+
191
+ return landing_page
192
+
193
+ async def all_collections(self, **kwargs) -> Collections:
194
+ """Read all collections from the database.
195
+
196
+ Returns:
197
+ Collections: A `Collections` object containing all the collections in the database and
198
+ links to various resources.
199
+
200
+ Raises:
201
+ Exception: If any error occurs while reading the collections from the database.
202
+ """
203
+ request: Request = kwargs["request"]
204
+ base_url = str(kwargs["request"].base_url)
205
+
206
+ limit = (
207
+ int(request.query_params["limit"])
208
+ if "limit" in request.query_params
209
+ else 10
210
+ )
211
+ token = (
212
+ request.query_params["token"] if "token" in request.query_params else None
213
+ )
214
+
215
+ hits = await self.database.get_all_collections(limit=limit, token=token)
216
+
217
+ next_search_after = None
218
+ next_link = None
219
+ if len(hits) == limit:
220
+ last_hit = hits[-1]
221
+ next_search_after = last_hit["sort"]
222
+ next_token = urlsafe_b64encode(
223
+ ",".join(map(str, next_search_after)).encode()
224
+ ).decode()
225
+ paging_links = PagingLinks(next=next_token, request=request)
226
+ next_link = paging_links.link_next()
227
+
228
+ links = [
229
+ {
230
+ "rel": Relations.root.value,
231
+ "type": MimeTypes.json,
232
+ "href": base_url,
233
+ },
234
+ {
235
+ "rel": Relations.parent.value,
236
+ "type": MimeTypes.json,
237
+ "href": base_url,
238
+ },
239
+ {
240
+ "rel": Relations.self.value,
241
+ "type": MimeTypes.json,
242
+ "href": urljoin(base_url, "collections"),
243
+ },
244
+ ]
245
+
246
+ if next_link:
247
+ links.append(next_link)
248
+
249
+ return Collections(
250
+ collections=[
251
+ self.collection_serializer.db_to_stac(c["_source"], base_url=base_url)
252
+ for c in hits
253
+ ],
254
+ links=links,
255
+ )
256
+
257
+ async def get_collection(self, collection_id: str, **kwargs) -> Collection:
258
+ """Get a collection from the database by its id.
259
+
260
+ Args:
261
+ collection_id (str): The id of the collection to retrieve.
262
+ kwargs: Additional keyword arguments passed to the API call.
263
+
264
+ Returns:
265
+ Collection: A `Collection` object representing the requested collection.
266
+
267
+ Raises:
268
+ NotFoundError: If the collection with the given id cannot be found in the database.
269
+ """
270
+ base_url = str(kwargs["request"].base_url)
271
+ collection = await self.database.find_collection(collection_id=collection_id)
272
+ return self.collection_serializer.db_to_stac(collection, base_url)
273
+
274
+ async def item_collection(
275
+ self,
276
+ collection_id: str,
277
+ bbox: Optional[List[NumType]] = None,
278
+ datetime: Union[str, datetime_type, None] = None,
279
+ limit: int = 10,
280
+ token: str = None,
281
+ **kwargs,
282
+ ) -> ItemCollection:
283
+ """Read items from a specific collection in the database.
284
+
285
+ Args:
286
+ collection_id (str): The identifier of the collection to read items from.
287
+ bbox (Optional[List[NumType]]): The bounding box to filter items by.
288
+ datetime (Union[str, datetime_type, None]): The datetime range to filter items by.
289
+ limit (int): The maximum number of items to return. The default value is 10.
290
+ token (str): A token used for pagination.
291
+ request (Request): The incoming request.
292
+
293
+ Returns:
294
+ ItemCollection: An `ItemCollection` object containing the items from the specified collection that meet
295
+ the filter criteria and links to various resources.
296
+
297
+ Raises:
298
+ HTTPException: If the specified collection is not found.
299
+ Exception: If any error occurs while reading the items from the database.
300
+ """
301
+ request: Request = kwargs["request"]
302
+ base_url = str(request.base_url)
303
+
304
+ collection = await self.get_collection(
305
+ collection_id=collection_id, request=request
306
+ )
307
+ collection_id = collection.get("id")
308
+ if collection_id is None:
309
+ raise HTTPException(status_code=404, detail="Collection not found")
310
+
311
+ search = self.database.make_search()
312
+ search = self.database.apply_collections_filter(
313
+ search=search, collection_ids=[collection_id]
314
+ )
315
+
316
+ if datetime:
317
+ datetime_search = self._return_date(datetime)
318
+ search = self.database.apply_datetime_filter(
319
+ search=search, datetime_search=datetime_search
320
+ )
321
+
322
+ if bbox:
323
+ bbox = [float(x) for x in bbox]
324
+ if len(bbox) == 6:
325
+ bbox = [bbox[0], bbox[1], bbox[3], bbox[4]]
326
+
327
+ search = self.database.apply_bbox_filter(search=search, bbox=bbox)
328
+
329
+ items, maybe_count, next_token = await self.database.execute_search(
330
+ search=search,
331
+ limit=limit,
332
+ sort=None,
333
+ token=token, # type: ignore
334
+ collection_ids=[collection_id],
335
+ )
336
+
337
+ items = [
338
+ self.item_serializer.db_to_stac(item, base_url=base_url) for item in items
339
+ ]
340
+
341
+ context_obj = None
342
+ if self.extension_is_enabled("ContextExtension"):
343
+ context_obj = {
344
+ "returned": len(items),
345
+ "limit": limit,
346
+ }
347
+ if maybe_count is not None:
348
+ context_obj["matched"] = maybe_count
349
+
350
+ links = []
351
+ if next_token:
352
+ links = await PagingLinks(request=request, next=next_token).get_links()
353
+
354
+ return ItemCollection(
355
+ type="FeatureCollection",
356
+ features=items,
357
+ links=links,
358
+ context=context_obj,
359
+ )
360
+
361
+ async def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
362
+ """Get an item from the database based on its id and collection id.
363
+
364
+ Args:
365
+ collection_id (str): The ID of the collection the item belongs to.
366
+ item_id (str): The ID of the item to be retrieved.
367
+
368
+ Returns:
369
+ Item: An `Item` object representing the requested item.
370
+
371
+ Raises:
372
+ Exception: If any error occurs while getting the item from the database.
373
+ NotFoundError: If the item does not exist in the specified collection.
374
+ """
375
+ base_url = str(kwargs["request"].base_url)
376
+ item = await self.database.get_one_item(
377
+ item_id=item_id, collection_id=collection_id
378
+ )
379
+ return self.item_serializer.db_to_stac(item, base_url)
380
+
381
+ @staticmethod
382
+ def _return_date(interval_str):
383
+ """
384
+ Convert a date interval string into a dictionary for filtering search results.
385
+
386
+ The date interval string should be formatted as either a single date or a range of dates separated
387
+ by "/". The date format should be ISO-8601 (YYYY-MM-DDTHH:MM:SSZ). If the interval string is a
388
+ single date, it will be converted to a dictionary with a single "eq" key whose value is the date in
389
+ the ISO-8601 format. If the interval string is a range of dates, it will be converted to a
390
+ dictionary with "gte" (greater than or equal to) and "lte" (less than or equal to) keys. If the
391
+ interval string is a range of dates with ".." instead of "/", the start and end dates will be
392
+ assigned default values to encompass the entire possible date range.
393
+
394
+ Args:
395
+ interval_str (str): The date interval string to be converted.
396
+
397
+ Returns:
398
+ dict: A dictionary representing the date interval for use in filtering search results.
399
+ """
400
+ intervals = interval_str.split("/")
401
+ if len(intervals) == 1:
402
+ datetime = f"{intervals[0][0:19]}Z"
403
+ return {"eq": datetime}
404
+ else:
405
+ start_date = intervals[0]
406
+ end_date = intervals[1]
407
+ if ".." not in intervals:
408
+ start_date = f"{start_date[0:19]}Z"
409
+ end_date = f"{end_date[0:19]}Z"
410
+ elif start_date != "..":
411
+ start_date = f"{start_date[0:19]}Z"
412
+ end_date = "2200-12-01T12:31:12Z"
413
+ elif end_date != "..":
414
+ start_date = "1900-10-01T00:00:00Z"
415
+ end_date = f"{end_date[0:19]}Z"
416
+ else:
417
+ start_date = "1900-10-01T00:00:00Z"
418
+ end_date = "2200-12-01T12:31:12Z"
419
+
420
+ return {"lte": end_date, "gte": start_date}
421
+
422
+ async def get_search(
423
+ self,
424
+ request: Request,
425
+ collections: Optional[List[str]] = None,
426
+ ids: Optional[List[str]] = None,
427
+ bbox: Optional[List[NumType]] = None,
428
+ datetime: Optional[Union[str, datetime_type]] = None,
429
+ limit: Optional[int] = 10,
430
+ query: Optional[str] = None,
431
+ token: Optional[str] = None,
432
+ fields: Optional[List[str]] = None,
433
+ sortby: Optional[str] = None,
434
+ intersects: Optional[str] = None,
435
+ filter: Optional[str] = None,
436
+ filter_lang: Optional[str] = None,
437
+ **kwargs,
438
+ ) -> ItemCollection:
439
+ """Get search results from the database.
440
+
441
+ Args:
442
+ collections (Optional[List[str]]): List of collection IDs to search in.
443
+ ids (Optional[List[str]]): List of item IDs to search for.
444
+ bbox (Optional[List[NumType]]): Bounding box to search in.
445
+ datetime (Optional[Union[str, datetime_type]]): Filter items based on the datetime field.
446
+ limit (Optional[int]): Maximum number of results to return.
447
+ query (Optional[str]): Query string to filter the results.
448
+ token (Optional[str]): Access token to use when searching the catalog.
449
+ fields (Optional[List[str]]): Fields to include or exclude from the results.
450
+ sortby (Optional[str]): Sorting options for the results.
451
+ intersects (Optional[str]): GeoJSON geometry to search in.
452
+ kwargs: Additional parameters to be passed to the API.
453
+
454
+ Returns:
455
+ ItemCollection: Collection of `Item` objects representing the search results.
456
+
457
+ Raises:
458
+ HTTPException: If any error occurs while searching the catalog.
459
+ """
460
+ base_args = {
461
+ "collections": collections,
462
+ "ids": ids,
463
+ "bbox": bbox,
464
+ "limit": limit,
465
+ "token": token,
466
+ "query": orjson.loads(query) if query else query,
467
+ }
468
+
469
+ # this is borrowed from stac-fastapi-pgstac
470
+ # Kludgy fix because using factory does not allow alias for filter-lan
471
+ query_params = str(request.query_params)
472
+ if filter_lang is None:
473
+ match = re.search(r"filter-lang=([a-z0-9-]+)", query_params, re.IGNORECASE)
474
+ if match:
475
+ filter_lang = match.group(1)
476
+
477
+ if datetime:
478
+ base_args["datetime"] = datetime
479
+
480
+ if intersects:
481
+ base_args["intersects"] = orjson.loads(unquote_plus(intersects))
482
+
483
+ if sortby:
484
+ sort_param = []
485
+ for sort in sortby:
486
+ sort_param.append(
487
+ {
488
+ "field": sort[1:],
489
+ "direction": "desc" if sort[0] == "-" else "asc",
490
+ }
491
+ )
492
+ print(sort_param)
493
+ base_args["sortby"] = sort_param
494
+
495
+ if filter:
496
+ if filter_lang == "cql2-json":
497
+ base_args["filter-lang"] = "cql2-json"
498
+ base_args["filter"] = orjson.loads(unquote_plus(filter))
499
+ else:
500
+ base_args["filter-lang"] = "cql2-json"
501
+ base_args["filter"] = orjson.loads(to_cql2(parse_cql2_text(filter)))
502
+
503
+ if fields:
504
+ includes = set()
505
+ excludes = set()
506
+ for field in fields:
507
+ if field[0] == "-":
508
+ excludes.add(field[1:])
509
+ elif field[0] == "+":
510
+ includes.add(field[1:])
511
+ else:
512
+ includes.add(field)
513
+ base_args["fields"] = {"include": includes, "exclude": excludes}
514
+
515
+ # Do the request
516
+ try:
517
+ search_request = self.post_request_model(**base_args)
518
+ except ValidationError:
519
+ raise HTTPException(status_code=400, detail="Invalid parameters provided")
520
+ resp = await self.post_search(search_request=search_request, request=request)
521
+
522
+ return resp
523
+
524
+ async def post_search(
525
+ self, search_request: BaseSearchPostRequest, request: Request
526
+ ) -> ItemCollection:
527
+ """
528
+ Perform a POST search on the catalog.
529
+
530
+ Args:
531
+ search_request (BaseSearchPostRequest): Request object that includes the parameters for the search.
532
+ kwargs: Keyword arguments passed to the function.
533
+
534
+ Returns:
535
+ ItemCollection: A collection of items matching the search criteria.
536
+
537
+ Raises:
538
+ HTTPException: If there is an error with the cql2_json filter.
539
+ """
540
+ base_url = str(request.base_url)
541
+
542
+ search = self.database.make_search()
543
+
544
+ if search_request.ids:
545
+ search = self.database.apply_ids_filter(
546
+ search=search, item_ids=search_request.ids
547
+ )
548
+
549
+ if search_request.collections:
550
+ search = self.database.apply_collections_filter(
551
+ search=search, collection_ids=search_request.collections
552
+ )
553
+
554
+ if search_request.datetime:
555
+ datetime_search = self._return_date(search_request.datetime)
556
+ search = self.database.apply_datetime_filter(
557
+ search=search, datetime_search=datetime_search
558
+ )
559
+
560
+ if search_request.bbox:
561
+ bbox = search_request.bbox
562
+ if len(bbox) == 6:
563
+ bbox = [bbox[0], bbox[1], bbox[3], bbox[4]]
564
+
565
+ search = self.database.apply_bbox_filter(search=search, bbox=bbox)
566
+
567
+ if search_request.intersects:
568
+ search = self.database.apply_intersects_filter(
569
+ search=search, intersects=search_request.intersects
570
+ )
571
+
572
+ if search_request.query:
573
+ for field_name, expr in search_request.query.items():
574
+ field = "properties__" + field_name
575
+ for op, value in expr.items():
576
+ search = self.database.apply_stacql_filter(
577
+ search=search, op=op, field=field, value=value
578
+ )
579
+
580
+ # only cql2_json is supported here
581
+ if hasattr(search_request, "filter"):
582
+ cql2_filter = getattr(search_request, "filter", None)
583
+ try:
584
+ search = self.database.apply_cql2_filter(search, cql2_filter)
585
+ except Exception as e:
586
+ raise HTTPException(
587
+ status_code=400, detail=f"Error with cql2_json filter: {e}"
588
+ )
589
+
590
+ sort = None
591
+ if search_request.sortby:
592
+ sort = self.database.populate_sort(search_request.sortby)
593
+
594
+ limit = 10
595
+ if search_request.limit:
596
+ limit = search_request.limit
597
+
598
+ items, maybe_count, next_token = await self.database.execute_search(
599
+ search=search,
600
+ limit=limit,
601
+ token=search_request.token, # type: ignore
602
+ sort=sort,
603
+ collection_ids=search_request.collections,
604
+ )
605
+
606
+ items = [
607
+ self.item_serializer.db_to_stac(item, base_url=base_url) for item in items
608
+ ]
609
+
610
+ if self.extension_is_enabled("FieldsExtension"):
611
+ if search_request.query is not None:
612
+ query_include: Set[str] = set(
613
+ [
614
+ k if k in Settings.get().indexed_fields else f"properties.{k}"
615
+ for k in search_request.query.keys()
616
+ ]
617
+ )
618
+ if not search_request.fields.include:
619
+ search_request.fields.include = query_include
620
+ else:
621
+ search_request.fields.include.union(query_include)
622
+
623
+ filter_kwargs = search_request.fields.filter_fields
624
+
625
+ items = [
626
+ orjson.loads(
627
+ stac_pydantic.Item(**feat).json(**filter_kwargs, exclude_unset=True)
628
+ )
629
+ for feat in items
630
+ ]
631
+
632
+ context_obj = None
633
+ if self.extension_is_enabled("ContextExtension"):
634
+ context_obj = {
635
+ "returned": len(items),
636
+ "limit": limit,
637
+ }
638
+ if maybe_count is not None:
639
+ context_obj["matched"] = maybe_count
640
+
641
+ links = []
642
+ if next_token:
643
+ links = await PagingLinks(request=request, next=next_token).get_links()
644
+
645
+ return ItemCollection(
646
+ type="FeatureCollection",
647
+ features=items,
648
+ links=links,
649
+ context=context_obj,
650
+ )
651
+
652
+
653
+ @attr.s
654
+ class TransactionsClient(AsyncBaseTransactionsClient):
655
+ """Transactions extension specific CRUD operations."""
656
+
657
+ database: BaseDatabaseLogic = attr.ib()
658
+ settings: ApiBaseSettings = attr.ib()
659
+ session: Session = attr.ib(default=attr.Factory(Session.create_from_env))
660
+
661
+ @overrides
662
+ async def create_item(
663
+ self, collection_id: str, item: stac_types.Item, **kwargs
664
+ ) -> stac_types.Item:
665
+ """Create an item in the collection.
666
+
667
+ Args:
668
+ collection_id (str): The id of the collection to add the item to.
669
+ item (stac_types.Item): The item to be added to the collection.
670
+ kwargs: Additional keyword arguments.
671
+
672
+ Returns:
673
+ stac_types.Item: The created item.
674
+
675
+ Raises:
676
+ NotFound: If the specified collection is not found in the database.
677
+ ConflictError: If the item in the specified collection already exists.
678
+
679
+ """
680
+ base_url = str(kwargs["request"].base_url)
681
+
682
+ # If a feature collection is posted
683
+ if item["type"] == "FeatureCollection":
684
+ bulk_client = BulkTransactionsClient(
685
+ database=self.database, settings=self.settings
686
+ )
687
+ processed_items = [
688
+ bulk_client.preprocess_item(item, base_url, BulkTransactionMethod.INSERT) for item in item["features"] # type: ignore
689
+ ]
690
+
691
+ await self.database.bulk_async(
692
+ collection_id, processed_items, refresh=kwargs.get("refresh", False)
693
+ )
694
+
695
+ return None # type: ignore
696
+ else:
697
+ item = await self.database.prep_create_item(item=item, base_url=base_url)
698
+ await self.database.create_item(item, refresh=kwargs.get("refresh", False))
699
+ return item
700
+
701
+ @overrides
702
+ async def update_item(
703
+ self, collection_id: str, item_id: str, item: stac_types.Item, **kwargs
704
+ ) -> stac_types.Item:
705
+ """Update an item in the collection.
706
+
707
+ Args:
708
+ collection_id (str): The ID of the collection the item belongs to.
709
+ item_id (str): The ID of the item to be updated.
710
+ item (stac_types.Item): The new item data.
711
+ kwargs: Other optional arguments, including the request object.
712
+
713
+ Returns:
714
+ stac_types.Item: The updated item object.
715
+
716
+ Raises:
717
+ NotFound: If the specified collection is not found in the database.
718
+
719
+ """
720
+ base_url = str(kwargs["request"].base_url)
721
+ now = datetime_type.now(timezone.utc).isoformat().replace("+00:00", "Z")
722
+ item["properties"]["updated"] = now
723
+
724
+ await self.database.check_collection_exists(collection_id)
725
+ await self.delete_item(item_id=item_id, collection_id=collection_id)
726
+ await self.create_item(collection_id=collection_id, item=item, **kwargs)
727
+
728
+ return ItemSerializer.db_to_stac(item, base_url)
729
+
730
+ @overrides
731
+ async def delete_item(
732
+ self, item_id: str, collection_id: str, **kwargs
733
+ ) -> stac_types.Item:
734
+ """Delete an item from a collection.
735
+
736
+ Args:
737
+ item_id (str): The identifier of the item to delete.
738
+ collection_id (str): The identifier of the collection that contains the item.
739
+
740
+ Returns:
741
+ Optional[stac_types.Item]: The deleted item, or `None` if the item was successfully deleted.
742
+ """
743
+ await self.database.delete_item(item_id=item_id, collection_id=collection_id)
744
+ return None # type: ignore
745
+
746
+ @overrides
747
+ async def create_collection(
748
+ self, collection: stac_types.Collection, **kwargs
749
+ ) -> stac_types.Collection:
750
+ """Create a new collection in the database.
751
+
752
+ Args:
753
+ collection (stac_types.Collection): The collection to be created.
754
+ kwargs: Additional keyword arguments.
755
+
756
+ Returns:
757
+ stac_types.Collection: The created collection object.
758
+
759
+ Raises:
760
+ ConflictError: If the collection already exists.
761
+ """
762
+ base_url = str(kwargs["request"].base_url)
763
+ collection_links = CollectionLinks(
764
+ collection_id=collection["id"], base_url=base_url
765
+ ).create_links()
766
+ collection["links"] = collection_links
767
+ await self.database.create_collection(collection=collection)
768
+
769
+ return CollectionSerializer.db_to_stac(collection, base_url)
770
+
771
+ @overrides
772
+ async def update_collection(
773
+ self, collection: stac_types.Collection, **kwargs
774
+ ) -> stac_types.Collection:
775
+ """
776
+ Update a collection.
777
+
778
+ This method updates an existing collection in the database by first finding
779
+ the collection by the id given in the keyword argument `collection_id`.
780
+ If no `collection_id` is given the id of the given collection object is used.
781
+ If the object and keyword collection ids don't match the sub items
782
+ collection id is updated else the items are left unchanged.
783
+ The updated collection is then returned.
784
+
785
+ Args:
786
+ collection: A STAC collection that needs to be updated.
787
+ kwargs: Additional keyword arguments.
788
+
789
+ Returns:
790
+ A STAC collection that has been updated in the database.
791
+
792
+ """
793
+ base_url = str(kwargs["request"].base_url)
794
+
795
+ collection_id = kwargs["request"].query_params.get(
796
+ "collection_id", collection["id"]
797
+ )
798
+
799
+ collection_links = CollectionLinks(
800
+ collection_id=collection["id"], base_url=base_url
801
+ ).create_links()
802
+ collection["links"] = collection_links
803
+
804
+ await self.database.update_collection(
805
+ collection_id=collection_id, collection=collection
806
+ )
807
+
808
+ return CollectionSerializer.db_to_stac(collection, base_url)
809
+
810
+ @overrides
811
+ async def delete_collection(
812
+ self, collection_id: str, **kwargs
813
+ ) -> stac_types.Collection:
814
+ """
815
+ Delete a collection.
816
+
817
+ This method deletes an existing collection in the database.
818
+
819
+ Args:
820
+ collection_id (str): The identifier of the collection that contains the item.
821
+ kwargs: Additional keyword arguments.
822
+
823
+ Returns:
824
+ None.
825
+
826
+ Raises:
827
+ NotFoundError: If the collection doesn't exist.
828
+ """
829
+ await self.database.delete_collection(collection_id=collection_id)
830
+ return None # type: ignore
831
+
832
+
833
+ @attr.s
834
+ class BulkTransactionsClient(BaseBulkTransactionsClient):
835
+ """A client for posting bulk transactions to a Postgres database.
836
+
837
+ Attributes:
838
+ session: An instance of `Session` to use for database connection.
839
+ database: An instance of `DatabaseLogic` to perform database operations.
840
+ """
841
+
842
+ database: BaseDatabaseLogic = attr.ib()
843
+ settings: ApiBaseSettings = attr.ib()
844
+ session: Session = attr.ib(default=attr.Factory(Session.create_from_env))
845
+ # database = DatabaseLogic()
846
+
847
+ def __attrs_post_init__(self):
848
+ """Create es engine."""
849
+ # settings = BaseSettings()
850
+ self.client = self.settings.create_client
851
+ # settings = SearchSettings()
852
+ # self.client = settings.create_client
853
+
854
+ def preprocess_item(
855
+ self, item: stac_types.Item, base_url, method: BulkTransactionMethod
856
+ ) -> stac_types.Item:
857
+ """Preprocess an item to match the data model.
858
+
859
+ Args:
860
+ item: The item to preprocess.
861
+ base_url: The base URL of the request.
862
+ method: The bulk transaction method.
863
+
864
+ Returns:
865
+ The preprocessed item.
866
+ """
867
+ exist_ok = method == BulkTransactionMethod.UPSERT
868
+ return self.database.sync_prep_create_item(
869
+ item=item, base_url=base_url, exist_ok=exist_ok
870
+ )
871
+
872
+ @overrides
873
+ def bulk_item_insert(
874
+ self, items: Items, chunk_size: Optional[int] = None, **kwargs
875
+ ) -> str:
876
+ """Perform a bulk insertion of items into the database using Elasticsearch.
877
+
878
+ Args:
879
+ items: The items to insert.
880
+ chunk_size: The size of each chunk for bulk processing.
881
+ **kwargs: Additional keyword arguments, such as `request` and `refresh`.
882
+
883
+ Returns:
884
+ A string indicating the number of items successfully added.
885
+ """
886
+ request = kwargs.get("request")
887
+ if request:
888
+ base_url = str(request.base_url)
889
+ else:
890
+ base_url = ""
891
+
892
+ processed_items = [
893
+ self.preprocess_item(item, base_url, items.method)
894
+ for item in items.items.values()
895
+ ]
896
+
897
+ # not a great way to get the collection_id-- should be part of the method signature
898
+ collection_id = processed_items[0]["collection"]
899
+
900
+ self.database.bulk_sync(
901
+ collection_id, processed_items, refresh=kwargs.get("refresh", False)
902
+ )
903
+
904
+ return f"Successfully added {len(processed_items)} Items."
905
+
906
+
907
+ @attr.s
908
+ class EsAsyncBaseFiltersClient(AsyncBaseFiltersClient):
909
+ """Defines a pattern for implementing the STAC filter extension."""
910
+
911
+ # todo: use the ES _mapping endpoint to dynamically find what fields exist
912
+ async def get_queryables(
913
+ self, collection_id: Optional[str] = None, **kwargs
914
+ ) -> Dict[str, Any]:
915
+ """Get the queryables available for the given collection_id.
916
+
917
+ If collection_id is None, returns the intersection of all
918
+ queryables over all collections.
919
+
920
+ This base implementation returns a blank queryable schema. This is not allowed
921
+ under OGC CQL but it is allowed by the STAC API Filter Extension
922
+
923
+ https://github.com/radiantearth/stac-api-spec/tree/master/fragments/filter#queryables
924
+
925
+ Args:
926
+ collection_id (str, optional): The id of the collection to get queryables for.
927
+ **kwargs: additional keyword arguments
928
+
929
+ Returns:
930
+ Dict[str, Any]: A dictionary containing the queryables for the given collection.
931
+ """
932
+ return {
933
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
934
+ "$id": "https://stac-api.example.com/queryables",
935
+ "type": "object",
936
+ "title": "Queryables for Example STAC API",
937
+ "description": "Queryable names for the example STAC API Item Search filter.",
938
+ "properties": {
939
+ "id": {
940
+ "description": "ID",
941
+ "$ref": "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json#/definitions/core/allOf/2/properties/id",
942
+ },
943
+ "collection": {
944
+ "description": "Collection",
945
+ "$ref": "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json#/definitions/core/allOf/2/then/properties/collection",
946
+ },
947
+ "geometry": {
948
+ "description": "Geometry",
949
+ "$ref": "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json#/definitions/core/allOf/1/oneOf/0/properties/geometry",
950
+ },
951
+ "datetime": {
952
+ "description": "Acquisition Timestamp",
953
+ "$ref": "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/datetime.json#/properties/datetime",
954
+ },
955
+ "created": {
956
+ "description": "Creation Timestamp",
957
+ "$ref": "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/datetime.json#/properties/created",
958
+ },
959
+ "updated": {
960
+ "description": "Creation Timestamp",
961
+ "$ref": "https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/datetime.json#/properties/updated",
962
+ },
963
+ "cloud_cover": {
964
+ "description": "Cloud Cover",
965
+ "$ref": "https://stac-extensions.github.io/eo/v1.0.0/schema.json#/definitions/fields/properties/eo:cloud_cover",
966
+ },
967
+ "cloud_shadow_percentage": {
968
+ "description": "Cloud Shadow Percentage",
969
+ "title": "Cloud Shadow Percentage",
970
+ "type": "number",
971
+ "minimum": 0,
972
+ "maximum": 100,
973
+ },
974
+ "nodata_pixel_percentage": {
975
+ "description": "No Data Pixel Percentage",
976
+ "title": "No Data Pixel Percentage",
977
+ "type": "number",
978
+ "minimum": 0,
979
+ "maximum": 100,
980
+ },
981
+ },
982
+ "additionalProperties": True,
983
+ }