Skip to content

Commit

Permalink
Merge pull request #4 from darrenwiens/pystac-client
Browse files Browse the repository at this point in the history
use pystac-client
  • Loading branch information
darrenwiens authored Oct 6, 2021
2 parents 4a9eef0 + 8e8a25a commit 207367d
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 170 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Display the UI for a STAC API::
ui = STAC_Query_UI("https://earth-search.aws.element84.com/v0")
ui.display()

After you have run the query, retrieve the results::
After you have run the query, retrieve the results from ``ui.query_results``::

ui.query_results
4 changes: 2 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Display the UI for a STAC API::
ui = STAC_Query_UI("https://earth-search.aws.element84.com/v0")
ui.display()

After you have run the query, retrieve the results::
After you have run the query, retrieve the results from ``ui.query_results``::

ui.query_results
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ipywidgets>=7.6.5
jupyterlab>=3.0.0
requests>=2.26.0
pystac-client>=0.3.0
38 changes: 8 additions & 30 deletions stac_nb/stac_nb.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from IPython.display import display as iDisplay
import ipywidgets as widgets
import json
import os
import requests
from pystac_client import Client


class STAC_Query_UI(widgets.VBox):
def __init__(self, stac_api: str, **kwargs):
super().__init__(**kwargs)
self.stac_api = stac_api
self.client = Client.open(stac_api)
self.query_results = None

collections_endpoint = os.path.join(stac_api, "collections")
collection_response = get_request(collections_endpoint, headers).json()
collection_ids = [i["id"] for i in collection_response["collections"]]
collection_ids = [c.id for c in self.client.get_all_collections()]

