Skip to content

Commit

Permalink
Merge pull request #185 from geocompx/184-output-datasets-frequently-…
Browse files Browse the repository at this point in the history
…updated

Do not evaluate chunks that update data #184
  • Loading branch information
Robinlovelace authored Aug 11, 2023
2 parents d9ddbe5 + b79e348 commit d2c94aa
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions 02-spatial-data.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ Finally, to export the raster for permanent storage, along with the CRS definiti
In the case of `elev`, we do it as follows:

```{python}
#| eval: false
new_dataset = rasterio.open(
'output/elev.tif', 'w',
driver='GTiff',
Expand All @@ -785,6 +786,7 @@ Note that the CRS we (arbitrarily) set for the `elev` raster is WGS84, defined u
Here is how we export the `grain` raster as well, using almost the exact same code just with `elev` replaced with `grain`:

```{python}
#| eval: false
new_dataset = rasterio.open(
'output/grain.tif', 'w',
driver='GTiff',
Expand Down
1 change: 1 addition & 0 deletions 05-geometry-operations.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ dst.close()
Here is another code section to demontrate another resampling method, the maximum resampling, i.e., every new pixel gets the maximum value of all the original pixels it coincides with. Note that the transform is identical (@fig-raster-resample), so we do not need to calculate it again:

```{python}
#| eval: false
dst = rasterio.open('output/dem_resample_maximum.tif', 'w', **dst_kwargs)
rasterio.warp.reproject(
source=rasterio.band(src, 1),
Expand Down
1 change: 1 addition & 0 deletions 06-raster-vector.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ Unfortunately, `rasterio` does not provide any way of extracting the contour lin
We hereby demonstrate the first and easiest approach, using `gdal_contour`. Although we deviate from exclusively using the Python language, the benefit of `gdal_contour` is the proven algorithm, customized to spatial data, and with many relevant options. `gdal_contour` (along with other GDAL programs) should already be installed on your system since this is a dependency of `rasterio`. For example, generating 50 $m$ contours of the `dem.tif` file can be done as follows:

```{python}
#| eval: false
os.system('gdal_contour -a elev data/dem.tif output/dem_contour.gpkg -i 50.0')
```

Expand Down
2 changes: 2 additions & 0 deletions 07-reproj.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ It is also possible to reproject into an in-memory `ndarray` object, see the [do
To write the reprojected raster, we first create a destination file connection `dst_nlcd`, pointing at the output file path of our choice (`output/nlcd_4326.tif`), using the updated metadata object created earlier (`dst_kwargs`):

```{python}
#| echo: false
dst_nlcd = rasterio.open('output/nlcd_4326.tif', 'w', **dst_kwargs)
```

Expand Down Expand Up @@ -579,6 +580,7 @@ template.meta
Then, we create a write-mode connection to our destination raster, using this metadata, meaning that as the resampling result is going to have identical metadata as the "template":

```{python}
#| echo: false
dst_nlcd_2 = rasterio.open('output/nlcd_4326_2.tif', 'w', **template.meta)
```

Expand Down
15 changes: 8 additions & 7 deletions 08-read-write-plot.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,21 @@ The next two sections will demonstrate how to do this.
The counterpart of `gpd.read_file` is the `.to_file` method that a `GeoDataFrame` has. It allows you to write `GeoDataFrame` objects to a wide range of geographic vector file formats, including the most common, such as `.geojson`, `.shp` and `.gpkg`. Based on the file name, `.to_file` decides automatically which driver to use. The speed of the writing process depends also on the driver.

```{python}
#| eval: false
world.to_file('output/world.gpkg')
```

Note: if you try to write to the same data source again, the function will overwrite the file:

```{python}
#| eval: false
world.to_file('output/world.gpkg')
```

Instead of overwriting the file, we could add a new layer to the file with `mode='a'` ("append" mode, as opposed to the default `mode='w'` for "write" mode). Appending is supported by several spatial formats, including GeoPackage. For example:

```{python}
#| eval: false
world.to_file('output/world_many_features.gpkg')
world.to_file('output/world_many_features.gpkg', mode='a')
```
Expand All @@ -423,6 +426,7 @@ Here, `world_many_features.gpkg` will contain a polygonal layer named `world` wi
Alternatively, you can create another, separate, layer, within the same file. The GeoPackage format also supports multiple layers within one file. For example:

```{python}
#| eval: false
world.to_file('output/world_many_layers.gpkg')
world.to_file('output/world_many_layers.gpkg', layer='world2')
```
Expand Down Expand Up @@ -547,12 +551,7 @@ The raster data type can be specified when writing a raster (see above). For an
rasterio.open('output/r.tif').meta['dtype']
```

The file `r.tif` has the data type `np.int8`, which we specified when creating it according to the data type of the original array:

```{python}
r.dtype
```

The file `r.tif` has the data type `np.int8`, which we specified when creating it according to the data type of the original array.
When reading the data back into the Python session, the array with the same data type is recreated:

```{python}
Expand Down Expand Up @@ -614,6 +613,7 @@ r.dtype
When writing the array to file, we do not need to specify any particular `nodata` value:

```{python}
#| eval: false
dst = rasterio.open(
'output/r_nodata_float.tif', 'w',
driver = 'GTiff',
Expand Down Expand Up @@ -654,6 +654,7 @@ r.dtype
When writing the array to file, we must specify `nodata=-9999` to keep track of our "No Data" flag:

```{python}
#| eval: false
dst = rasterio.open(
'output/r_nodata_int.tif', 'w',
driver = 'GTiff',
Expand Down Expand Up @@ -765,7 +766,7 @@ For example, the following code section exports the same raster plot to a file n

```{python}
#| output: false
#| eval: false
fig, ax = plt.subplots(figsize=(5, 7))
rasterio.plot.show(nz_elev, ax=ax)
nz.to_crs(nz_elev.crs).plot(ax=ax, facecolor='none', edgecolor='r');
Expand Down

0 comments on commit d2c94aa

Please sign in to comment.