diff --git a/02-spatial-data.qmd b/02-spatial-data.qmd index 1f7afb75..d1f51aa1 100644 --- a/02-spatial-data.qmd +++ b/02-spatial-data.qmd @@ -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', @@ -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', diff --git a/05-geometry-operations.qmd b/05-geometry-operations.qmd index 8aa6abf0..33a94f05 100644 --- a/05-geometry-operations.qmd +++ b/05-geometry-operations.qmd @@ -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), diff --git a/06-raster-vector.qmd b/06-raster-vector.qmd index 87318b5d..1123776e 100644 --- a/06-raster-vector.qmd +++ b/06-raster-vector.qmd @@ -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') ``` diff --git a/07-reproj.qmd b/07-reproj.qmd index f94be6ac..4161e5b8 100644 --- a/07-reproj.qmd +++ b/07-reproj.qmd @@ -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) ``` @@ -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) ``` diff --git a/08-read-write-plot.qmd b/08-read-write-plot.qmd index eec8706d..329b9a4b 100644 --- a/08-read-write-plot.qmd +++ b/08-read-write-plot.qmd @@ -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') ``` @@ -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') ``` @@ -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} @@ -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', @@ -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', @@ -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');