Skip to content

Commit d17e01c

Browse files
author
Nate Weisiger
committed
Added clip and ship notebook
1 parent 1d0bac4 commit d17e01c

19 files changed

+504
-327
lines changed

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ RUN ln -s $CONDA_DIR/envs/python2/bin/pip $CONDA_DIR/bin/pip2.7 && \
7373
pygdal==2.1.3.3 \
7474
geojsonio
7575

76-
# Add pip3.5 shortcut and install Python3 packages
77-
RUN pip3.5 install --upgrade pip && \
78-
pip3.5 install matplotlib && \
79-
pip3.5 install scikit-image && \
80-
pip3.5 install \
76+
# Add pip shortcut and install Python3 packages
77+
RUN pip install --upgrade pip && \
78+
pip install matplotlib && \
79+
pip install scikit-image && \
80+
pip install \
8181
pyproj \
8282
ipywidgets \
8383
ipyleaflet \

jupyter-notebooks/data-api-tutorials/Cairo.geojson

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"type": "Polygon",
3+
"coordinates": [
4+
[
5+
[
6+
-122.47455596923828,
7+
37.810326435534755
8+
],
9+
[
10+
-122.49172210693358,
11+
37.795406713958236
12+
],
13+
[
14+
-122.52056121826172,
15+
37.784282779035216
16+
],
17+
[
18+
-122.51953124999999,
19+
37.6971326434885
20+
],
21+
[
22+
-122.38941192626953,
23+
37.69441603823106
24+
],
25+
[
26+
-122.38872528076173,
27+
37.705010235842614
28+
],
29+
[
30+
-122.36228942871092,
31+
37.70935613533687
32+
],
33+
[
34+
-122.34992980957031,
35+
37.727280276860036
36+
],
37+
[
38+
-122.37773895263672,
39+
37.76230130281876
40+
],
41+
[
42+
-122.38494873046875,
43+
37.794592824285104
44+
],
45+
[
46+
-122.40554809570311,
47+
37.813310018173155
48+
],
49+
[
50+
-122.46150970458983,
51+
37.805715207044685
52+
],
53+
[
54+
-122.47455596923828,
55+
37.810326435534755
56+
]
57+
]
58+
]
59+
}

0 commit comments

Comments
 (0)