Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor updates in epi 7, 8 and 10. Also exercise added in epi 8 #122

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions episodes/07-vector-data-in-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ questions:

In the preceding episodes, we have prepared, selected and downloaded raster data from before and after the wildfire event in the summer of 2023 on the Greek island of Rhodes. To evaluate the impact of this wildfire on the vital infrastructure and built-up areas we are going to create a subset of vector data representing these assets. In this episode you will learn how to extract vector data with specific characteristics like the type of attributes or their locations. The dataset that we will generate in this episode can lateron be confronted with scorched areas which we determine by analyzing the satellite images [Episode 9: Raster Calculations in Python](09-raster-calculations.md).

We'll be examining vector datasets that represent the valuable assests of Rhodes. As mentioned in [Episode 2: Introduction to Vector Data](02-intro-vector-data.md), vector data uses points, lines, and polygons to depict specific features on the Earth's surface. These geographic elements can have one or more attributes, like 'name' and 'population' for a city. In this epidoe we'll be using two open data sources: the Database of Global Administrative Areas (GADM) dataset to generate a polygon for the island of Rhodes and and Open Street Map data for the vital infrastructure and valuable assets.
We'll be examining vector datasets that represent the valuable assests of Rhodes. As mentioned in [Episode 2: Introduction to Vector Data](02-intro-vector-data.md), vector data uses points, lines, and polygons to depict specific features on the Earth's surface. These geographic elements can have one or more attributes, like 'name' and 'population' for a city. In this episode we'll be using two open data sources: the Database of Global Administrative Areas (GADM) dataset to generate a polygon for the island of Rhodes and and Open Street Map data for the vital infrastructure and valuable assets.

To handle the vector data in python we use the package [`geopandas`](https://geopandas.org/en/stable/). This package allows us to open, manipulate, and write vector dataset through python.

Expand Down Expand Up @@ -308,16 +308,16 @@ Now it will be up to you to create a dataset with valueable assets. You should b
:::challenge
## Exercise: Get the built-up regions

Create a `builtup_buffer` from the file `data/osm_landuse.gpkg` by the following steps:
Create a `builtup_buffer` from the file `data/osm/osm_landuse.gpkg` by the following steps:

1. Load the land use data from `data/osm_landuse.gpkg` and mask it with the administrative boundary of Rhodes Island (`gdf_rhodes`).
1. Load the land use data from `data/osm/osm_landuse.gpkg` and mask it with the administrative boundary of Rhodes Island (`gdf_rhodes`).
2. Select the land use data for "commercial", "industrial", and "residential".
3. Create a 10m buffer around the land use data.
4. Visualize the results.

After completing the exercise, answer the following questions:

1. How many unique land use types are there in `landuse.gpkg`?
1. How many unique land use types are there in `osm_landuse.gpkg`?
2. After selecting the three types of land use, how many entries (rows) are there in the results?

Hints:
Expand All @@ -329,7 +329,7 @@ Hints:
::::solution
```python
# Read data with a mask of Rhodes
gdf_landuse = gpd.read_file('../data/osm/landuse.gpkg', mask=gdf_rhodes)
gdf_landuse = gpd.read_file('./data_workshop/osm/osm_landuse.gpkg', mask=gdf_rhodes)

# Find number of unique landuse types
print(len(gdf_landuse['fclass'].unique()))
Expand Down
52 changes: 50 additions & 2 deletions episodes/08-crop-raster-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
file in your current working directory, and extract the archive file by double-clicking on it or by running the
following command in your terminal `tar -zxvf geospatial-python-raster-dataset.tar.gz`. Use the file `geospatial-python-raster-dataset/search.json`
(instead of `search.json`) to get started with this lesson.

Check warning on line 33 in episodes/08-crop-raster-data.md

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[missing anchor]: [Align the CRS of the raster and the vector data](#Align-the-CRS-of-the-raster-and-the-vector-data)
We also use the cropped fields polygons `fields_cropped.shp`, which was generated in an exercise from [Episode 7: Vector data in python](07-vector-data-in-python.md).
:::

Expand Down Expand Up @@ -143,9 +143,57 @@

![](fig/E08/visual_clip.png){alt="Clip results"}

:::challenge
## Exercise: Get the red band for the Rhodes

Now that you have seen how clip a raster using a polygon, we want you to do this for the red band of the sattelite image. Use the shape of Rhodes from GADM and clip the red band with it. Furthermore, make sure to transform the no data values to Not a Number values.

::::solution
```python
# Solution

# Step 1 - Load the datasets - Vector data

import geopandas as gpd
gdf_greece = gpd.read_file('./data_workshop/gadm/ADM_ADM_3.gpkg')
gdf_rhodes = gdf_greece.loc[gdf_greece['NAME_3']=='Rhodos']

# Step 2 - Load the raster red band
import rioxarray
path_red = './data_workshop/sentinel2/red.tif'
red = rioxarray.open_rasterio(path_red, overview_level=1)

# Step 3 - It will not work, since it is not projected yet

gdf_rhodes = gdf_rhodes.to_crs(red.rio.crs)

# Step 4 - Clip the two

red_clip = red.rio.clip(gdf_rhodes["geometry"])

# Step 5 - assing nan values to no data

red_clip_nan = red_clip.where(red_clip!=red_clip.rio.nodata)

# Step 6 - Visualize the result

red_clip_nan.plot()

```

![](fig/E08/solution_excercise.png){alt="rhodes_builtup_buffer"}

::::
:::






### Match two rasters

Sometimes you need to match two rasters with different extents, resolutions, or CRS. The `reproject_match` function can be used for this purpose. We will demonstrate this by matching the cropped raster `visual_clip` with the Digital Elevation Model (DEM),`rhodes_dem.tif` of Rhodes.
Sometimes you need to match two rasters with different extents, resolutions, or CRS. For this you can use the [`reproject_match`](https://corteva.github.io/rioxarray/stable/examples/reproject_match.html#Reproject-Match) function . We will demonstrate this by matching the cropped raster `visual_clip` with the Digital Elevation Model (DEM),`rhodes_dem.tif` of Rhodes.

First, let's load the DEM:

Expand All @@ -156,7 +204,7 @@
And visualize it:

```python
dem.plot()

Check warning on line 207 in episodes/08-crop-raster-data.md

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[missing file]: [](fig/E08/solution_excercise.png)
```

![](fig/E08/dem.png){alt="DEM"}
Expand Down Expand Up @@ -196,7 +244,7 @@
Finally, we can save the matched DEM for later use. We save it as a Cloud-Optimized GeoTIFF (COG) file:

```python
dem_match.rio.to_raster('dem_rhodes_match.tif', driver='COG')
dem_matched.rio.to_raster('dem_rhodes_match.tif', driver='COG')
```

:::callout
Expand Down
2 changes: 1 addition & 1 deletion episodes/10-zonal-statistics.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- Calculate raster statistics over zones
:::




## Introduction
Expand Down Expand Up @@ -123,7 +123,7 @@
assets_rasterized_xarr.plot()
```

![](../fig/E10/zones_rasterized_xarray.png){alt="Rasterized zones"}

Check warning on line 126 in episodes/10-zonal-statistics.md

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[missing file]: [](../fig/E10/zones_rasterized_xarray.png)

Then we can calculate the zonal statistics using the `zonal_stats` function:

Expand Down
Binary file added episodes/fig/E08/solution_exercise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading