-
Notifications
You must be signed in to change notification settings - Fork 21
/
stac.py
61 lines (54 loc) · 1.79 KB
/
stac.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
from pystac_client import Client
import sliderule
import requests
import json
import sys
# parameters
debug = False
client = False
manual = True
page = None
url = 'https://cmr.earthdata.nasa.gov/stac/LPCLOUD'
headers = {'Content-Type': 'application/json'}
limit = 200
collections = ["HLSS30.v2.0", "HLSL30.v2.0"]
time_start = "2021-01-01T00:00:00Z"
time_end = "2022-01-01T23:59:59Z"
coordinates = [[[coord["lon"], coord["lat"]] for coord in polygon] for polygon in [sliderule.toregion("examples/grandmesa.geojson")["poly"]]]
# debug output
if debug:
import logging
import http.client as http_client
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
# client stac request
if client:
catalog = Client.open(url)
timeRange = '{}/{}'.format(time_start, time_end)
geometry = {"type": "Polygon", "coordinates": coordinates}
results = catalog.search(collections=collections, datetime=timeRange, intersects=geometry)
items = results.get_all_items_as_dict()
print(f"Returned {results.matched()} features")
# manual stac request
if manual:
rqst = {
"limit": 100,
"datetime": '{}/{}'.format(time_start, time_end),
"collections": collections,
"intersects": {
"type": "Polygon",
"coordinates": coordinates
}
}
if page:
rqst["page"] = page
context = requests.Session()
context.trust_env = False
data = context.post(url+"/search", data=json.dumps(rqst), headers=headers)
data.raise_for_status()
geojson = data.json()
print("Returned {} features".format(geojson["context"]["returned"]))