2
2
3
3
[ ![ Build Status] ( https://travis-ci.org/planetlabs/planet-client-python.svg?branch=master )] ( https://travis-ci.org/planetlabs/planet-client-python )
4
4
5
- Python client library and CLI for Planet's public APIs.
5
+ Python client library for Planet's APIs.
6
6
7
7
The client provides access to the following Planet APIs:
8
8
* [ analytics] ( https://developers.planet.com/docs/analytics/ )
@@ -14,7 +14,7 @@ The client provides access to the following Planet APIs:
14
14
15
15
### Prerequisites
16
16
17
- * Python version 3.6 +
17
+ * Python version 3.7 +
18
18
19
19
### Install package
20
20
@@ -28,49 +28,158 @@ flag is highly recommended for those new to [pip](https://pip.pypa.io).
28
28
A PEX executable (Windows not supported) and source releases are
29
29
[ here] ( https://github.com/planetlabs/planet-client-python/releases/latest ) .
30
30
31
- ## Documentation
31
+ ## Authentication
32
32
33
- Online documentation:
34
- https://planetlabs.github.io/planet-client-python/index.html
33
+ Planet's APIs require an account for use.
34
+ [ Sign up here ] ( https://www.planet.com/explorer/?signup ) .
35
35
36
- Documentation is also provided for download
37
- [ here] ( https://github.com/planetlabs/planet-client-python/releases/latest ) .
38
36
37
+ ## Quick Start
39
38
40
- ## Development
39
+ The client modules within the Python library are asynchronous, which greatly
40
+ speeds up many interactions with Planet's APIs. Support for asynchronous
41
+ development is native to Python 3.6+ via the
42
+ [ ` asyncio ` module] ( https://docs.python.org/3/library/asyncio.html ) . A great
43
+ resource for getting started with asynchronous programming in Python is
44
+ https://project-awesome.org/timofurrer/awesome-asyncio . The Writings and Talks
45
+ sections are particularly helpful in getting oriented.
41
46
42
- To contribute or develop with this library, see
43
- [ CONTRIBUTING] ( https://github.com/planetlabs/planet-client-python/CONTRIBUTING.md )
47
+ ``` python
48
+ import asyncio
49
+ import os
44
50
51
+ import planet
45
52
46
- ## API Key
53
+ API_KEY = os.getenv( ' PL_API_KEY ' )
47
54
48
- The API requires an account for use. [ Signup here] ( https://www.planet.com/explorer/?signup ) .
55
+ image_ids = [' 3949357_1454705_2020-12-01_241c' ]
56
+ order_details = planet.OrderDetails(
57
+ ' test_order' ,
58
+ [planet.Product(image_ids, ' analytic' , ' psorthotile' )]
59
+ )
49
60
50
- This can be provided via the environment variable ` PL_API_KEY ` or the flag ` -k ` or ` --api-key ` .
61
+ async def create_order (order_details ):
62
+ async with planet.Session(auth = (API_KEY , ' ' )) as ps:
63
+ client = planet.OrdersClient(ps)
64
+ return await client.create_order(order_details)
51
65
52
- Using ` planet init ` your account credentials (login/password) can be used to obtain the api key.
66
+ oid = asyncio.run(create_order(order_details))
67
+ print (oid)
68
+ ```
53
69
70
+ Not into async? No problem. Just wrap the library and async operations together
71
+ and call from your synchronous code.
54
72
55
- # Example CLI Usage
73
+ ``` python
74
+ def sync_create_order (order_details ):
75
+ return asyncio.run(create_order(order_details))
56
76
57
- ** Hint:** autocompletion can be enabled in some shells using:
58
- ``` console
59
- $ eval "$(_PLANET_COMPLETE=source planet)"
77
+ oid = sync_create_order(order_details)
78
+ print (oid)
60
79
```
61
80
62
- Basics and help:
63
-
64
- ``` console
65
- $ planet --help
81
+ When using ` asyncio.run ` to develop synchronous code with the async library,
82
+ keep in mind this excerpt from the
83
+ [ asyncio.run] ( https://docs.python.org/3/library/asyncio-task.html#asyncio.run )
84
+ documentation:
85
+
86
+ "* This function always creates a new event loop and closes it at the end. It
87
+ should be used as a main entry point for asyncio programs, and should ideally
88
+ only be called once.* "
89
+
90
+ Do you have a use case where native synchronous support is essential? If so,
91
+ please contribute to
92
+ [ Determine need for synchronous support] ( https://github.com/planetlabs/planet-client-python/issues/251 )
93
+
94
+
95
+ Why async? Because things get * really cool* when you want to work with multiple
96
+ orders. Here's an example of submitting two orders, waiting for them to
97
+ complete, and downloading them. The orders each clip a set of images to a
98
+ specific area of interest (AOI), so they cannot be combined into one order.
99
+ (hint: [ Planet Explorer] ( https://www.planet.com/explorer/ ) was used to define
100
+ the AOIs and get the image ids.)
101
+
102
+
103
+ ``` python
104
+ import asyncio
105
+ import os
106
+
107
+ import planet
108
+
109
+ API_KEY = os.getenv(' PL_API_KEY' )
110
+
111
+ iowa_aoi = {
112
+ " type" : " Polygon" ,
113
+ " coordinates" : [[
114
+ [- 91.198465 , 42.893071 ],
115
+ [- 91.121931 , 42.893071 ],
116
+ [- 91.121931 , 42.946205 ],
117
+ [- 91.198465 , 42.946205 ],
118
+ [- 91.198465 , 42.893071 ]]]
119
+ }
120
+
121
+ iowa_images = [
122
+ ' 20200925_161029_69_2223' ,
123
+ ' 20200925_161027_48_2223'
124
+ ]
125
+ iowa_order = planet.OrderDetails(
126
+ ' iowa_order' ,
127
+ [planet.Product(iowa_images, ' analytic' , ' PSScene4Band' )],
128
+ tools = [planet.Tool(' clip' , {' aoi' : iowa_aoi})]
129
+ )
130
+
131
+ oregon_aoi = {
132
+ " type" : " Polygon" ,
133
+ " coordinates" : [[
134
+ [- 117.558734 , 45.229745 ],
135
+ [- 117.452447 , 45.229745 ],
136
+ [- 117.452447 , 45.301865 ],
137
+ [- 117.558734 , 45.301865 ],
138
+ [- 117.558734 , 45.229745 ]]]
139
+ }
140
+
141
+ oregon_images = [
142
+ ' 20200909_182525_1014' ,
143
+ ' 20200909_182524_1014'
144
+ ]
145
+ oregon_order = planet.OrderDetails(
146
+ ' oregon_order' ,
147
+ [planet.Product(oregon_images, ' analytic' , ' PSScene4Band' )],
148
+ tools = [planet.Tool(' clip' , {' aoi' : oregon_aoi})]
149
+ )
150
+
151
+
152
+ async def create_and_download (order_detail , client ):
153
+ oid = await client.create_order(order_detail)
154
+ print (oid)
155
+ state = await client.poll(oid, verbose = True )
156
+ print (state)
157
+ filenames = await client.download_order(oid, progress_bar = True )
158
+ print (f ' downloaded { oid} , { len (filenames)} files downloaded. ' )
159
+
160
+
161
+ async def main ():
162
+ async with planet.Session(auth = (API_KEY , ' ' )) as ps:
163
+ client = planet.OrdersClient(ps)
164
+ await asyncio.gather(
165
+ create_and_download(iowa_order, client),
166
+ create_and_download(oregon_order, client)
167
+ )
168
+
169
+ asyncio.run(main())
66
170
```
171
+ [ Example output] ( example_output.md )
67
172
68
- Specific API client usage:
69
- ``` console
70
- $ planet data
71
- ```
72
173
73
- Specific command help:
74
- ``` console
75
- $ planet data download --help
76
- ```
174
+ ## Documentation
175
+
176
+ Online documentation:
177
+ https://planetlabs.github.io/planet-client-python/index.html
178
+
179
+ Documentation is also provided for download
180
+ [ here] ( https://github.com/planetlabs/planet-client-python/releases/latest ) .
181
+
182
+ ## Development
183
+
184
+ To contribute or develop with this library, see
185
+ [ CONTRIBUTING] ( https://github.com/planetlabs/planet-client-python/CONTRIBUTING.md )
0 commit comments