Skip to content

Commit

Permalink
ch03 - jn comments
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldorman committed Oct 21, 2023
1 parent 9cbaa69 commit e1969f3
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 03-spatial-operations.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ ndvi = (nir-red)/(nir+red)
We also convert values \>`1` to "No Data".
<!-- 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
Expand All @@ -1077,11 +1078,26 @@ ndvi[ndvi>1] = np.nan
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`.
<!-- jn: maybe explain or reference [::-1]? -->
<!-- md: good idea, now added a note -->
```{python}
landsat_rgb = landsat[:3][::-1] / landsat.max()
```
::: callout-note
Python slicing notation, which **numpy**, **pandas** and **geopandas** also follow, is `object[start:stop:step]`.
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]`
In the above example:
* The slicing expression `[:3]` therefore means layers `0`, `1`, `2` (up to `3`, exclusive)
* The slicing expression `[::-1]` therefore means all (three) bands in reverse order
:::
@fig-raster-ndvi shows the RGB image and the NDVI values calculated for the Landsat satellite image of the Zion National Park.
```{python}
Expand Down

0 comments on commit e1969f3

Please sign in to comment.