Skip to content

Commit

Permalink
update stack and move to rioxarray for reading data (#309)
Browse files Browse the repository at this point in the history
* start the book stack

* update nightly testing infrastructure

* fix myst_nb to myst-nb

* add missing nbconvert

* swap open_rasterio from xarray to rioxarray

* update notebooks
  • Loading branch information
ljwolf authored Oct 13, 2023
1 parent 78e9425 commit cfb60e1
Show file tree
Hide file tree
Showing 5 changed files with 459 additions and 3,313 deletions.
1 change: 1 addition & 0 deletions infrastructure/book_stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies:
- ipython
- mapclassify
- myst-nb
- nbconvert
- osmnx
- pandas
- pysal
Expand Down
2,892 changes: 277 additions & 2,615 deletions notebooks/03_spatial_data.ipynb

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions notebooks/03_spatial_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ represented as grids where each cell contains a sample. A grid can also be
thought of as a table with rows and columns but, as we discussed in the previous
chapter, both of them are directly tied to a geographic location. This is in sharp contrast with geographic tables, where geography is confined to a single column.

To explore how Python represents surfaces, we will use an extract for the Brazilian city of Sao Paulo of a [global population dataset](../data/ghsl/build_ghsl_extract). This dataset records population counts in cells of the same dimensions uniformly covering the surface of the Earth. Our extract is available as a GeoTIF file, a variation of the TIF image format that includes geographic information. We can use the `open_rasterio()` method from the `xarray` package to read in the GeoTIF:
To explore how Python represents surfaces, we will use an extract for the Brazilian city of Sao Paulo of a [global population dataset](../data/ghsl/build_ghsl_extract). This dataset records population counts in cells of the same dimensions uniformly covering the surface of the Earth. Our extract is available as a GeoTIF file, a variation of the TIF image format that includes geographic information. We can use the `open_rasterio()` method from the `rioxarray` package to read in the GeoTIF:

```python
pop = xarray.open_rasterio("../data/ghsl/ghsl_sao_paulo.tif")
pop = rioxarray.open_rasterio("../data/ghsl/ghsl_sao_paulo.tif")
```

This reads the data into a `DataArray` object:
Expand Down Expand Up @@ -357,10 +357,10 @@ We will see two ways of going from surfaces to tables: one converts every pixel

#### One pixel at a time

Technically, going from surface to table involves traversing from `xarray` to `pandas` objects. This is actually a well established bridge. To illustrate it with an example, let's revisit the population counts in [Sao Paulo](../data/ghsl/build_ghsl_extract) used earlier. We can read the surface into a `DataArray` object with the `open_rasterio()` method:
Technically, going from surface to table involves traversing from `xarray` to `pandas` objects. This is actually a well established bridge. To illustrate it with an example, let's revisit the population counts in [Sao Paulo](../data/ghsl/build_ghsl_extract) used earlier. We can read the surface into a `DataArray` object with `rioxarray`, a special package designed to work with raster data in `xarray`. We can use its `open_rasterio()` method to read in the data:

```python
surface = xarray.open_rasterio("../data/ghsl/ghsl_sao_paulo.tif")
surface = rioxarray.open_rasterio("../data/ghsl/ghsl_sao_paulo.tif")
```

Transferring to a table is as simple as calling the `DataArray`'s `to_series()` method:
Expand Down Expand Up @@ -454,7 +454,7 @@ use the digital elevation model [(DEM)](../data/nasadem/build_nasadem_sd) surfac
Let's start by reading the data. First, the elevation model:

```python caption="Digital Elevation Model as a raster."
dem = xarray.open_rasterio("../data/nasadem/nasadem_sd.tif").sel(
dem = rioxarray.open_rasterio("../data/nasadem/nasadem_sd.tif").sel(
band=1
)
dem.where(dem > 0).plot.imshow();
Expand Down
867 changes: 175 additions & 692 deletions notebooks/07_local_autocorrelation.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion notebooks/07_local_autocorrelation.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ For this case, we will use the GHSL dataset that contains an extract of gridded

```python
# Open GeoTIFF file and read into `xarray.DataArray`
pop = xarray.open_rasterio("../data/ghsl/ghsl_sao_paulo.tif")
pop = rioxarray.open_rasterio("../data/ghsl/ghsl_sao_paulo.tif")
```

Next is building a weights matrix that represents the spatial configuration of pixels with values in `pop`. We will use the same approach as we saw in the chapter on weights:
Expand Down

0 comments on commit cfb60e1

Please sign in to comment.