Skip to content

Commit

Permalink
landsat.tif to reflectance
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldorman committed Jan 15, 2024
1 parent 94b1f2c commit 950ad77
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 4,648 deletions.
11 changes: 5 additions & 6 deletions 03-spatial-operations.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ $$ {#eq-ndvi}
, where $NIR$ is the near-infrared band and $Red$ is the red band.
Let's calculate NDVI for the multispectral Landsat satellite file (`landsat.tif`) of the Zion National Park.
The file `landsat.tif` contains surface reflectance values (range `0`-`1`) in the blue, green, red and near-infrared (NIR) bands.
We start by reading the file and extracting the NIR and red bands, which are the fourth and third bands, respectively.
Next, we apply the formula to calculate the NDVI values.
Expand All @@ -1012,13 +1013,11 @@ red = landsat[2]
ndvi = (nir-red)/(nir+red)
```
We also convert values \>`1` to "No Data".
<!-- We also convert values \>`1` to "No Data". -->
<!-- ndvi[ndvi>1] = np.nan -->
<!-- jn: explain why -->
<!-- md: I guess because the 'landsat' dataset has (uncorrected) brightness values, so the NDVI turns out to be beyond the valid range? I suggest adding a box to explain that, but I'm not sure what are the values exactly. this dataset is from 'geocomr' so maybe Jakub/Robin, can you please help with that? -->
```{python}
ndvi[ndvi>1] = np.nan
```
<!-- md: Solved now after discussion on GitHub and help from Jakub https://github.com/geocompx/geocompy/issues/218 -->
When plotting an RGB image using the `rasterio.plot.show` function, the function assumes that values are in the range `[0,1]` for floats, or `[0,255]` for integers (otherwise clipped) and the order of bands is RGB.
To "prepare" the multi-band raster for `rasterio.plot.show`, we therefore reverse the order of the first three bands (to go from B-G-R-NIR to R-G-B), using the `[:3]` slice to select the first three bands and then the `[::-1]` slice to reverse the bands order, and divide by the raster maximum to set the maximum value to `1`.
Expand All @@ -1033,7 +1032,7 @@ The default is to start from the beginning, go to the end, and use steps of `1`.
Otherwise, `start` is inclusive and `end` is exclusive, whereas negative `step` values imply going backwards starting from the end.
Also, always keep in mind that Python indices start from `0`.
When subsetting two- or three-dimensional objects, indices for each dimension are separated by commas, where either index can be set to `:` meaning "all values".
The last dimensions can also be omitted implying `:`, e.g., to subset the first three bands from a three-dimensional array `a` we can use either `a[:3,:,:]` or `a[:3]`
The last dimensions can also be omitted implying `:`, e.g., to subset the first three bands from a three-dimensional array `a` we can use either `a[:3,:,:]` or `a[:3]`.
In the above example:
Expand Down
Binary file added __pycache__/book_options.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/book_options_pdf.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/map_to_png.cpython-310.pyc
Binary file not shown.
Binary file modified data/landsat.tif
Binary file not shown.
29 changes: 29 additions & 0 deletions geckodriver.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
1705312590934 geckodriver INFO Listening on 127.0.0.1:55633
1705312591726 mozrunner::runner INFO Running command: "/usr/bin/firefox" "--marionette" "--headless" "--remote-debugging-port" "60745" "-no-remote" "-profile" "/tmp/rust_mozprofileHiRqgD"
*** You are running in headless mode.
Gtk-Message: 11:56:31.944: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it.
[GFX1-]: RenderCompositorSWGL failed mapping default framebuffer, no dt
1705312722835 geckodriver INFO Listening on 127.0.0.1:51571
1705312723585 mozrunner::runner INFO Running command: "/usr/bin/firefox" "--marionette" "--headless" "--remote-debugging-port" "45313" "-no-remote" "-profile" "/tmp/rust_mozprofilehJ2Leb"
*** You are running in headless mode.
Gtk-Message: 11:58:43.918: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it.
[GFX1-]: RenderCompositorSWGL failed mapping default framebuffer, no dt
1705312854156 geckodriver INFO Listening on 127.0.0.1:49011
1705312854970 mozrunner::runner INFO Running command: "/usr/bin/firefox" "--marionette" "--headless" "--remote-debugging-port" "59281" "-no-remote" "-profile" "/tmp/rust_mozprofilenFVfLB"
*** You are running in headless mode.
Gtk-Message: 12:00:55.199: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it.
[GFX1-]: RenderCompositorSWGL failed mapping default framebuffer, no dt
1705312985526 geckodriver INFO Listening on 127.0.0.1:49377
1705312986366 mozrunner::runner INFO Running command: "/usr/bin/firefox" "--marionette" "--headless" "--remote-debugging-port" "43449" "-no-remote" "-profile" "/tmp/rust_mozprofilegpWGdP"
*** You are running in headless mode.
Gtk-Message: 12:03:06.707: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it.
[GFX1-]: RenderCompositorSWGL failed mapping default framebuffer, no dt
1705313099038 geckodriver INFO Listening on 127.0.0.1:58039
1705313099973 geckodriver INFO Listening on 127.0.0.1:42423
1705313100794 geckodriver INFO Listening on 127.0.0.1:54139
1705313101626 geckodriver INFO Listening on 127.0.0.1:38085
1705313102482 geckodriver INFO Listening on 127.0.0.1:43737
1705313103327 geckodriver INFO Listening on 127.0.0.1:37079
1705313104189 geckodriver INFO Listening on 127.0.0.1:41321
1705313104991 geckodriver INFO Listening on 127.0.0.1:33763
1705313105755 geckodriver INFO Listening on 127.0.0.1:47579
82 changes: 41 additions & 41 deletions output/plot_rasterio2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified output/w_many_features.gpkg
Binary file not shown.
Binary file modified output/w_many_layers.gpkg
Binary file not shown.
Binary file modified output/world.gpkg
Binary file not shown.
Loading

0 comments on commit 950ad77

Please sign in to comment.