self.collections_w = widgets.SelectMultiple(
options=collection_ids,
Expand Down Expand Up @@ -100,7 +96,7 @@ def click_button(self, b):
payload_dict = dict(
collections=self.collections_w.value,
datetime=f"{start_datetime}/{end_datetime}",
limit=self.limit_w.value,
max_items=self.limit_w.value,
)

try:
Expand All @@ -117,29 +113,11 @@ def click_button(self, b):
if ids is not None and len(ids) > 0:
payload_dict["ids"] = [x.strip(" ") for x in ids.split(",")]

payload = json.dumps(payload_dict)
query_response = self.client.search(**payload_dict)

search_endpoint = os.path.join(self.stac_api, "search")

query_response = post_request(search_endpoint, headers=headers, data=payload)

self.query_results = query_response.json()
self.query_results = list(query_response.get_items())

with self.response_text:
if self.show_query_w.value:
print(f"QUERY: {vars(query_response.request)}")
matches = query_response.json()
print(f"MATCHES: {len(matches['features'])}")


headers = {"Content-Type": "application/json"}


def get_request(url, headers={}):
response = requests.get(url, headers=headers)
return response


def post_request(url, headers={}, data={}):
response = requests.post(url, headers=headers, data=data)
return response
print(f"QUERY: {vars(query_response)}")
print(f"MATCHES: {len(self.query_results)}")
259 changes: 157 additions & 102 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,165 @@
import pytest
from pystac_client.item_search import ItemSearch


STAC_API = "https://api/endpoint"


@pytest.fixture(autouse=True)
def mock_responses(
requests_mock,
landing_page_response,
collections_response,
collection_response,
search_response,
):

requests_mock.get(f"{STAC_API}", json=landing_page_response)
requests_mock.get(f"{STAC_API}/collections", json=collections_response)
requests_mock.get(
f"{STAC_API}/collections/simple-collection", json=collection_response
)
requests_mock.post(f"{STAC_API}/search", json=search_response)


@pytest.fixture(scope="module")
def landing_page_response():
return {
"stac_version": "1.0.0",
"stac_api_version": "0.9.0",
"type": "Catalog",
"id": "stac-server",
"title": "STAC API",
"description": "Test STAC API",
"links": [
{
"rel": "child",
"href": "https://api/endpoint/collections/simple-collection",
},
{
"rel": "service-desc",
"type": "application/vnd.oai.openapi+json;version=3.0",
"href": "https://api/endpoint/api",
},
{
"rel": "conformance",
"type": "application/json",
"href": "https://api/endpoint/conformance",
},
{
"rel": "data",
"type": "application/json",
"href": "https://api/endpoint/collections",
},
{
"rel": "self",
"type": "application/json",
"href": "https://api/endpoint/",
},
{
"rel": "search",
"type": "application/geo+json",
"href": "https://api/endpoint/search",
},
{"rel": "docs", "href": "https://stac-utils.github.io/stac-server/"},
],
"conformsTo": [
"https://api.stacspec.org/v1.0.0-beta.2/core",
"https://api.stacspec.org/v1.0.0-beta.2/item-search",
"https://api.stacspec.org/v1.0.0-beta.2/item-search#fields",
"https://api.stacspec.org/v1.0.0-beta.2/item-search#sort",
"https://api.stacspec.org/v1.0.0-beta.2/item-search#query",
"http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/core",
"http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/oas30",
"http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/geojson",
],
}


@pytest.fixture(scope="module")
def collections_response(collection_response):
return {"collections": [collection_response]}


@pytest.fixture(scope="module")
def collection_response():
return {
"collections": [
"id": "simple-collection",
"type": "Collection",
"stac_extensions": [
"https://stac-extensions.github.io/eo/v1.0.0/schema.json",
"https://stac-extensions.github.io/projection/v1.0.0/schema.json",
"https://stac-extensions.github.io/view/v1.0.0/schema.json",
],
"stac_version": "1.0.0",
"description": "A simple collection demonstrating core catalog fields with links to a couple of items",
"title": "Simple Example Collection",
"providers": [
{
"id": "simple-collection",
"type": "Collection",
"stac_extensions": [
"https://stac-extensions.github.io/eo/v1.0.0/schema.json",
"https://stac-extensions.github.io/projection/v1.0.0/schema.json",
"https://stac-extensions.github.io/view/v1.0.0/schema.json",
],
"stac_version": "1.0.0",
"description": "A simple collection demonstrating core catalog fields with links to a couple of items",
"title": "Simple Example Collection",
"providers": [
{
"name": "Remote Data, Inc",
"description": "Producers of awesome spatiotemporal assets",
"roles": ["producer", "processor"],
"url": "http://remotedata.io",
}
],
"extent": {
"spatial": {
"bbox": [
[
172.91173669923782,
1.3438851951615003,
172.95469614953714,
1.3690476620161975,
]
]
},
"temporal": {
"interval": [
["2020-12-11T22:38:32.125Z", "2020-12-14T18:02:31.437Z"]
]
},
},
"license": "CC-BY-4.0",
"summaries": {
"platform": ["cool_sat1", "cool_sat2"],
"constellation": ["ion"],
"instruments": ["cool_sensor_v1", "cool_sensor_v2"],
"gsd": {"minimum": 0.512, "maximum": 0.66},
"eo:cloud_cover": {"minimum": 1.2, "maximum": 1.2},
"proj:epsg": {"minimum": 32659, "maximum": 32659},
"view:sun_elevation": {"minimum": 54.9, "maximum": 54.9},
"view:off_nadir": {"minimum": 3.8, "maximum": 3.8},
"view:sun_azimuth": {"minimum": 135.7, "maximum": 135.7},
},
"links": [
{
"rel": "root",
"href": "./collection.json",
"type": "application/json",
"title": "Simple Example Collection",
},
{
"rel": "item",
"href": "./simple-item.json",
"type": "application/geo+json",
"title": "Simple Item",
},
{
"rel": "item",
"href": "./core-item.json",
"type": "application/geo+json",
"title": "Core Item",
},
{
"rel": "item",
"href": "./extended-item.json",
"type": "application/geo+json",
"title": "Extended Item",
},
{
"rel": "self",
"href": "https://raw.githubusercontent.com/radiantearth/stac-spec/v1.0.0/examples/collection.json",
"type": "application/json",
},
],
"name": "Remote Data, Inc",
"description": "Producers of awesome spatiotemporal assets",
"roles": ["producer", "processor"],
"url": "http://remotedata.io",
}
]
],
"extent": {
"spatial": {
"bbox": [
[
172.91173669923782,
1.3438851951615003,
172.95469614953714,
1.3690476620161975,
]
]
},
"temporal": {
"interval": [["2020-12-11T22:38:32.125Z", "2020-12-14T18:02:31.437Z"]]
},
},
"license": "CC-BY-4.0",
"summaries": {
"platform": ["cool_sat1", "cool_sat2"],
"constellation": ["ion"],
"instruments": ["cool_sensor_v1", "cool_sensor_v2"],
"gsd": {"minimum": 0.512, "maximum": 0.66},
"eo:cloud_cover": {"minimum": 1.2, "maximum": 1.2},
"proj:epsg": {"minimum": 32659, "maximum": 32659},
"view:sun_elevation": {"minimum": 54.9, "maximum": 54.9},
"view:off_nadir": {"minimum": 3.8, "maximum": 3.8},
"view:sun_azimuth": {"minimum": 135.7, "maximum": 135.7},
},
"links": [
{
"rel": "root",
"href": "./collection.json",
"type": "application/json",
"title": "Simple Example Collection",
},
{
"rel": "item",
"href": "./simple-item.json",
"type": "application/geo+json",
"title": "Simple Item",
},
{
"rel": "item",
"href": "./core-item.json",
"type": "application/geo+json",
"title": "Core Item",
},
{
"rel": "item",
"href": "./extended-item.json",
"type": "application/geo+json",
"title": "Extended Item",
},
{
"rel": "self",
"href": "https://raw.githubusercontent.com/radiantearth/stac-spec/v1.0.0/examples/collection.json",
"type": "application/json",
},
],
}


Expand All @@ -95,9 +169,9 @@ def search_response():
"type": "FeatureCollection",
"stac_version": "1.0.0-beta.2",
"stac_extensions": [],
"context": {"page": 1, "limit": 10, "matched": 3337, "returned": 10},
"context": {"page": 1, "limit": 10, "matched": 3337, "returned": 1},
"numberMatched": 3337,
"numberReturned": 10,
"numberReturned": 1,
"features": [
{
"stac_version": "1.0.0",
Expand All @@ -124,26 +198,7 @@ def search_response():
},
"properties": {"datetime": "2020-12-11T22:38:32.125000Z"},
"collection": "simple-collection",
"links": [
{
"rel": "collection",
"href": "./collection.json",
"type": "application/json",
"title": "Simple Example Collection",
},
{
"rel": "root",
"href": "./collection.json",
"type": "application/json",
"title": "Simple Example Collection",
},
{
"rel": "parent",
"href": "./collection.json",
"type": "application/json",
"title": "Simple Example Collection",
},
],
"links": [],
"assets": {
"visual": {
"href": "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2.tif",
Expand Down
Loading

0 comments on commit 207367d

Please sign in to comment.