Skip to content

Commit 6147eef

Browse files
author
Sara Safavi
committed
add interactive rasterio, matplotlib tutorials
1 parent 29c115d commit 6147eef

File tree

4 files changed

+215
-130
lines changed

4 files changed

+215
-130
lines changed

jupyter-notebooks/inspecting-sat-imagery/Inspecting Satellite Imagery.ipynb renamed to jupyter-notebooks/getting-to-know-sat-imagery/Inspecting Satellite Imagery.ipynb

Lines changed: 89 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
55
"metadata": {},
66
"source": [
77
"# Inspecting Satellite Imagery using Rasterio\n",
8-
"## A first look at analyzing satellite data with Python\n",
8+
"## A first look at satellite data with Python\n",
99
"\n",
10-
"At this point, you've explored different ways of searching for, filtering, and downloading satellite imagery. Now let's use one of these acquired datasets and dig into it a bit with Python.\n",
10+
"At this point, you will have learned different ways of searching for, filtering, and downloading satellite imagery. Now let's use one of these acquired datasets and dig into it a bit with Python.\n",
1111
"\n",
12-
"Here we're going to use a Python library called `rasterio`: you may be familiar with it already, or perhaps with the related C library, `GDAL`."
12+
"Here we're going to use a Python library called `rasterio`: you may be familiar with it already, or perhaps with the related C library, `GDAL`. If you've used `numpy` before, working with `rasterio` will feel very familiar."
1313
]
1414
},
1515
{
1616
"cell_type": "code",
17-
"execution_count": 50,
17+
"execution_count": null,
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
2121
"import rasterio\n",
22-
"satdat = rasterio.open(\"example.tif\")"
22+
"\n",
23+
"# feel free to replace 'example' with your own GeoTIFF:\n",
24+
"# note that this Notebook will assume we're working with PlanetScope 4-band imagery\n",
25+
"image_file = \"example.tif\"\n",
26+
"\n",
27+
"satdat = rasterio.open(image_file)"
2328
]
2429
},
2530
{
@@ -32,40 +37,22 @@
3237
},
3338
{
3439
"cell_type": "code",
35-
"execution_count": 51,
40+
"execution_count": null,
3641
"metadata": {},
37-
"outputs": [
38-
{
39-
"data": {
40-
"text/plain": [
41-
"BoundingBox(left=540759.0, bottom=3754401.0, right=568047.0, top=3767985.0)"
42-
]
43-
},
44-
"execution_count": 51,
45-
"metadata": {},
46-
"output_type": "execute_result"
47-
}
48-
],
42+
"outputs": [],
4943
"source": [
5044
"# Get the bounding box of this GeoTIFF\n",
45+
"\n",
5146
"satdat.bounds"
5247
]
5348
},
5449
{
5550
"cell_type": "code",
56-
"execution_count": 63,
51+
"execution_count": null,
5752
"metadata": {},
58-
"outputs": [
59-
{
60-
"name": "stdout",
61-
"output_type": "stream",
62-
"text": [
63-
"Width: 27288.0, Height: 13584.0\n"
64-
]
65-
}
66-
],
53+
"outputs": [],
6754
"source": [
68-
"# Get dimensions, in map units (here, that's meters)\n",
55+
"# Get dimensions, in map units (using the example GeoTIFF, that's meters)\n",
6956
"\n",
7057
"width = satdat.bounds.right - satdat.bounds.left\n",
7158
"height = satdat.bounds.top - satdat.bounds.bottom\n",
@@ -75,40 +62,22 @@
7562
},
7663
{
7764
"cell_type": "code",
78-
"execution_count": 64,
65+
"execution_count": null,
7966
"metadata": {},
80-
"outputs": [
81-
{
82-
"name": "stdout",
83-
"output_type": "stream",
84-
"text": [
85-
"Width: 9096, Height: 4528\n"
86-
]
87-
}
88-
],
67+
"outputs": [],
8968
"source": [
9069
"# Get dimensions, in pixels\n",
70+
"\n",
9171
"px_width = satdat.width\n",
9272
"px_height = satdat.height\n",
9373
"print(\"Width: {}, Height: {}\".format(px_width, px_height))"
9474
]
9575
},
9676
{
9777
"cell_type": "code",
98-
"execution_count": 65,
78+
"execution_count": null,
9979
"metadata": {},
100-
"outputs": [
101-
{
102-
"data": {
103-
"text/plain": [
104-
"(3.0, 3.0)"
105-
]
106-
},
107-
"execution_count": 65,
108-
"metadata": {},
109-
"output_type": "execute_result"
110-
}
111-
],
80+
"outputs": [],
11281
"source": [
11382
"# How many meters to a pixel?\n",
11483
"\n",
@@ -120,70 +89,51 @@
12089
},
12190
{
12291
"cell_type": "code",
123-
"execution_count": 66,
92+
"execution_count": null,
12493
"metadata": {},
125-
"outputs": [
126-
{
127-
"data": {
128-
"text/plain": [
129-
"CRS({'init': 'epsg:32611'})"
130-
]
131-
},
132-
"execution_count": 66,
133-
"metadata": {},
134-
"output_type": "execute_result"
135-
}
136-
],
94+
"outputs": [],
13795
"source": [
13896
"# Get coordinate reference system\n",
97+
"\n",
13998
"satdat.crs"
14099
]
141100
},
142101
{
143102
"cell_type": "code",
144-
"execution_count": 59,
103+
"execution_count": null,
145104
"metadata": {},
146-
"outputs": [
147-
{
148-
"name": "stdout",
149-
"output_type": "stream",
150-
"text": [
151-
"Top left corner coordinates: (540759.0, 3767985.0)\n",
152-
"Bottom right corner coordinates: (568047.0, 3754401.0)\n"
153-
]
154-
},
155-
{
156-
"name": "stderr",
157-
"output_type": "stream",
158-
"text": [
159-
"/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2910: FutureWarning: The value of this property will change in version 1.0. Please see https://github.com/mapbox/rasterio/issues/86 for details.\n",
160-
" exec(code_obj, self.user_global_ns, self.user_ns)\n"
161-
]
162-
}
163-
],
105+
"outputs": [],
164106
"source": [
165107
"# Get coordinates of top-left & bottom right corners\n",
166-
"# NOTE: how to do this depends on your Rasterio version:\n",
167-
"# the below example may generate a FutureWarning, which is safe to ignore here\n",
168108
"\n",
169-
"try:\n",
170-
" topleft = satdat.transform * (0, 0)\n",
171-
" botright = satdat.transform * (width, height)\n",
172-
" \n",
173-
"except TypeError:\n",
174-
" topleft = satdat.affine * (0, 0)\n",
175-
" botright = satdat.affine * (width, height)\n",
109+
"# NOTE: how to do this depends on your Rasterio version --\n",
110+
"# if you're running against a pre-1.0 release use satdat.affine instead\n",
111+
"\n",
112+
"topleft = satdat.transform * (0, 0)\n",
113+
"botright = satdat.transform * (width, height)\n",
176114
" \n",
177115
"print(\"Top left corner coordinates: {}\".format(topleft))\n",
178116
"print(\"Bottom right corner coordinates: {}\".format(botright))"
179117
]
180118
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": null,
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"# Another way of viewing most of the previous values:\n",
126+
"# Get the basic metadata of this GeoTIFF\n",
127+
"\n",
128+
"satdat.meta"
129+
]
130+
},
181131
{
182132
"cell_type": "markdown",
183133
"metadata": {},
184134
"source": [
185135
"## Bands\n",
186-
"So far, we haven't done too much raster-specific work yet. Since we know we're inspecting a multispectral satellite image, let's see what we can learn about its bands."
136+
"So far, we haven't done too much geospatial-raster-specific work yet. Since we know we're inspecting a multispectral satellite image, let's see what we can learn about its bands."
187137
]
188138
},
189139
{
@@ -193,6 +143,7 @@
193143
"outputs": [],
194144
"source": [
195145
"# Get the number of bands by listing their indices\n",
146+
"\n",
196147
"satdat.indexes"
197148
]
198149
},
@@ -214,7 +165,21 @@
214165
"blue = satdat.read(1)\n",
215166
"green = satdat.read(2)\n",
216167
"red = satdat.read(3)\n",
217-
"nir = satdat.read(4)"
168+
"nir = satdat.read(4)\n",
169+
"\n",
170+
"# or:\n",
171+
"# blue, green, red, nir = satdat.read()"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"## Pixels\n",
179+
"\n",
180+
"In a raster dataset, each pixel has a value. Pixels are arranged in a grid, and pixels representing equivalent data have the same value:\n",
181+
"\n",
182+
"![pixels2.png](pixels2.png)"
218183
]
219184
},
220185
{
@@ -225,7 +190,7 @@
225190
"source": [
226191
"# bands are stored as numpy arrays\n",
227192
"\n",
228-
"type(blue)"
193+
"print(type(blue))"
229194
]
230195
},
231196
{
@@ -234,7 +199,9 @@
234199
"metadata": {},
235200
"outputs": [],
236201
"source": [
237-
"blue"
202+
"# how many dimensions would a raster have?\n",
203+
"\n",
204+
"print(blue.ndim)"
238205
]
239206
},
240207
{
@@ -243,56 +210,48 @@
243210
"metadata": {},
244211
"outputs": [],
245212
"source": [
246-
"# Output min & max pixel values in each band\n",
213+
"# take a look at the summarized array\n",
247214
"\n",
248-
"print(blue.min(), blue.max())\n",
249-
"print(green.min(), green.max())\n",
250-
"print(red.min(), red.max())\n",
251-
"print(nir.min(), nir.max())"
215+
"print(blue)"
252216
]
253217
},
254218
{
255-
"cell_type": "markdown",
219+
"cell_type": "code",
220+
"execution_count": null,
256221
"metadata": {},
222+
"outputs": [],
257223
"source": [
258-
"## Pixels"
224+
"# Output a min & max pixel value in each band\n",
225+
"\n",
226+
"# we'll need to use numpy directly for this\n",
227+
"import numpy\n",
228+
"\n",
229+
"print(numpy.amin(blue), numpy.amax(blue))\n",
230+
"print(numpy.amin(green), numpy.amax(green))\n",
231+
"print(numpy.amin(red), numpy.amax(red))\n",
232+
"print(numpy.amin(nir), numpy.amax(nir))"
259233
]
260234
},
261235
{
262236
"cell_type": "code",
263-
"execution_count": 61,
237+
"execution_count": null,
264238
"metadata": {},
265-
"outputs": [
266-
{
267-
"data": {
268-
"text/plain": [
269-
"6382"
270-
]
271-
},
272-
"execution_count": 61,
273-
"metadata": {},
274-
"output_type": "execute_result"
275-
}
276-
],
239+
"outputs": [],
277240
"source": [
278-
"# Let's grab the pixel 5km east and 5km south of the upper left corner\n",
241+
"# Let's grab the pixel 2km east and 2km south of the upper left corner\n",
279242
"\n",
280243
"# get the pixel \n",
281-
"px_x = satdat.bounds.left + 5000\n",
282-
"px_y = satdat.bounds.top - 5000\n",
244+
"px_x = satdat.bounds.left + 2000\n",
245+
"px_y = satdat.bounds.top - 2000\n",
283246
"\n",
284247
"row, col = satdat.index(px_x, px_y)\n",
285248
"\n",
286-
"# Now let's look at the value of Band1 (\"blue\") at this pixel\n",
287-
"blue[row, col]"
249+
"# Now let's look at the value of each band at this pixel\n",
250+
"print(\"Red: {}\".format(red[row, col]))\n",
251+
"print(\"Green: {}\".format(green[row, col]))\n",
252+
"print(\"Blue: {}\".format(blue[row, col]))\n",
253+
"print(\"NIR: {}\".format(nir[row, col]))"
288254
]
289-
},
290-
{
291-
"cell_type": "code",
292-
"execution_count": null,
293-
"metadata": {},
294-
"outputs": [],
295-
"source": []
296255
}
297256
],
298257
"metadata": {
@@ -311,7 +270,7 @@
311270
"name": "python",
312271
"nbconvert_exporter": "python",
313272
"pygments_lexer": "ipython3",
314-
"version": "3.6.2"
273+
"version": "3.6.3"
315274
}
316275
},
317276
"nbformat": 4,

0 commit comments

Comments
 (0)