Skip to content
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

trainings for the one to many program #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
302 changes: 302 additions & 0 deletions jupyter-notebooks/trainings/Basemaps/Basemap_streaming.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f94068e6-f0a2-40cf-ab96-bde308876141",
"metadata": {},
"source": [
"## Overview ##\n",
"---\n",
"In this notebook, you will learn how to order a [Planet Basemap](https://developers.planet.com/docs/data/visual-basemaps/) using your [Area of Interest](https://developers.planet.com/apis/orders/basemaps/#order-basemaps-by-area-of-interest-aoi) (AOI) and a [Quad ID](https://developers.planet.com/apis/orders/basemaps/#order-basemaps-by-quad-ids-and-deliver-to-cloud). We will place this order via Planet's [Orders API](https://developers.planet.com/apis/orders/) using our Planet Python [SDK](https://planet-sdk-for-python-v2.readthedocs.io/en/latest/python/sdk-guide/). \n",
"\n",
"1. Get a Basemap ID using either [Planet Explorer](https://developers.planet.com/docs/apps/explorer/), the [Basemap Viewer](https://www.planet.com/basemaps/#/mosaic/), or by API request. \n",
"2. Create a JSON order packet with order parameters.\n",
"3. Set up a session with the Planet SDK, place the order, and download it.\n",
"4. Repeat steps 1-3 using quad IDs. "
]
},
{
"cell_type": "markdown",
"id": "a89cdc6b-2857-49c3-81e2-af22cf92806b",
"metadata": {},
"source": [
"### Import Planet and Related Packages\n",
"\n",
"---\n",
"\n",
"Make sure you have Planet's Python SDK properly downloaded. You can find out more about this [here](https://developers.planet.com/docs/pythonclient/). Find your [API key](https://developers.planet.com/quickstart/apis/).\n",
"\n",
"Next set up a session by importing needed Python packages, pulling in your API Key, and make an initial request (to retrieve the Orders API parameters) to confirm a connection with the server."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fcfc778a-857f-43ce-ac1c-4a17fd657c08",
"metadata": {},
"outputs": [],
"source": [
"import planet\n",
"import os\n",
"import json\n",
"import requests\n",
"from requests.auth import HTTPBasicAuth"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5dc58e0c-ee2f-43e8-b8fc-9c06d225d8f5",
"metadata": {},
"outputs": [],
"source": [
"PL_API_KEY = os.environ.get('PL_API_KEY')\n",
"auth = planet.Auth.from_key(PL_API_KEY)"
]
},
{
"cell_type": "markdown",
"id": "16439817-7825-487c-a3a4-69b039fb53b1",
"metadata": {},
"source": [
"### Get your basemap names"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75c34420-be54-4760-9f32-20c2445ca2d7",
"metadata": {},
"outputs": [],
"source": [
"BASEMAP_API_URL = 'https://api.planet.com/basemaps/v1/mosaics'\n",
"auth = HTTPBasicAuth(PL_API_KEY, '')\n",
"basemapServiceResponse = requests.get(url=BASEMAP_API_URL, auth=auth)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c1611dd-3fd6-44ca-9e7d-14f1215ca77f",
"metadata": {},
"outputs": [],
"source": [
"basemaps = basemapServiceResponse.raise_for_status()\n",
"if basemapServiceResponse.status_code != 204:\n",
" basemaps = json.loads(basemapServiceResponse.text)\n",
"mosaics = basemaps['mosaics']\n",
"names = [feature['name'] for feature in mosaics]\n",
"print(names)"
]
},
{
"cell_type": "markdown",
"id": "82755c4f",
"metadata": {},
"source": [
"Iterate through all your available basemaps and extract all the global monthly from 2020."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b688d7d2-39f3-42fe-a824-ef8a9d9b9146",
"metadata": {},
"outputs": [],
"source": [
"global_monthly = []\n",
"cont = True\n",
"while cont:\n",
" for name in names:\n",
" #Here I am filtering for the global monthly basemap\n",
" if \"2020\" in name and \"global_monthly\" in name:\n",
" global_monthly.append(name)\n",
" next_url = basemaps['_links']['_next']\n",
" basemapServiceResponse = requests.get(url=next_url, auth=auth)\n",
" basemaps = json.loads(basemapServiceResponse.text)\n",
" mosaics = basemaps['mosaics']\n",
" names = [feature['name'] for feature in mosaics]\n",
" if \"_next\" not in basemaps[\"_links\"]:\n",
" cont = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b6bdf96-0aa7-44d2-8651-22b1b336fb2f",
"metadata": {},
"outputs": [],
"source": [
"global_monthly"
]
},
{
"cell_type": "markdown",
"id": "58d28178-2a25-4951-80fd-3a944dc9d4e8",
"metadata": {
"tags": []
},
"source": [
"### Create an order packet\n",
"---\n",
"Package up the details of your order in a [JSON object](https://developers.planet.com/apis/orders/basemaps/#example-order-query-json-block) and make a POST request, passing in the Orders URL, your JSON, your API key, and the content-type header. We are [delivering](https://developers.planet.com/apis/orders/delivery/) this order to a Google Cloud Storage bucket. You can see examples of using tools [here](https://developers.planet.com/apis/orders/tools/). Make sure to replace the mosaic name and coordinates with your specifications."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49288bd9-47e8-4865-bd5c-1c3c815a30e2",
"metadata": {},
"outputs": [],
"source": [
"BASEMAP_API_URL = 'https://api.planet.com/basemaps/v1/mosaics'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d90799e2-11da-4f52-a3b3-e80f15c28d0a",
"metadata": {},
"outputs": [],
"source": [
"order_params = {\n",
" \"name\": \"basemap order with geometry\",\n",
" \"source_type\": \"basemaps\",\n",
" \"order_type\": \"partial\",\n",
" \"products\": [\n",
" {\n",
" \"mosaic_name\": global_monthly[0],\n",
" \"geometry\": {\n",
" \"type\": \"Polygon\",\n",
" \"coordinates\": [\n",
" [\n",
" [4.607406, 52.353994],\n",
" [4.680005, 52.353994],\n",
" [4.680005, 52.395523],\n",
" [4.607406, 52.395523],\n",
" [4.607406, 52.353994]\n",
" ]\n",
" ]\n",
" }\n",
" }\n",
" ],\n",
" \"tools\": [{\"clip\": {}}]\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "b44869b6-0e7e-4fdc-8852-508ba2074bdc",
"metadata": {},
"source": [
"### Create a session with SDK and poll for success\n",
"\n",
"Here, we are creating an order using the SDK. Then we are waiting for the order to be delievered to our GCP storage bucket. Note that this can take some time."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1bf7890-6c51-470e-bdfc-5efb781414da",
"metadata": {},
"outputs": [],
"source": [
"async def create_and_deliver_order(order_params, client):\n",
" '''Create an order and wait for it to delivered\n",
"\n",
" Parameters:\n",
" order_params: An order request\n",
" client: An Order client object\n",
" '''\n",
" with planet.reporting.StateBar(state='creating') as reporter:\n",
" # Place an order to the Orders API\n",
" order = await client.create_order(order_params)\n",
" reporter.update(state='created', order_id=order['id'])\n",
" # Wait while the order is being completed\n",
" await client.wait(order['id'], callback=reporter.update_state,\n",
" max_attempts=0)\n",
" # if we get here that means the order completed. Yay! Download the files.\n",
" await client.download_order(order['id'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45c71902-c64a-4ad8-841d-4e4bb9c14915",
"metadata": {},
"outputs": [],
"source": [
"async with planet.Session() as ps:\n",
" # The Orders API client\n",
" client = ps.client('orders')\n",
" # Create the order and deliver it to GCP\n",
" await create_and_deliver_order(order_params, client)"
]
},
{
"cell_type": "markdown",
"id": "36e4700a-87fb-4dc5-882d-0939b656b880",
"metadata": {},
"source": [
"## Creating a streaming link for scenes\n",
"\n",
"This quick demo shows you how to use the tiles API to create WMTS links to stream into you GIS"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90326987-2890-4388-822a-3b0a7bb32fdb",
"metadata": {},
"outputs": [],
"source": [
"tile_url = 'https://tiles0.planet.com/data/v1/layers'\n",
"#Copy the list of scene IDs from the API dialog into the ids JSON\n",
"data = {\n",
" \"ids\": [\n",
" \"PSScene:20230813_184925_91_2475\"\n",
" ]\n",
"}\n",
"api_key = os.getenv('PL_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8425c5b9-935f-492f-b052-0814f9830901",
"metadata": {},
"outputs": [],
"source": [
"res = requests.post(tile_url, auth=(api_key, \"\"), data=data)\n",
"print(res)\n",
"print(res.json())\n",
"name = res.json()[\"name\"]\n",
"url = \"{}/wmts/{}?api_key={}\".format(tile_url,name, api_key)\n",
"\n",
"# This URL includes your API key, be careful where you share it!\n",
"print(url)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading