-
Notifications
You must be signed in to change notification settings - Fork 95
Code snippets for all APIs #939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kevinlacaille
wants to merge
46
commits into
maint-2.0
Choose a base branch
from
code_snippets_orders_api-540
base: maint-2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+699
−0
Draft
Changes from 20 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
8731065
Skelleton readme and file.
kevinlacaille fc7455c
Added a few more snippets.
kevinlacaille 88cf85f
Added a few more snippets.
kevinlacaille 21953fd
Added all the snippets.
kevinlacaille 5d57005
Added an order example, though may be not needed.
kevinlacaille 9dbcaca
Removed unused lib.
kevinlacaille a33d17f
Created skeleton for data API snippets.
kevinlacaille 69c717f
Added about half the snippets for data.
kevinlacaille 0a6eb42
Added all the snippets and a search filter example.
kevinlacaille e89e8e2
Changed print to callback and return
kevinlacaille c83e2fe
Return nothing for wait.
kevinlacaille bbdc717
Added all snippets. TO DO: create request.
kevinlacaille 34a2a61
Added docstring to order request example.
kevinlacaille 74c9ab9
Added docstring to search filter creation.
kevinlacaille 8595f24
Added a create request function.
kevinlacaille 3688ebe
Added keywords.
kevinlacaille d719f21
Added keywords.
kevinlacaille f6c24cd
Updated comment.
kevinlacaille ff1526a
linting
kevinlacaille a81d00b
linting
kevinlacaille fdb21da
new structure for sdk docs, based on cli
cholmes 70269e9
new structure for sdk docs, based on cli
cholmes 00191ba
fleshed out subscriptions docs for SDK
cholmes 74e9459
Added pymdownx.snippets as a MD ext.
kevinlacaille 4a1f78d
Updated docs requirements.
kevinlacaille 6ba91c4
Merge branch 'code_snippets_orders_api-540' of github.com:planetlabs/…
kevinlacaille 45c132b
Removed and added to a new branch, for docs, snippets-docs-936.
kevinlacaille 84a9c1f
Removed and added to a new branch, for docs, snippets-docs-936.
kevinlacaille d18bdc1
Removed and added to a new branch, for docs, snippets-docs-936.
kevinlacaille b80cd53
Removed unneeded readme
kevinlacaille 5ea5fb8
Renamed file for testing.
kevinlacaille f177894
Updated search test and added tests for create search and update search.
kevinlacaille ababdab
Added a few more tests, some work and some dont
kevinlacaille a8d5d49
Added download snippets.
kevinlacaille d8e7046
Renamed file for testing.
kevinlacaille 3e769b0
Removed unused pytest fixture.
kevinlacaille 307cd99
Added a bunch of orders snippets with clever tricks to get order ids.…
kevinlacaille a06abfc
Remove downloaded files.
kevinlacaille a0eb085
Download asset added.
kevinlacaille 92bc2eb
Added all passing Orders tests.
kevinlacaille e718771
Fixed all broken tests.
kevinlacaille cd0cae9
Fixed unused test
kevinlacaille cfbf44d
test
kevinlacaille 008bb55
linting
kevinlacaille 84b3ede
Have tests create their own search id; limit to 2 items.
kevinlacaille a38c0c1
Added succesful order to download. Temp. solution.
kevinlacaille File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Code snippets for each function in the SDK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
# Copyright 2023 Planet Labs PBC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
"""Example of creating and downloading multiple orders. | ||
|
||
This is an example of submitting two orders, waiting for them to complete, and | ||
downloading them. The orders each clip a set of images to a specific area of | ||
interest (AOI), so they cannot be combined into one order. | ||
|
||
[Planet Explorer](https://www.planet.com/explorer/) was used to define | ||
the AOIs and get the image ids. | ||
""" | ||
from pathlib import Path | ||
import planet | ||
from planet import data_filter | ||
import json | ||
from datetime import datetime | ||
|
||
|
||
# search | ||
async def search(item_types, search_filter, name, sort, limit): | ||
'''Code snippet for search.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
async for item in client.search(item_types=item_types, | ||
search_filter=search_filter, | ||
name=name, | ||
sort=sort, | ||
limit=limit): | ||
return item | ||
|
||
|
||
# create_search | ||
async def create_search(item_types, search_filter, name): | ||
'''Code snippet for create_search.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
items = await client.create_search(item_types=item_types, | ||
search_filter=search_filter, | ||
name=name) | ||
return items | ||
|
||
|
||
# update_search | ||
async def update_search(search_id, item_types, search_filter, name): | ||
'''Code snippet for update_search.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
items = await client.update_search(search_id=search_id, | ||
item_types=item_types, | ||
search_filter=search_filter, | ||
name=name) | ||
return items | ||
|
||
|
||
# list_searches | ||
async def list_searches(sort, search_type, limit): | ||
'''Code snippet for list_searches.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
async for item in client.list_searches(sort=sort, | ||
search_type=search_type, | ||
limit=limit): | ||
return item | ||
|
||
|
||
# delete_search | ||
async def delete_search(search_id): | ||
'''Code snippet for delete_search.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
await client.delete_search(search_id) | ||
|
||
|
||
# get_search | ||
async def get_search(search_id): | ||
'''Code snippet for get_search.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
items = await client.get_search(search_id) | ||
return items | ||
|
||
|
||
# run_search | ||
async def run_search(search_id): | ||
'''Code snippet for run_search.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
async for item in client.run_search(search_id): | ||
return item | ||
|
||
|
||
# get_stats | ||
async def get_stats(item_types, search_filter, interval): | ||
'''Code snippet for get_stats.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
items = await client.get_stats(item_types=item_types, | ||
search_filter=search_filter, | ||
interval=interval) | ||
return items | ||
|
||
|
||
# list_item_assets | ||
async def list_item_assets(item_type_id, item_id): | ||
'''Code snippet for list_item_assets.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
assets = await client.list_item_assets(item_type_id, item_id) | ||
return assets | ||
|
||
|
||
# get_asset | ||
async def get_asset(item_type, item_id, asset_type): | ||
'''Code snippet for get_asset.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
asset = await client.get_asset(item_type, item_id, asset_type) | ||
return asset | ||
|
||
|
||
# activate_asset | ||
async def activate_asset(item_type, item_id, asset_type): | ||
'''Code snippet for activate_asset.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
asset = await client.get_asset(item_type, item_id, asset_type) | ||
await client.activate_asset(asset) | ||
|
||
|
||
# wait_asset | ||
async def wait_asset(item_type, item_id, asset_type): | ||
'''Code snippet for wait_asset.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
asset = await client.get_asset(item_type, item_id, asset_type) | ||
_ = await client.wait_asset(asset, callback=print) | ||
|
||
|
||
# download_asset w/o checksum | ||
async def download_asset_without_checksum(item_type, | ||
item_id, | ||
asset_type, | ||
filename, | ||
directory, | ||
overwrite): | ||
'''Code snippet for download_asset without a checksum.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
asset = await client.get_asset(item_type, item_id, asset_type) | ||
path = await client.download_asset(asset=asset, | ||
filename=filename, | ||
directory=Path(directory), | ||
overwrite=overwrite) | ||
return path | ||
|
||
|
||
# download_asset w/ checksum | ||
async def download_asset_with_checksum(item_type, | ||
item_id, | ||
asset_type, | ||
filename, | ||
directory, | ||
overwrite): | ||
'''Code snippet for download_asset with a checksum.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('data') | ||
asset = await client.get_asset(item_type, item_id, asset_type) | ||
path = await client.download_asset(asset=asset, | ||
filename=filename, | ||
directory=Path(directory), | ||
overwrite=overwrite) | ||
client.validate_checksum(asset, path) | ||
return path | ||
|
||
|
||
# Create search filters | ||
def create_search_filter(): | ||
'''Create a search filter.''' | ||
|
||
# Geometry you wish to clip to | ||
with open("aoi.geojson") as f: | ||
geom = json.loads(f.read()) | ||
|
||
# Build your filters with all types of requirements | ||
date_range_filter = data_filter.date_range_filter("acquired", | ||
gte=datetime(month=1, | ||
day=1, | ||
year=2017), | ||
lte=datetime(month=1, | ||
day=1, | ||
year=2018)) | ||
clear_percent_filter = data_filter.range_filter('clear_percent', 90) | ||
cloud_cover_filter = data_filter.range_filter('cloud_cover', None, 0.1) | ||
geom_filter = data_filter.geometry_filter(geom) | ||
asset_filter = data_filter.asset_filter( | ||
["basic_analytic_4b", "basic_udm2", "ortho_visual"]) | ||
permission_filter = data_filter.permission_filter() | ||
std_quality_filter = data_filter.std_quality_filter() | ||
|
||
# Search filter containing all filters listed above | ||
search_filter = data_filter.and_filter([ | ||
date_range_filter, | ||
clear_percent_filter, | ||
cloud_cover_filter, | ||
geom_filter, | ||
asset_filter, | ||
permission_filter, | ||
std_quality_filter | ||
]) | ||
|
||
return search_filter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
# Copyright 2023 Planet Labs PBC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
"""Example of creating and downloading multiple orders. | ||
|
||
This is an example of submitting two orders, waiting for them to complete, and | ||
downloading them. The orders each clip a set of images to a specific area of | ||
interest (AOI), so they cannot be combined into one order. | ||
|
||
[Planet Explorer](https://www.planet.com/explorer/) was used to define | ||
the AOIs and get the image ids. | ||
""" | ||
import json | ||
from pathlib import Path | ||
import planet | ||
|
||
|
||
# create_order() | ||
async def create_order(request): | ||
'''Code snippet for create_order.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
order = await client.create_order(request=request) | ||
return order | ||
|
||
|
||
# get_order() | ||
async def get_order(order_id): | ||
'''Code snippet for get_order.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
order = await client.get_order(order_id=order_id) | ||
return order | ||
|
||
|
||
# cancel_order() | ||
async def cancel_order(order_id): | ||
'''Code snippet for cancel_order.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
json_resp = await client.cancel_order(order_id=order_id) | ||
return json.dumps(json_resp) | ||
|
||
|
||
# cancel_orders() | ||
async def cancel_orders(order_id1, order_id2): | ||
'''Code snippet for cancel_order.''' | ||
order_ids = [order_id1, order_id2] | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
json_resp = await client.cancel_order(order_ids=order_ids) | ||
return json.dumps(json_resp) | ||
|
||
|
||
# aggregated_order_stats() | ||
async def aggregated_order_stats(): | ||
'''Code snippet for aggregated_order_stats.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
json_resp = await client.aggregated_order_stats() | ||
return json.dumps(json_resp) | ||
|
||
|
||
# download_asset() | ||
async def download_asset(dl_url, directory): | ||
'''Code snippet for download_asset.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
filename = await client.download_asset(location=dl_url, | ||
directory=directory) | ||
dl_path = Path(directory, filename) | ||
return dl_path | ||
|
||
|
||
# download_order() w/o checksum | ||
async def download_order_without_checksum(order_id, directory): | ||
'''Code snippet for download_order without checksum.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
filenames = await client.download_order(order_id, directory=directory) | ||
dl_path = Path(directory, filenames) | ||
return dl_path | ||
|
||
|
||
# download_order() w checksum | ||
async def download_order_with_checksum(order_id, directory): | ||
'''Code snippet for download_order with checksum.''' | ||
# Options: 'MD5' or 'SHA256' | ||
checksum = 'MD5' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
filenames = await client.download_order(order_id=order_id, | ||
directory=directory) | ||
client.validate_checksum(Path(directory, order_id), checksum) | ||
dl_path = Path(directory, filenames) | ||
return dl_path | ||
|
||
|
||
# wait() | ||
async def wait(order_id): | ||
'''Code snippet for wait.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
_ = await client.wait(order_id=order_id, callback=print) | ||
|
||
|
||
# list_orders() | ||
async def list_orders(): | ||
'''Code snippet for list_orders.''' | ||
async with planet.Session() as sess: | ||
client = sess.client('orders') | ||
async for order in client.list_orders(): | ||
return order | ||
|
||
|
||
def create_request(): | ||
'''Create an order request.''' | ||
|
||
# The Orders API will be asked to mask, or clip, results to | ||
# this area of interest. | ||
aoi = { | ||
"type": | ||
"Polygon", | ||
"coordinates": [[[-91.198465, 42.893071], [-91.121931, 42.893071], | ||
[-91.121931, 42.946205], [-91.198465, 42.946205], | ||
[-91.198465, 42.893071]]] | ||
} | ||
|
||
# In practice, you will use a Data API search to find items, but | ||
# for this example take them as given. | ||
items = ['20200925_161029_69_2223', '20200925_161027_48_2223'] | ||
|
||
order = planet.order_request.build_request( | ||
name='iowa_order', | ||
products=[ | ||
planet.order_request.product(item_ids=items, | ||
product_bundle='analytic_udm2', | ||
item_type='PSScene') | ||
], | ||
tools=[planet.order_request.clip_tool(aoi=aoi)]) | ||
|
||
return order | ||
|
||
|
||
async def order_example(): | ||
# Create an order request | ||
request = create_request() | ||
|
||
# Create order | ||
order = await create_order(request) | ||
|
||
# Get order ID | ||
order_id = order['id'] | ||
|
||
# Wait for download to be ready | ||
await wait(order_id) | ||
|
||
# Download order | ||
_ = await download_order_with_checksum(order_id, './') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.