You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: index.Rmd
+15-14Lines changed: 15 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -144,7 +144,7 @@ with open('data/AHN3_05m_DTM.tif', 'wb') as file:
144
144
file.write(response.read())
145
145
```
146
146
147
-
Before continuing, please check if this step was successful.
147
+
Before continuing, please check if this step was successful (*Hint: you can check if files have been written into corresponding directory*).
148
148
149
149
## From a file with GDAL
150
150
[GDAL](https://gdal.org/) handles raster and vector geospatial data formats with Python, Java, R and C APIs. When opening a raster file in gdal, the object has a hierarchical structure starting at the Dataset level. A Dataset has a Geotransform (metadata) and can contain one or more Bands. Each Band has a Data array and potentially Overviews.
@@ -156,6 +156,7 @@ Before continuing, please check if this step was successful.
156
156
</center>
157
157
</figure>
158
158
159
+
159
160
Let us open the file we just saved. You will see you first get the dataset, and need to access the band (even though there is only one), before the data array can be accessed.
160
161
161
162
```{python, eval=FALSE}
@@ -177,13 +178,14 @@ ds = None
177
178
178
179
```{block, type="alert alert-success"}
179
180
> **Question 1**: Why do we set ds to None at the end of your script? What may happen if you do not do that?
180
-
181
181
<details>
182
-
<summary>**Click for answer**</summary>
182
+
<summary>*Click for answer*</summary>
183
183
Keeping files open may leave you vulnerable to losing data, (Geo)Pandas manage resources under the hood so you don't explicitly need to close files, but for the case of GDAL, and as you will later see, Rasterio, it's important to close your files or open them with a context manager `with open ...`
184
184
</details>
185
-
186
185
```
186
+
187
+
188
+
187
189
The GDAL Python API is not the best documented Python module. Therefore, Rasterio is explained as an alternative raster data handling module.
> **Question 2**: Adjust the code above to take a look at the DTM. Note the gaps that appear. What are these gaps?
219
-
220
221
<details>
221
-
<summary>**Click for answer**</summary>
222
+
<summary>*Click for answer*</summary>
222
223
These are buildings.
223
224
</details>
224
225
```
225
226
226
227
228
+
227
229
The metadata shows the driver (GDAL's way of knowing how to function with a specific file format), datatype, nodata value, width of raster in number of cells, height of raster in number of cells, number of raster bands in the dataset, coordinate reference system, and transformation values.
228
230
229
231
In the back-end, raster layers in Rasterio are stored as NumPy arrays, which appear when the data are read with the method `.read()`:
@@ -289,9 +291,8 @@ with rasterio.open('data/AHN3_05m_CHM.tif', 'w', **kwargs) as file:
289
291
290
292
```{block, type="alert alert-success"}
291
293
> **Question 3**: Where is the CHM the highest in the study area? Is it what you expected?
292
-
293
294
<details>
294
-
<summary>**Click for answer**</summary>
295
+
<summary>*Click for answer*</summary>
295
296
Think about where you have the most forests on campus.
296
297
</details>
297
298
```
@@ -370,14 +371,14 @@ plt.show()
370
371
371
372
```{block, type="alert alert-success"}
372
373
> **Question 4**: Why do we want an equal scale in the x and y direction for this figure?
373
-
374
374
<details>
375
-
<summary>**Click for answer**</summary>
375
+
<summary>*Click for answer*</summary>
376
376
To visualize the buildings properly, otherwise their geometries will be skewed.
377
377
</details>
378
-
379
378
```
380
379
380
+
381
+
381
382
## Other functionality
382
383
Note that this tutorial only scratches the surface of the possibilities of Rasterio. It can do most if not all things you did in `R` in the Vector - Raster tutorial. Rasterio for example also allows you to do [masking](https://rasterio.readthedocs.io/en/latest/topics/masking-by-shapefile.html), [reprojecting](https://rasterio.readthedocs.io/en/latest/topics/reproject.html), and [resampling](https://rasterio.readthedocs.io/en/latest/topics/resampling.html).
383
384
@@ -474,14 +475,14 @@ plt.show()
474
475
475
476
```{block, type="alert alert-success"}
476
477
> **Question 5**: What is represented on the x and y axis? The default axis labels are DN (x) and Frequency (y); if you were to change them, what labels would you pick to better reflect the content of the plots?
477
-
478
478
<details>
479
-
<summary>**Click for answer**</summary>
480
-
The y axis represents the count of pixels. Meanwhile the x axis represents the pixel's DN(digital value), in this tutorial since we are looking at elevation this value is actually meters. For example, in the first plot (DSM) you can see that most pixel values are in the 10 to 15 meter range
479
+
<summary>*Click for answer*</summary>
480
+
The y axis represents the count of pixels. Meanwhile the x axis represents the pixel's DN(digital value), in this tutorial since we are looking at elevation this value is actually meters. For example, in the first plot (DSM) you can see that most pixel values are in the 10 to 15 meter range
481
481
</details>
482
482
```
483
483
484
484
485
+
485
486
# More info
486
487
*[Tutorial working with rasters in Python with Rasterio](https://geohackweek.github.io/raster/04-workingwithrasters/)
487
488
*[Tutorial working with raster in Python with GDAL (for Python 2)](https://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html)
0 commit comments