Skip to content

Commit 44b4bd1

Browse files
authored
add section on gdal api (#242)
1 parent 8294206 commit 44b4bd1

3 files changed

Lines changed: 46 additions & 101 deletions

File tree

988 Bytes
Binary file not shown.

workshop/jupyter/content/notebooks/05-raster-data.ipynb

Lines changed: 45 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -47,132 +47,77 @@
4747
"cell_type": "markdown",
4848
"metadata": {},
4949
"source": [
50-
"## Tools\n",
50+
"## GDAL\n",
5151
"\n",
52-
"[GDAL](https://gdal.org/) is today the reference toolbox to read and write raster data. It is used by almost all of the FOSS4G programmes and libraries that interact with rasters. GDAL is also used by many commercial products. A [Python API](https://gdal.org/python/) is available for GDAL that provides much of the functionality.\n",
53-
"\n",
54-
"The [RasterIO](https://rasterio.readthedocs.io) library makes interaction with rasters considerably more convenient, however. It is in essence a bridge between GDAL and the [NumPy](https://numpy.org/) package for scientific computing. With RasterIO rasters are easily translated into NumPy arrays and vice-versa."
55-
]
56-
},
57-
{
58-
"cell_type": "markdown",
59-
"metadata": {},
60-
"source": [
61-
"## Reading in and inspecting a raster\n",
62-
"\n",
63-
"Possibly the most essential operation is to open a raster for processing or inspection. This is rather simple:\n",
64-
"\n"
65-
]
52+
"[GDAL](https://gdal.org/) is the reference toolbox to read and write raster data. It is used by almost all of the \n",
53+
"FOSS4G programmes and libraries that interact with rasters. GDAL is also used by many commercial products. \n",
54+
"A [Python API](https://gdal.org/python/) is available with most of the GDAL functionality."
55+
]
6656
},
6757
{
6858
"cell_type": "code",
6959
"execution_count": null,
7060
"metadata": {},
7161
"outputs": [],
7262
"source": [
73-
"import rasterio"
74-
]
75-
},
76-
{
77-
"cell_type": "code",
78-
"execution_count": null,
79-
"metadata": {},
80-
"outputs": [],
81-
"source": [
82-
"world = rasterio.open('../data/world.rgb.tif')"
63+
"from osgeo import gdal\n",
64+
"gdal.UseExceptions()\n",
65+
"ds = gdal.Open('../data/world.rgb.tif')\n",
66+
"band = ds.GetRasterBand(1)\n",
67+
"band.GetDescription()"
8368
]
8469
},
8570
{
8671
"cell_type": "markdown",
8772
"metadata": {},
8873
"source": [
89-
"The `open` method returns an object of the class `DatasetReader`, which contains the raster meta-data and the set of bands included. `open` can also be invoked in write mode (with the extra argument `'w'`), in which case it returns a `DatasetWriter` type object.\n",
90-
"\n",
91-
"`DatasetReader` provides easy access to useful meta-data, for instance the raster dimensions:"
92-
]
74+
"A typical use case for using gdal in python is resampling a grid and creating overviews, so the tiff can be used as a\n",
75+
"Cloud Optimized GeoTiff"
76+
]
9377
},
9478
{
9579
"cell_type": "code",
9680
"execution_count": null,
9781
"metadata": {},
9882
"outputs": [],
9983
"source": [
100-
"world.width"
84+
"# Resample\n",
85+
"gdal.Warp('temp/output_3857_cog.tif','../data/world.rgb.tif',dstSRS='EPSG:3857',format='COG')\n"
86+
10187
]
10288
},
103-
{
104-
"cell_type": "code",
105-
"execution_count": null,
106-
"metadata": {},
107-
"outputs": [],
108-
"source": [
109-
"world.height"
110-
]
111-
},
112-
{
113-
"cell_type": "markdown",
114-
"metadata": {},
115-
"source": [
116-
"Also information related to the reader object itself:"
117-
]
118-
},
119-
{
120-
"cell_type": "code",
121-
"execution_count": null,
122-
"metadata": {},
123-
"outputs": [],
124-
"source": [
125-
"world.name"
126-
]
127-
},
128-
{
129-
"cell_type": "code",
130-
"execution_count": null,
131-
"metadata": {},
132-
"outputs": [],
133-
"source": [
134-
"world.mode"
135-
]
136-
},
137-
{
138-
"cell_type": "code",
139-
"execution_count": null,
140-
"metadata": {},
141-
"outputs": [],
142-
"source": [
143-
"world.meta"
144-
]
145-
},
146-
{
89+
{
14790
"cell_type": "code",
14891
"execution_count": null,
14992
"metadata": {},
15093
"outputs": [],
15194
"source": [
152-
"world.shape"
95+
"# Display COG in a viewer \n",
96+
"from localtileserver import TileClient, get_leaflet_tile_layer \n",
97+
"from ipyleaflet import Map \n",
98+
"client = TileClient('temp/output_3857_cog.tif') \n",
99+
"m = Map() \n",
100+
"m.add(get_leaflet_tile_layer(client)) \n",
101+
"m\n"
153102
]
154103
},
104+
105+
155106
{
156107
"cell_type": "markdown",
157108
"metadata": {},
158109
"source": [
159-
"The `count` property informs on the number of bands:"
160-
]
161-
},
162-
{
163-
"cell_type": "code",
164-
"execution_count": null,
165-
"metadata": {},
166-
"outputs": [],
167-
"source": [
168-
"world.count"
110+
"## RasterIO \n",
111+
"\n",
112+
"The [RasterIO](https://rasterio.readthedocs.io) library is a bridge between GDAL and the [NumPy](https://numpy.org/) package\n",
113+
"for scientific computing. With RasterIO rasters are easily translated into NumPy arrays and vice-versa."
169114
]
170115
},
171116
{
172117
"cell_type": "markdown",
173118
"metadata": {},
174119
"source": [
175-
"Data types of bands are provided with `dtypes`:"
120+
"Open a raster for processing or inspection:\n"
176121
]
177122
},
178123
{
@@ -181,23 +126,19 @@
181126
"metadata": {},
182127
"outputs": [],
183128
"source": [
184-
"world.dtypes"
129+
"import rasterio\n",
130+
"world = rasterio.open('../data/world.rgb.tif')"
185131
]
186132
},
187133
{
188134
"cell_type": "markdown",
189135
"metadata": {},
190136
"source": [
191-
"The geographic information is also available:"
192-
]
193-
},
194-
{
195-
"cell_type": "code",
196-
"execution_count": null,
197-
"metadata": {},
198-
"outputs": [],
199-
"source": [
200-
"world.crs"
137+
"The `open` method returns an object of the class `DatasetReader`, which contains the raster meta-data and the set of \n",
138+
"bands included. `open` can also be invoked in write mode (with the extra argument `'w'`), in which case it returns a \n",
139+
"`DatasetWriter` type object.\n",
140+
"\n",
141+
"`DatasetReader` provides easy access to useful meta-data:"
201142
]
202143
},
203144
{
@@ -206,14 +147,17 @@
206147
"metadata": {},
207148
"outputs": [],
208149
"source": [
209-
"world.bounds"
150+
"print('size:', world.width, world.height)\n",
151+
"print('crs:', world.crs)\n",
152+
"print('bounds:', world.bounds)\n",
153+
"\n"
210154
]
211155
},
212156
{
213157
"cell_type": "markdown",
214158
"metadata": {},
215159
"source": [
216-
"## Basic raster plotting"
160+
"## Raster plotting"
217161
]
218162
},
219163
{
@@ -238,7 +182,7 @@
238182
"cell_type": "markdown",
239183
"metadata": {},
240184
"source": [
241-
"We can also display a single band of a multiband image by passing a tuple (raster source, band):"
185+
"We can also display a single band or a multiband image by passing a tuple (raster source, band):"
242186
]
243187
},
244188
{
@@ -533,4 +477,4 @@
533477
},
534478
"nbformat": 4,
535479
"nbformat_minor": 4
536-
}
480+
}

workshop/jupyter/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ geopandas
77
descartes
88
folium
99
bokeh
10+
localtileserver
1011
ipyleaflet
1112
pydeck
1213
pygeometa # >0.7 removed by Just

0 commit comments

Comments
 (0)