Skip to content

Commit dbad606

Browse files
committed
differences for PR #183
1 parent da6edc4 commit dbad606

3 files changed

Lines changed: 73 additions & 18 deletions

File tree

06-raster-intro.md

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ downloading high-resolution images when only quick previews are required.
173173

174174
Overviews are often computed using powers of 2 as down-sampling (or zoom) factors. So, typically, the first level
175175
overview (index 0) corresponds to a zoom factor of 2, the second level overview (index 1) corresponds to a zoom factor
176-
of 4, and so on. Here, we open the third level overview (index 2, zoom factor 8) and check that the resolution is about 80 m:
176+
of 4, and so on. According to [GDAL documentation](https://gdal.org/en/stable/drivers/raster/cog.html#drivers/raster/cog-co-RESAMPLING), the default resampling method for non-paletted images is `CUBIC`, for paletted images it is `NEAREST`.
177+
178+
Here, we open the third level overview (index 2, zoom factor 8) and check that the resolution is about 80 m:
177179

178180
```python
179181
import rioxarray
@@ -217,6 +219,44 @@ More options can be consulted [here](https://docs.xarray.dev/en/v2024.02.0/gener
217219

218220
:::
219221

222+
:::challenge
223+
## Exercise: visualize the NIR band
224+
225+
There are other bands in the downloaded Sentinel-2 scene in `data/sentinel2/` directory. In this exercise, let's look in to the NIR (near-infrared) band `data/sentinel2/nir.tif`. Please perform the following steps:
226+
227+
1. Load the NIR band using `rioxarray.open_rasterio()`, what is the dimension of the NIR band? What is the resolution?
228+
2. Based on the dimension and resolution, load the NIR band again with the appropriate `overview_level` for visualization.
229+
3. Use the `plot` method to visualize the NIR band. Do you see interesting patterns in the image? What do you think they are?
230+
231+
::::solution
232+
233+
We can use the same techniques that we have used for the red band to explore the NIR band, then inspect the dimension and resolution.
234+
235+
```python
236+
rhodes_nir = rioxarray.open_rasterio("./data/sentinel2/nir.tif")
237+
print(rhodes_nir.sizes)
238+
print(rhodes_nir.rio.resolution())
239+
```
240+
241+
```output
242+
Frozen({'band': 1, 'y': 10980, 'x': 10980})
243+
(10.0, -10.0)
244+
```
245+
246+
So it turned out that the NIR band has the same dimension and resolution as the red band. Therefore, we can load the NIR band with `overview_level=2` for visualization.
247+
248+
```python
249+
rhodes_nir_80 = rioxarray.open_rasterio("./data/sentinel2/nir.tif", overview_level=2)
250+
rhodes_nir_80.plot(robust=True)
251+
```
252+
253+
![](fig/E06/rhodes_nir_80.png){alt="NIR band 80m resolution"}
254+
255+
As we can see in the image, there is a pattern of low values in NIR band on Rhodes Island, which is likely to be the burned area. We will further explore this in [episode 9](/episodes/09-raster-calculations.md).
256+
257+
::::
258+
:::
259+
220260
## View Raster Coordinate Reference System (CRS) in Python
221261
Another information that we're interested in is the CRS, and it can be accessed with `.rio.crs`. We introduced the concept of a CRS in [an earlier
222262
episode](03-crs.md). Now we will see how features of the CRS appear in our data file and what
@@ -278,15 +318,7 @@ crs.area_of_use
278318
AreaOfUse(west=24.0, south=0.0, east=30.0, north=84.0, name='Between 24°E and 30°E, northern hemisphere between equator and 84°N, onshore and offshore. Belarus. Bulgaria. Central African Republic. Democratic Republic of the Congo (Zaire). Egypt. Estonia. Finland. Greece. Latvia. Lesotho. Libya. Lithuania. Moldova. Norway. Poland. Romania. Russian Federation. Sudan. Svalbard. Türkiye (Turkey). Uganda. Ukraine.')
279319
```
280320

281-
:::challenge
282-
## Exercise: find the axes units of the CRS
283-
What units are our data in? See if you can find a method to examine this information using `help(crs)` or `dir(crs)`
284321

285-
::::solution
286-
`crs.axis_info` tells us that the CRS for our raster has two axis and both are in meters.
287-
We could also get this information from the attribute `rhodes_red_80.rio.crs.linear_units`.
288-
::::
289-
:::
290322

291323
### Understanding pyproj CRS Summary
292324
Let's break down the pieces of the `pyproj` CRS summary. The string contains all of the individual CRS elements that Python or another GIS might need, separated into distinct sections, and datum.
@@ -517,24 +549,47 @@ rhodes_visual.plot.imshow()
517549

518550
![Overview of the true-color image (multi-band raster)](fig/E06/rhodes_multiband_80.png){alt="true-color image overview"}
519551

520-
Note that the `DataArray.plot.imshow()` function makes assumptions about the shape of the input DataArray, that since it has three channels, the correct colormap for these channels is RGB. It does not work directly on image arrays with more than 3 channels. One can replace one of the RGB channels with another band, to make a false-color image.
552+
One can also specify the size of the plot, the aspect ratio:
553+
```python
554+
rhodes_visual.plot.imshow(size=5, aspect=1)
555+
```
556+
557+
![Overview of the true-color image with the correct aspect ratio](fig/E06/rhodes_multiband_80_equal_aspect.png){alt="raster plot with correct aspect ratio"}
558+
559+
521560

522561
:::challenge
523-
## Exercise: set the plotting aspect ratio
524-
As seen in the figure above, the true-color image is stretched. Let's visualize it with the right aspect ratio. You can use the [documentation](https://xarray.pydata.org/en/stable/generated/xarray.DataArray.plot.imshow.html) of `DataArray.plot.imshow()`.
562+
## Exercise: what is the correct order of the color channels?
525563

526-
::::solution
527-
Since we know the height/width ratio is 1:1 (check the `rio.height` and `rio.width` attributes), we can set the aspect ratio to be 1. For example, we can choose the size to be 5 inches, and set `aspect=1`. Note that according to the [documentation](https://xarray.pydata.org/en/stable/generated/xarray.DataArray.plot.imshow.html) of `DataArray.plot.imshow()`, when specifying the `aspect` argument, `size` also needs to be provided.
564+
We just visualized the true-color image `rhodes_visual`, which has three color channels: red, green, and blue. Apparently, the `plot.imshow()` function makes assumptions on the order of the channels in `rhodes_visual` to plot a true-color image. Can you figure out this order?
565+
566+
There are multiple ways to do this.
567+
568+
Hints:
569+
570+
1. You can use the [documentation](https://xarray.pydata.org/en/stable/generated/xarray.DataArray.plot.imshow.html) of `DataArray.plot.imshow()`, or
571+
572+
2. You can experiment this by yourself by mannually replacing values in one channel and see how the image changes.
573+
574+
:::solution
575+
576+
The order is red, green, blue.
577+
578+
To replace values of one channel, for example you can do:
528579

529580
```python
530-
rhodes_visual.plot.imshow(size=5, aspect=1)
581+
# The following code generates a red-ish image
582+
# Therefore we know that the first channel is red.
583+
rhodes_visual_modified = rhodes_visual.copy()
584+
rhodes_visual_modified.values[0,:,:] = 255 # set the values to maximum
531585
```
532586

533-
![Overview of the true-color image with the correct aspect ratio](fig/E06/rhodes_multiband_80_equal_aspect.png){alt="raster plot with correct aspect ratio"}
534-
535587
::::
536588
:::
537589

590+
In conclusion, the `DataArray.plot.imshow()` function makes assumptions about the shape of the input DataArray, and the order of these channels. The correct colormap for these channels is RGB. It does not work directly on image arrays with more than 3 channels. One can also replace one of the RGB channels with another, to make a false-color image.
591+
592+
538593
:::keypoints
539594
- `rioxarray` and `xarray` are for working with multidimensional arrays like pandas is for working with tabular data.
540595
- `rioxarray` stores CRS information as a CRS object that can be converted to an EPSG code or PROJ4 string.

fig/E06/rhodes_nir_80.png

147 KB
Loading

md5sum.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"episodes/03-crs.md" "da701bd7f61a26a5e617b80c09d2c621" "site/built/03-crs.md" "2026-01-13"
1111
"episodes/04-geo-landscape.md" "389e4a1251ae837bf6dc9ff7e88238ae" "site/built/04-geo-landscape.md" "2026-01-13"
1212
"episodes/05-access-data.md" "50a0e69f2044698f68250bee946117ec" "site/built/05-access-data.md" "2026-04-13"
13-
"episodes/06-raster-intro.md" "df117e17faa1f9f5149168bb1201f576" "site/built/06-raster-intro.md" "2026-01-13"
13+
"episodes/06-raster-intro.md" "267a46edd817d74293d3cb90409fe8d8" "site/built/06-raster-intro.md" "2026-04-14"
1414
"episodes/07-vector-data-in-python.md" "235db0013b1b35708c162086cb404e19" "site/built/07-vector-data-in-python.md" "2025-05-20"
1515
"episodes/08-crop-raster-data.md" "f1bed5014e3601cfb2e2ecaa01fb4215" "site/built/08-crop-raster-data.md" "2026-01-13"
1616
"episodes/09-raster-calculations.md" "f011028f5d2f2bc921596330479afcc5" "site/built/09-raster-calculations.md" "2025-05-20"

0 commit comments

Comments
 (0)