|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Item Clipping and Download\n", |
| 8 | + "\n", |
| 9 | + "This example clips, downloads and unzips a given scene to the AOI provided, returning only the image data contained in with the AOI. To so this, we use the [\"Clips\" API](https://www.planet.com/docs/reference/clips-api/).\n", |
| 10 | + "\n", |
| 11 | + "In this example, we are clipping a scene to contain only Golden Gate Park in San Francisco." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "markdown", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "### Input Parameters " |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "metadata": { |
| 25 | + "collapsed": true |
| 26 | + }, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "from os import environ\n", |
| 30 | + "\n", |
| 31 | + "# Set API key (this should to be an environment variable)\n", |
| 32 | + "api_key = environ['PL_API_KEY']\n", |
| 33 | + "\n", |
| 34 | + "# Sent Scene ID\n", |
| 35 | + "scene_id = '20170517_192201_1056516_RapidEye-2'\n", |
| 36 | + "\n", |
| 37 | + "# Set Item Type\n", |
| 38 | + "item_type = 'REOrthoTile'\n", |
| 39 | + "\n", |
| 40 | + "# Set Asset Type\n", |
| 41 | + "asset_type = 'visual'\n", |
| 42 | + "\n", |
| 43 | + "# Area Of Interest (clip geometry) in GeoJSON format\n", |
| 44 | + "aoi_json = '''{\n", |
| 45 | + " \"type\": \"Polygon\",\n", |
| 46 | + " \"coordinates\": [\n", |
| 47 | + " [\n", |
| 48 | + " [\n", |
| 49 | + " -122.51103401184083,\n", |
| 50 | + " 37.771596736802074\n", |
| 51 | + " ],\n", |
| 52 | + " [\n", |
| 53 | + " -122.51060485839844,\n", |
| 54 | + " 37.763997637045456\n", |
| 55 | + " ],\n", |
| 56 | + " [\n", |
| 57 | + " -122.45902061462401,\n", |
| 58 | + " 37.76603318676243\n", |
| 59 | + " ],\n", |
| 60 | + " [\n", |
| 61 | + " -122.45773315429689,\n", |
| 62 | + " 37.7654903789825\n", |
| 63 | + " ],\n", |
| 64 | + " [\n", |
| 65 | + " -122.45275497436523,\n", |
| 66 | + " 37.76637243960179\n", |
| 67 | + " ],\n", |
| 68 | + " [\n", |
| 69 | + " -122.45455741882324,\n", |
| 70 | + " 37.775124624817906\n", |
| 71 | + " ],\n", |
| 72 | + " [\n", |
| 73 | + " -122.46597290039062,\n", |
| 74 | + " 37.7738356083287\n", |
| 75 | + " ],\n", |
| 76 | + " [\n", |
| 77 | + " -122.51103401184083,\n", |
| 78 | + " 37.771596736802074\n", |
| 79 | + " ]\n", |
| 80 | + " ]\n", |
| 81 | + " ]\n", |
| 82 | + " }'''" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "markdown", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + "### Clip scenes to AOI, poll status, and download when complete" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "metadata": { |
| 96 | + "collapsed": true |
| 97 | + }, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "import json\n", |
| 101 | + "import requests\n", |
| 102 | + "import time\n", |
| 103 | + "\n", |
| 104 | + "# Construct clip API payload\n", |
| 105 | + "clip_payload = {\n", |
| 106 | + " 'aoi': json.loads(aoi_json),\n", |
| 107 | + " 'targets': [\n", |
| 108 | + " {\n", |
| 109 | + " 'item_id': scene_id,\n", |
| 110 | + " 'item_type': item_type,\n", |
| 111 | + " 'asset_type': asset_type\n", |
| 112 | + " }\n", |
| 113 | + " ]\n", |
| 114 | + "}\n", |
| 115 | + "\n", |
| 116 | + "# Request clip of scene (This will take some time to complete)\n", |
| 117 | + "request = requests.post('https://api.planet.com/compute/ops/clips/v1', auth=(api_key, ''), json=clip_payload)\n", |
| 118 | + "clip_url = request.json()['_links']['_self']\n", |
| 119 | + "\n", |
| 120 | + "# Poll API to monitor clip status. Once finished, download and upzip the scene\n", |
| 121 | + "clip_succeeded = False\n", |
| 122 | + "while not clip_succeeded:\n", |
| 123 | + "\n", |
| 124 | + " # Poll API\n", |
| 125 | + " check_state_request = requests.get(clip_url, auth=(api_key, ''))\n", |
| 126 | + " \n", |
| 127 | + " # If clipping process succeeded , we are done\n", |
| 128 | + " if check_state_request.json()['state'] == 'succeeded':\n", |
| 129 | + " clip_download_url = check_state_request.json()['_links']['results'][0]\n", |
| 130 | + " clip_succeeded = True\n", |
| 131 | + " print(\"Clip of scene succeeded and is ready to download\") \n", |
| 132 | + " \n", |
| 133 | + " # Still activating. Wait 1 second and check again.\n", |
| 134 | + " else:\n", |
| 135 | + " print(\"...Still waiting for clipping to complete...\")\n", |
| 136 | + " time.sleep(1)" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "metadata": {}, |
| 142 | + "source": [ |
| 143 | + "### Download and upzip the clip\n", |
| 144 | + "Once complete, look in the output directory to see your clipped tif file.\n", |
| 145 | + "\n", |
| 146 | + "NOTE: Clipped scene will only be available for 5 minutes." |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": null, |
| 152 | + "metadata": { |
| 153 | + "collapsed": true |
| 154 | + }, |
| 155 | + "outputs": [], |
| 156 | + "source": [ |
| 157 | + "import os\n", |
| 158 | + "from tqdm import tqdm\n", |
| 159 | + "import zipfile\n", |
| 160 | + "\n", |
| 161 | + "# Download clip\n", |
| 162 | + "response = requests.get(clip_download_url, stream=True)\n", |
| 163 | + "with open('output/' + scene_id + '.zip', \"wb\") as handle:\n", |
| 164 | + " for data in tqdm(response.iter_content()):\n", |
| 165 | + " handle.write(data)\n", |
| 166 | + "\n", |
| 167 | + "# Unzip file\n", |
| 168 | + "ziped_item = zipfile.ZipFile('output/' + scene_id + '.zip')\n", |
| 169 | + "ziped_item.extractall('output/' + scene_id) \n", |
| 170 | + " \n", |
| 171 | + "# Delete zip file\n", |
| 172 | + "os.remove('output/' + scene_id + '.zip')\n", |
| 173 | + "print('Downloaded clips located in: output/')" |
| 174 | + ] |
| 175 | + } |
| 176 | + ], |
| 177 | + "metadata": { |
| 178 | + "kernelspec": { |
| 179 | + "display_name": "Python 3", |
| 180 | + "language": "python", |
| 181 | + "name": "python3" |
| 182 | + }, |
| 183 | + "language_info": { |
| 184 | + "codemirror_mode": { |
| 185 | + "name": "ipython", |
| 186 | + "version": 3 |
| 187 | + }, |
| 188 | + "file_extension": ".py", |
| 189 | + "mimetype": "text/x-python", |
| 190 | + "name": "python", |
| 191 | + "nbconvert_exporter": "python", |
| 192 | + "pygments_lexer": "ipython3", |
| 193 | + "version": "3.6.1" |
| 194 | + } |
| 195 | + }, |
| 196 | + "nbformat": 4, |
| 197 | + "nbformat_minor": 1 |
| 198 | +} |
0 commit comments