Skip to content

Commit 6c00058

Browse files
POST a feature collection to the items route (#367)
* process feature collection * add insert feature colllection test * tests pass * run pre-commit * clean up test * run pre-commit again * Update changelog * return none * pre-commit * Update return type of create_item function Co-authored-by: Nathan Zimmerman <[email protected]>
1 parent a3185b4 commit 6c00058

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
* Add hook to allow adding dependencies to routes. ([#295](https://github.com/stac-utils/stac-fastapi/pull/295))
8+
* Ability to POST an ItemCollection to the collections/{collectionId}/items route. ([#367](https://github.com/stac-utils/stac-fastapi/pull/367))
89
* Add STAC API - Collections conformance class. ([383](https://github.com/stac-utils/stac-fastapi/pull/383))
910

1011
### Changed

stac_fastapi/sqlalchemy/stac_fastapi/sqlalchemy/transactions.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@ class TransactionsClient(BaseTransactionsClient):
3535
)
3636

3737
def create_item(
38-
self, item: stac_types.Item, **kwargs
39-
) -> Optional[Union[stac_types.Item, Response]]:
38+
self, model: Union[stac_types.Item, stac_types.ItemCollection], **kwargs
39+
) -> Optional[stac_types.Item]:
4040
"""Create item."""
4141
base_url = str(kwargs["request"].base_url)
42-
data = self.item_serializer.stac_to_db(item)
42+
43+
# If a feature collection is posted
44+
if model["type"] == "FeatureCollection":
45+
bulk_client = BulkTransactionsClient(session=self.session)
46+
bulk_client.bulk_item_insert(items=model["features"])
47+
return None
48+
49+
# Otherwise a single item has been posted
50+
data = self.item_serializer.stac_to_db(model)
4351
with self.session.writer.context_session() as session:
4452
session.add(data)
4553
return self.item_serializer.db_to_stac(data, base_url)

stac_fastapi/sqlalchemy/tests/clients/test_postgres.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,35 @@ def test_bulk_item_insert_chunked(
312312
)
313313

314314

315+
def test_feature_collection_insert(
316+
postgres_core: CoreCrudClient,
317+
postgres_transactions: TransactionsClient,
318+
load_test_data: Callable,
319+
):
320+
coll = load_test_data("test_collection.json")
321+
postgres_transactions.create_collection(coll, request=MockStarletteRequest)
322+
323+
item = load_test_data("test_item.json")
324+
325+
features = []
326+
for _ in range(10):
327+
_item = deepcopy(item)
328+
_item["id"] = str(uuid.uuid4())
329+
features.append(_item)
330+
331+
feature_collection = {"type": "FeatureCollection", "features": features}
332+
333+
postgres_transactions.create_item(feature_collection, request=MockStarletteRequest)
334+
335+
fc = postgres_core.item_collection(coll["id"], request=MockStarletteRequest)
336+
assert len(fc["features"]) >= 10
337+
338+
for item in features:
339+
postgres_transactions.delete_item(
340+
item["id"], item["collection"], request=MockStarletteRequest
341+
)
342+
343+
315344
def test_landing_page_no_collection_title(
316345
postgres_core: CoreCrudClient,
317346
postgres_transactions: TransactionsClient,

0 commit comments

Comments
 (0)