Skip to content

Commit 1673778

Browse files
authored
Merge pull request planetlabs#213 from planetlabs/orders-sdk-example
add Notebook showing Orders creation & download with SDK
2 parents 6cc220f + 55d64a5 commit 1673778

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "bEcGVnFww2gV"
7+
},
8+
"source": [
9+
"# Orders API & Planet SDK \n",
10+
"\n",
11+
"Getting started with Planet SDK and the Orders API."
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {
17+
"id": "oA6ODbRP0mN9"
18+
},
19+
"source": [
20+
"## Authentication\n",
21+
"\n",
22+
"The new SDK supports a couple different methods of authentication (and soon, will support OIDC / Okta token-based authentication as well). [Read more in the docs here](https://planet-sdk-for-python-v2.readthedocs.io/en/latest/api/#planet.auth.Auth).\n",
23+
"\n",
24+
"For this example, I'll use the `getpass` tool to pass in my API key as a string to the `from_key` method."
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {
31+
"id": "nYHpseo0Qc3U"
32+
},
33+
"outputs": [],
34+
"source": [
35+
"from getpass import getpass\n",
36+
"from planet import Auth\n",
37+
"api_key = getpass('Enter your API key:')\n",
38+
"\n",
39+
"auth = Auth.from_key(api_key)"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {
45+
"id": "MHZXLWT41Ccn"
46+
},
47+
"source": [
48+
"# Demo"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {
54+
"id": "6z6I9B_636Ck"
55+
},
56+
"source": [
57+
"## Building an Order request"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {
63+
"id": "9xOEqQiw5yid"
64+
},
65+
"source": [
66+
"### Method 1: Old style\n",
67+
"\n",
68+
"With this method, you can build an Order request by directly using a blob of JSON -- similar to how you might have built an Order in the Python Client V1, or directly using the API via e.g., `requests`:"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {
75+
"id": "nh6waD-QzSh-"
76+
},
77+
"outputs": [],
78+
"source": [
79+
"request = {\n",
80+
" \"name\": \"test_order_sdk_method_1\",\n",
81+
" \"products\": [\n",
82+
" {\n",
83+
" \"item_ids\":[ \n",
84+
" \"20200922_183724_23_106a\",\n",
85+
" \"20200922_183722_17_106a\"\n",
86+
" ],\n",
87+
" \"item_type\": \"PSScene\",\n",
88+
" \"product_bundle\": \"analytic_udm2\"\n",
89+
" }\n",
90+
" ],\n",
91+
" \"tools\": [\n",
92+
" {\n",
93+
" \"reproject\": {\n",
94+
" \"projection\": \"EPSG:4326\",\n",
95+
" \"kernel\": \"cubic\"\n",
96+
" }\n",
97+
" }\n",
98+
" ]\n",
99+
"}"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {
105+
"id": "1yCzN0Ag64wf"
106+
},
107+
"source": [
108+
"### Method 2: New style\n",
109+
"\n",
110+
"In V2 ofthe SDK, you can also build an Order request object using the new `order_request` functionality -- here you can specify all Order details including products, bundles & fallback bundles, cloud delivery configuration, tools & toolchain operations, etc. [Read more in the docs here](https://planet-sdk-for-python-v2.readthedocs.io/en/latest/api/#planet.order_request)."
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": null,
116+
"metadata": {
117+
"id": "FwFi1bktMY2u"
118+
},
119+
"outputs": [],
120+
"source": [
121+
"from planet import order_request\n",
122+
"\n",
123+
"item_ids = [\"20200922_183724_23_106a\", \"20200922_183722_17_106a\"]\n",
124+
"\n",
125+
"products = [\n",
126+
" order_request.product(item_ids, 'analytic_udm2', 'PSScene')\n",
127+
"]\n",
128+
"\n",
129+
"tools = [\n",
130+
" order_request.reproject_tool(projection='EPSG:4326', kernel='cubic')\n",
131+
"]\n",
132+
"\n",
133+
"request = order_request.build_request(\n",
134+
" 'test_order_sdk_method_2', products=products, tools=tools)\n"
135+
]
136+
},
137+
{
138+
"cell_type": "markdown",
139+
"metadata": {
140+
"id": "f_k8HHcL8gQ5"
141+
},
142+
"source": [
143+
"## Create the Order\n",
144+
"\n",
145+
"Regardless of the method you use to build your order, the next step after building an Order is to send a \"create\" request to the Orders API.\n",
146+
"\n",
147+
"To do this, we'll create a `Session` to manage our communcation with Planet in general -- this will make use of that `auth` object we created earlier. Within the context of this Session, we'll create an Orders API-specific `client` to handle interactions with the Orders API."
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"metadata": {
154+
"id": "GCARBCQZMm2v"
155+
},
156+
"outputs": [],
157+
"source": [
158+
"from planet import Session, OrdersClient\n",
159+
"\n",
160+
"# an async Orders client to request order creation\n",
161+
"async def main():\n",
162+
" async with Session(auth=auth) as sess:\n",
163+
" cl = OrdersClient(sess)\n",
164+
" order = await cl.create_order(request)\n",
165+
"\n",
166+
"# async magic to remember: \"async def\" to create a coroutine, then \"await\" to make it run\n",
167+
"await main()"
168+
]
169+
},
170+
{
171+
"cell_type": "markdown",
172+
"metadata": {
173+
"id": "xOyyXodY6cL0"
174+
},
175+
"source": [
176+
"## Alternative: Create the Order, then monitor status until complete"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {
183+
"id": "5lFCwq1cNCda"
184+
},
185+
"outputs": [],
186+
"source": [
187+
"from planet import reporting, Session, OrdersClient\n",
188+
"\n",
189+
"# remember: \"async def\" to create the async coroutine\n",
190+
"async def create_poll_and_download():\n",
191+
" async with Session(auth=auth) as sess:\n",
192+
" cl = OrdersClient(sess)\n",
193+
"\n",
194+
" # Use \"reporting\" to manage polling for order status\n",
195+
" with reporting.StateBar(state='creating') as bar:\n",
196+
" # create order via Orders client\n",
197+
" order = await cl.create_order(request)\n",
198+
" bar.update(state='created', order_id=order['id'])\n",
199+
"\n",
200+
" # poll...poll...poll...\n",
201+
" await cl.wait(order['id'], callback=bar.update_state)\n",
202+
"\n",
203+
" # if we get here that means the order completed. Yay! Download the files.\n",
204+
" await cl.download_order(order['id'])\n",
205+
"\n",
206+
"# remember: \"await\" to run the thing\n",
207+
"await create_poll_and_download()"
208+
]
209+
}
210+
],
211+
"metadata": {
212+
"colab": {
213+
"collapsed_sections": [],
214+
"provenance": []
215+
},
216+
"kernelspec": {
217+
"display_name": "Python 3",
218+
"language": "python",
219+
"name": "python3"
220+
},
221+
"language_info": {
222+
"codemirror_mode": {
223+
"name": "ipython",
224+
"version": 3
225+
},
226+
"file_extension": ".py",
227+
"mimetype": "text/x-python",
228+
"name": "python",
229+
"nbconvert_exporter": "python",
230+
"pygments_lexer": "ipython3",
231+
"version": "3.8.10"
232+
}
233+
},
234+
"nbformat": 4,
235+
"nbformat_minor": 1
236+
}

0 commit comments

Comments
 (0)