Skip to content

Commit 6d392c8

Browse files
committed
Updated rasterio syntax and other small syntax and comment changes.
1 parent 6961ecc commit 6d392c8

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

jupyter-notebooks/coastal-erosion-example/1_rasterio_firstlook.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"metadata": {},
120120
"outputs": [],
121121
"source": [
122+
"# Open our image\n",
122123
"satdat = rasterio.open(image_file)\n",
123124
"\n",
124125
"# satdat is our open dataset object\n",
@@ -170,8 +171,9 @@
170171
"metadata": {},
171172
"outputs": [],
172173
"source": [
173-
"# PlanetScope 4-band band order: BGRN\n",
174+
"# PlanetScope 3-band band order: BGR\n",
174175
"\n",
176+
"# Extract the rasterbands\n",
175177
"with rasterio.open(image_file) as src:\n",
176178
" blue = src.read(1)\n",
177179
" green = src.read(2)\n",

jupyter-notebooks/coastal-erosion-example/2_rasterbands.ipynb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"metadata": {},
111111
"outputs": [],
112112
"source": [
113+
"# Open our image\n",
113114
"satdat = rasterio.open(image_file)\n",
114115
"\n",
115116
"# satdat is our open dataset object\n",
@@ -130,8 +131,7 @@
130131
"outputs": [],
131132
"source": [
132133
"# Get the image's coordinate reference system (WGS84 / UTM 15N)\n",
133-
"\n",
134-
"satdat.crs"
134+
"print(satdat.crs)"
135135
]
136136
},
137137
{
@@ -148,7 +148,6 @@
148148
"outputs": [],
149149
"source": [
150150
"# Minimum bounding box in projected units (meters), in our zone\n",
151-
"\n",
152151
"print(satdat.bounds)"
153152
]
154153
},
@@ -159,7 +158,6 @@
159158
"outputs": [],
160159
"source": [
161160
"# Get dimensions, in projected units (using the example GeoTIFF, that's meters)\n",
162-
"\n",
163161
"width_in_projected_units = satdat.bounds.right - satdat.bounds.left\n",
164162
"height_in_projected_units = satdat.bounds.top - satdat.bounds.bottom\n",
165163
"\n",
@@ -173,7 +171,6 @@
173171
"outputs": [],
174172
"source": [
175173
"# Number of rows and columns (pixels)\n",
176-
"\n",
177174
"print(\"Rows: {}, Columns: {}\".format(satdat.height, satdat.width))"
178175
]
179176
},
@@ -183,7 +180,8 @@
183180
"metadata": {},
184181
"outputs": [],
185182
"source": [
186-
"# This dataset's projection uses meters as projected units. What are the dimensions of a single pixel in meters?\n",
183+
"# This dataset's projection uses meters as projected units. \n",
184+
"# What are the dimensions of a single pixel in meters?\n",
187185
"\n",
188186
"xres = width_in_projected_units / satdat.width\n",
189187
"yres = height_in_projected_units / satdat.height\n",
@@ -223,8 +221,8 @@
223221
"metadata": {},
224222
"outputs": [],
225223
"source": [
226-
"# All of the metadata required to create an image of the same dimensions, datatype, format, etc. is stored in\n",
227-
"# the dataset's profile:\n",
224+
"# All of the metadata required to create an image of the same dimensions, \n",
225+
"# datatype, format, etc. is stored in the dataset's profile:\n",
228226
"\n",
229227
"satdat.profile"
230228
]
@@ -263,7 +261,6 @@
263261
"outputs": [],
264262
"source": [
265263
"# PlanetScope 3-band band order: BGR\n",
266-
"\n",
267264
"with rasterio.open(image_file) as src:\n",
268265
" blue = src.read(1)\n",
269266
" green = src.read(2)\n",
@@ -277,7 +274,6 @@
277274
"outputs": [],
278275
"source": [
279276
"# To construct a visual image, we will need the red, green, and blue bands\n",
280-
"\n",
281277
"visual_image = np.dstack((blue, green, red))"
282278
]
283279
},
@@ -295,7 +291,8 @@
295291
"outputs": [],
296292
"source": [
297293
"plt.imshow(visual_image)\n",
298-
"plt.title(\"Visual Image\")"
294+
"plt.title(\"Visual Image\")\n",
295+
"plt.show()"
299296
]
300297
},
301298
{

jupyter-notebooks/coastal-erosion-example/3_Compute_NDWI.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@
202202
"fig = plt.imshow(ndwi)\n",
203203
"fig.set_cmap('Blues')\n",
204204
"plt.title(\"NDWI\")\n",
205-
"plt.colorbar()"
205+
"plt.colorbar()\n",
206+
"plt.show()"
206207
]
207208
},
208209
{

jupyter-notebooks/coastal-erosion-example/4_masks_and_filters.ipynb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@
115115
"metadata": {},
116116
"outputs": [],
117117
"source": [
118-
"blue, green, red, nir = satdat.read()"
118+
"# Extract rasterbands\n",
119+
"with rasterio.open(image_file) as src:\n",
120+
" blue = src.read(1)\n",
121+
" green = src.read(2)\n",
122+
" red = src.read(3)\n",
123+
" nir = src.read(4)"
119124
]
120125
},
121126
{
@@ -134,6 +139,7 @@
134139
"metadata": {},
135140
"outputs": [],
136141
"source": [
142+
"# Generate the water and land masks\n",
137143
"water_mask = np.full(ndwi.shape, np.nan)\n",
138144
"land_mask = np.full(ndwi.shape, np.nan)\n",
139145
"\n",
@@ -167,7 +173,8 @@
167173
"\n",
168174
"plt.imshow(water_mask)\n",
169175
"plt.title(\"Houston water mask\")\n",
170-
"plt.colorbar()"
176+
"plt.colorbar()\n",
177+
"plt.show()"
171178
]
172179
},
173180
{
@@ -180,7 +187,8 @@
180187
"\n",
181188
"plt.imshow(land_mask)\n",
182189
"plt.title(\"Houston land mask\")\n",
183-
"plt.colorbar()"
190+
"plt.colorbar()\n",
191+
"plt.show()"
184192
]
185193
},
186194
{
@@ -232,8 +240,7 @@
232240
"mask_closed_opened = cv2.morphologyEx(mask_closed, cv2.MORPH_OPEN, opening_kernel)\n",
233241
"\n",
234242
"# Ensure the clipped areas remain clipped\n",
235-
"mask_closed_opened[mask_closed_opened == 0] = np.nan\n",
236-
"\n"
243+
"mask_closed_opened[mask_closed_opened == 0] = np.nan"
237244
]
238245
},
239246
{
@@ -243,7 +250,8 @@
243250
"outputs": [],
244251
"source": [
245252
"plt.imshow(mask_closed_opened)\n",
246-
"plt.title(\"Houston filtered water mask\")"
253+
"plt.title(\"Houston filtered water mask\")\n",
254+
"plt.show()"
247255
]
248256
},
249257
{

jupyter-notebooks/coastal-erosion-example/5_plotting_a_histogram.ipynb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,13 @@
9696
"metadata": {},
9797
"outputs": [],
9898
"source": [
99-
"# Load the 4 bands into 2d arrays - recall that we previously learned PlanetScope band order is BGRN.\n",
100-
"blue, green, red, nir = satdat.read()"
99+
"# Load the 4 bands into 2d arrays\n",
100+
"# recall that we previously learned 4-band PlanetScope band order is BGRN.\n",
101+
"with rasterio.open(image_file) as src:\n",
102+
" blue = src.read(1)\n",
103+
" green = src.read(2)\n",
104+
" red = src.read(3)\n",
105+
" nir = src.read(4)"
101106
]
102107
},
103108
{
@@ -108,10 +113,10 @@
108113
"outputs": [],
109114
"source": [
110115
"# Define a new figure\n",
111-
"fig2 = plt.figure(figsize=(20,10))\n",
116+
"fig = plt.figure(figsize=(20,10))\n",
112117
"\n",
113118
"# Give this new figure a subplot, which will contain the histogram itself\n",
114-
"ax = fig2.add_subplot(111)\n",
119+
"ax = fig.add_subplot(111)\n",
115120
"\n",
116121
"# Add a title & (x,y) labels to the plot\n",
117122
"plt.title(\"Histogram Example\", fontsize=18, fontweight='bold')\n",
@@ -133,7 +138,7 @@
133138
"ax.hist(x,bins,color=color)\n",
134139
"\n",
135140
"# Save the generated figure to an external image file\n",
136-
"fig2.savefig(\"histogram.png\", dpi=200, bbox_inches='tight', pad_inches=0.7)\n",
141+
"fig.savefig(\"histogram.png\", dpi=200, bbox_inches='tight', pad_inches=0.7)\n",
137142
"\n",
138143
"# Finally - let's take a look!\n",
139144
"plt.show()"

0 commit comments

Comments
 (0)