1- from datetime import datetime , timedelta
2- import os
31import requests
42
53from api .api_types import Opportunity , OpportunityCollection , Search
64
75BLACKSKY_BASE_URL = "https://api.dev.blacksky.com/v1"
86
9- def stac_search_to_oppurtunities_request (search_request : Search ):
107
8+ def stac_search_to_oppurtunities_request (search_request : Search ):
119 """
1210 :param search_request: STAC search as passed on to find_future_items
1311 :return: a triple of iw request body, geom and bbox (geom and bbox needed again later to construct STAC answers)
1412 """
1513 bs_number , bs_product = "BS-TEST" , "Standard"
1614 if search_request .product_id :
17- bs_number , bs_product = search_request .product_id .split (':' )
15+ bs_number , bs_product = search_request .product_id .split (":" )
1816
1917 return {
20- "item" : {
21- "name" : "Blacksky_Request" ,
22- "description" : "STAC Sprint" ,
23- "timeframe" : {
24- "lowerBoundType" : "CLOSED" ,
25- "lowerEndpoint" : search_request .start_date .isoformat (),
26- "upperBoundType" : "CLOSED" ,
27- "upperEndpoint" : search_request .end_date .isoformat ()
28- },
29- "geometry" : {
30- "type" : "Point" ,
31- "coordinates" : [
32- search_request .geometry .dict ()['coordinates' ][0 ],
33- search_request .geometry .dict ()['coordinates' ][1 ],
34- 0
35- ]
36- },
37- "frequency" : "ONCE" ,
38- "offeringId" : "391327b7-f4ee-4e7f-a894-3cffef19cae0" ,
39- "frequency" : "ONCE" ,
40- "offeringParamValues" : {
41- "priority" : "STANDARD" ,
42- "sensor" : "blacksky"
43- },
44- "externalId" : "1234"
18+ "item" : {
19+ "name" : "Blacksky_Request" ,
20+ "description" : "STAC Sprint" ,
21+ "timeframe" : {
22+ "lowerBoundType" : "CLOSED" ,
23+ "lowerEndpoint" : search_request .start_date .isoformat (),
24+ "upperBoundType" : "CLOSED" ,
25+ "upperEndpoint" : search_request .end_date .isoformat (),
4526 },
46- "includeWeather" : True
47- }
27+ "geometry" : {
28+ "type" : "Point" ,
29+ "coordinates" : [
30+ search_request .geometry .dict ()["coordinates" ][0 ],
31+ search_request .geometry .dict ()["coordinates" ][1 ],
32+ 0 ,
33+ ],
34+ },
35+ "frequency" : "ONCE" ,
36+ "offeringId" : "391327b7-f4ee-4e7f-a894-3cffef19cae0" ,
37+ "frequency" : "ONCE" ,
38+ "offeringParamValues" : {"priority" : "STANDARD" , "sensor" : "blacksky" },
39+ "externalId" : "1234" ,
40+ },
41+ "includeWeather" : True ,
42+ }
4843
49- def get_oppurtunities (blacksky_request ):
5044
45+ def get_oppurtunities (blacksky_request , token ):
5146 headers = {
52- ' accept' : ' application/json' ,
53- ' Content-Type' : ' application/json' ,
54- ' authorization' : os . getenv ( "BLACKSKY_TOKEN" )
47+ " accept" : " application/json" ,
48+ " Content-Type" : " application/json" ,
49+ " authorization" : token ,
5550 }
5651
5752 r = requests .post (
58- f"{ BLACKSKY_BASE_URL } /feasibility/plan" ,
59- headers = headers ,
60- json = blacksky_request
53+ f"{ BLACKSKY_BASE_URL } /feasibility/plan" , headers = headers , json = blacksky_request
6154 )
62- return r .json ()[' opportunities' ]
55+ return r .json ()[" opportunities" ]
6356
6457
6558def oppurtunity_to_stac_item (iw ):
66-
6759 """
6860 translates a Planet Imaging Windows into a STAC item
6961 :param iw: an element from the 'imaging_windows' array of a /imaging_windows/[search_id] response
@@ -72,39 +64,28 @@ def oppurtunity_to_stac_item(iw):
7264
7365 item = Opportunity (
7466 id = iw ["satellite" ],
75- geometry = {
76- 'type' : 'Point' ,
77- 'coordinates' : [
78- iw ['longitude' ],
79- iw ['latitude' ],
80- 0
81- ]
82- },
67+ geometry = {"type" : "Point" , "coordinates" : [iw ["longitude" ], iw ["latitude" ], 0 ]},
8368 properties = {
84- 'title' : '' ,
85- 'datetime' : f"{ iw ['timestamp' ]} /{ iw ['timestamp' ]} " ,
86- 'constraints' : {
87- 'off_nadir' : iw ['offNadirAngleDegrees' ],
88- 'cloud_cover' : iw ['weatherForecast' ]['cloudCover' ]
89- }
90- })
69+ "title" : "" ,
70+ "datetime" : f"{ iw ['timestamp' ]} /{ iw ['timestamp' ]} " ,
71+ "constraints" : {
72+ "off_nadir" : iw ["offNadirAngleDegrees" ],
73+ "cloud_cover" : iw ["weatherForecast" ]["cloudCover" ],
74+ },
75+ },
76+ )
9177
9278 return item
9379
94- class BlackskyBackend :
9580
81+ class BlackskyBackend :
9682 async def find_opportunities (
97- self ,
98- search_request : Search ,
99- token : str ,
83+ self ,
84+ search_request : Search ,
85+ token : str ,
10086 ) -> OpportunityCollection :
101-
10287 blacksky_request = stac_search_to_oppurtunities_request (search_request )
103- oppurtunities = get_oppurtunities (blacksky_request )
104- stac_items = [
105- oppurtunity_to_stac_item (iw )
106- for iw
107- in oppurtunities
108- ]
88+ oppurtunities = get_oppurtunities (blacksky_request , token )
89+ stac_items = [oppurtunity_to_stac_item (iw ) for iw in oppurtunities ]
10990
11091 return OpportunityCollection (features = stac_items , links = [])
0 commit comments