Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return existing links in pgstac #282

Merged
merged 8 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ coverage.xml
*.log
.git
.envrc

venv
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,6 @@ docs/api/*

# Direnv
.envrc

# Virtualenv
venv
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Fixed

* Links stored with Collections and Items (e.g. license links) are now returned with those STAC objects ([#282](https://github.com/stac-utils/stac-fastapi/pull/282))

## [2.2.0]

### Added
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ run-database:
run-joplin-sqlalchemy:
docker-compose run --rm loadjoplin-sqlalchemy

.PHONY: run-joplin-pgstac
run-joplin-pgstac:
docker-compose run --rm loadjoplin-pgstac

.PHONY: test
test: test-sqlalchemy test-pgstac

Expand Down
24 changes: 15 additions & 9 deletions scripts/ingest_joplin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@
raise Exception("You must include full path/port to stac instance")


def post_or_put(url: str, data: dict):
"""Post or put data to url."""
r = requests.post(url, json=data)
if r.status_code == 409:
# Exists, so update
r = requests.put(url, json=data)
# Unchanged may throw a 404
if not r.status_code == 404:
r.raise_for_status()
else:
r.raise_for_status()


def ingest_joplin_data(app_host: str = app_host, data_dir: Path = joplindata):
"""ingest data."""

with open(data_dir / "collection.json") as f:
collection = json.load(f)

r = requests.post(urljoin(app_host, "collections"), json=collection)
if r.status_code not in (200, 409):
r.raise_for_status()
post_or_put(urljoin(app_host, "/collections"), collection)

with open(data_dir / "index.geojson") as f:
index = json.load(f)

for feat in index["features"]:
del feat["stac_extensions"]
r = requests.post(
urljoin(app_host, f"collections/{collection['id']}/items"), json=feat
)
if r.status_code == 409:
continue
r.raise_for_status()
post_or_put(urljoin(app_host, f"collections/{collection['id']}/items"), feat)


if __name__ == "__main__":
Expand Down
20 changes: 17 additions & 3 deletions stac_fastapi/pgstac/stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
NumType = Union[float, int]


def _extended_links(
original_links: Optional[List[Dict[str, Any]]], new_links: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
"""Extend links with new links."""
if original_links is None:
return new_links
new_link_rels = set([link["rel"] for link in new_links])
return new_links + [
link for link in original_links if link["rel"] not in new_link_rels
]


lossyrob marked this conversation as resolved.
Show resolved Hide resolved
@attr.s
class CoreCrudClient(AsyncBaseCoreClient):
"""Client for core endpoints defined by stac."""
Expand All @@ -45,9 +57,11 @@ async def all_collections(self, **kwargs) -> Collections:
if collections is not None and len(collections) > 0:
for c in collections:
coll = Collection(**c)
coll["links"] = await CollectionLinks(
links = await CollectionLinks(
collection_id=coll["id"], request=request
).get_links()
coll["links"] = _extended_links(coll.get("links"), links)

linked_collections.append(coll)
links = [
{
Expand Down Expand Up @@ -95,7 +109,7 @@ async def get_collection(self, id: str, **kwargs) -> Collection:
if collection is None:
raise NotFoundError(f"Collection {id} does not exist.")
links = await CollectionLinks(collection_id=id, request=request).get_links()
collection["links"] = links
collection["links"] = _extended_links(collection.get("links"), links)
return Collection(**collection)

async def _search_base(
Expand Down Expand Up @@ -152,7 +166,7 @@ async def _search_base(
item_id=feature["id"],
request=request,
).get_links()
feature["links"] = links
feature["links"] = _extended_links(feature.get("links"), links)
exclude = search_request.fields.exclude
if exclude and len(exclude) == 0:
exclude = None
Expand Down
21 changes: 3 additions & 18 deletions stac_fastapi/pgstac/tests/data/test_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,9 @@
},
"links": [
{
"href": "http://localhost:8081/collections/landsat-8-l1",
"rel": "self",
"type": "application/json"
},
{
"href": "http://localhost:8081/",
"rel": "parent",
"type": "application/json"
},
{
"href": "http://localhost:8081/collections/landsat-8-l1/items",
"rel": "item",
"type": "application/geo+json"
},
{
"href": "http://localhost:8081/",
"rel": "root",
"type": "application/json"
"rel": "license",
"href": "https://creativecommons.org/licenses/publicdomain/",
"title": "public domain"
}
],
"title": "Landsat 8 L1",
Expand Down
11 changes: 11 additions & 0 deletions stac_fastapi/pgstac/tests/resources/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,14 @@ async def test_returns_valid_links_in_collections(app_client, load_test_data):
for i in collection.to_dict()["links"]
if i not in single_coll_mocked_link.to_dict()["links"]
] == []


@pytest.mark.asyncio
async def test_returns_license_link(app_client, load_test_collection):
coll = load_test_collection

resp = await app_client.get(f"/collections/{coll.id}")
assert resp.status_code == 200
resp_json = resp.json()
link_rel_types = [link["rel"] for link in resp_json["links"]]
assert "license" in link_rel_types
8 changes: 7 additions & 1 deletion stac_fastapi/testdata/joplin/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"description": "This imagery was acquired by the NOAA Remote Sensing Division to support NOAA national security and emergency response requirements. In addition, it will be used for ongoing research efforts for testing and developing standards for airborne digital imagery. Individual images have been combined into a larger mosaic and tiled for distribution. The approximate ground sample distance (GSD) for each pixel is 35 cm (1.14 feet).",
"stac_version": "1.0.0",
"license": "public-domain",
"links": [],
"links": [
{
"rel": "license",
"href": "https://creativecommons.org/licenses/publicdomain/",
"title": "public domain"
}
],
"type": "collection",
"extent": {
"spatial": {
Expand Down