Skip to content

Commit 1e888a7

Browse files
2023 edition - update materials (#64)
1 parent 6ae2cac commit 1e888a7

50 files changed

Lines changed: 674 additions & 592 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

_solved/00-jupyter_introduction.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"\n",
99
"\n",
1010
"> *DS Python for GIS and Geoscience* \n",
11-
"> *November, 2022*\n",
11+
"> *September, 2023*\n",
1212
">\n",
13-
"> *© 2022, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
13+
"> *© 2023, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
1414
"\n",
1515
"---"
1616
]
@@ -548,7 +548,7 @@
548548
"name": "python",
549549
"nbconvert_exporter": "python",
550550
"pygments_lexer": "ipython3",
551-
"version": "3.10.6"
551+
"version": "3.11.5"
552552
},
553553
"nav_menu": {},
554554
"toc": {

_solved/01-introduction-tabular-data.ipynb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"\n",
99
"\n",
1010
"> *DS Python for GIS and Geoscience* \n",
11-
"> *November, 2022*\n",
11+
"> *September, 2023*\n",
1212
">\n",
13-
"> *© 2022, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
13+
"> *© 2023, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
1414
"\n",
1515
"---"
1616
]
@@ -231,6 +231,22 @@
231231
"countries.sort_values(by='pop_est')"
232232
]
233233
},
234+
{
235+
"cell_type": "markdown",
236+
"metadata": {},
237+
"source": [
238+
"<div style=\"border: 5px solid #3776ab; border-radius: 2px; padding: 2em;\">\n",
239+
"\n",
240+
"## Python recap\n",
241+
"\n",
242+
"Python objects have **attributes** and **methods**:\n",
243+
"\n",
244+
"* Attribute: `obj.attribute` (no parentheses!) -> property of the object (pandas examples: `dtypes`, `columns`, `shape`, ..)\n",
245+
"* Method: `obj.method()` (function call with parentheses) -> action (pandas examples: `mean()`, `sort_values()`, ...)\n",
246+
"\n",
247+
"</div>"
248+
]
249+
},
234250
{
235251
"cell_type": "markdown",
236252
"metadata": {},
@@ -524,7 +540,7 @@
524540
"cell_type": "markdown",
525541
"metadata": {},
526542
"source": [
527-
"Often, you want to select rows based on a certain condition. This can be done with *'boolean indexing'* (like a where clause in SQL). \n",
543+
"Often, you want to select rows based on a certain condition. This can be done with *'boolean indexing'* (like a WHERE clause in SQL).\n",
528544
"\n",
529545
"The indexer (or boolean mask) should be 1-dimensional and the same length as the thing being indexed."
530546
]
@@ -1886,6 +1902,7 @@
18861902
"metadata": {
18871903
"celltoolbar": "Nbtutor - export exercises",
18881904
"jupytext": {
1905+
"cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow",
18891906
"formats": "ipynb,md:myst"
18901907
},
18911908
"kernelspec": {
@@ -1903,7 +1920,7 @@
19031920
"name": "python",
19041921
"nbconvert_exporter": "python",
19051922
"pygments_lexer": "ipython3",
1906-
"version": "3.10.6"
1923+
"version": "3.11.5"
19071924
},
19081925
"widgets": {
19091926
"application/vnd.jupyter.widget-state+json": {

_solved/02-introduction-geospatial-data.ipynb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"\n",
99
"\n",
1010
"> *DS Python for GIS and Geoscience* \n",
11-
"> *November, 2022*\n",
11+
"> *September, 2023*\n",
1212
">\n",
13-
"> *© 2022, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
13+
"> *© 2023, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
1414
"\n",
1515
"---"
1616
]
@@ -377,7 +377,7 @@
377377
"metadata": {},
378378
"outputs": [],
379379
"source": [
380-
"from shapely.geometry import Point, Polygon, LineString"
380+
"from shapely import Point, Polygon, LineString"
381381
]
382382
},
383383
{
@@ -1061,6 +1061,9 @@
10611061
],
10621062
"metadata": {
10631063
"celltoolbar": "Nbtutor - export exercises",
1064+
"jupytext": {
1065+
"cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow"
1066+
},
10641067
"kernelspec": {
10651068
"display_name": "Python 3 (ipykernel)",
10661069
"language": "python",
@@ -1076,7 +1079,7 @@
10761079
"name": "python",
10771080
"nbconvert_exporter": "python",
10781081
"pygments_lexer": "ipython3",
1079-
"version": "3.10.6"
1082+
"version": "3.11.5"
10801083
},
10811084
"widgets": {
10821085
"application/vnd.jupyter.widget-state+json": {

_solved/03-coordinate-reference-systems.ipynb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"\n",
99
"\n",
1010
"> *DS Python for GIS and Geoscience* \n",
11-
"> *November, 2022*\n",
11+
"> *September, 2023*\n",
1212
">\n",
13-
"> *© 2022, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
13+
"> *© 2023, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
1414
"\n",
1515
"---"
1616
]
@@ -533,6 +533,10 @@
533533
],
534534
"metadata": {
535535
"celltoolbar": "Nbtutor - export exercises",
536+
"jupytext": {
537+
"cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow",
538+
"notebook_metadata_filter": "-jupytext.cell_metadata_filter"
539+
},
536540
"kernelspec": {
537541
"display_name": "Python 3 (ipykernel)",
538542
"language": "python",
@@ -548,7 +552,7 @@
548552
"name": "python",
549553
"nbconvert_exporter": "python",
550554
"pygments_lexer": "ipython3",
551-
"version": "3.10.6"
555+
"version": "3.11.5"
552556
},
553557
"widgets": {
554558
"application/vnd.jupyter.widget-state+json": {

_solved/04-spatial-relationships-joins.ipynb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"\n",
99
"\n",
1010
"> *DS Python for GIS and Geoscience* \n",
11-
"> *November, 2022*\n",
11+
"> *September, 2023*\n",
1212
">\n",
13-
"> *© 2022, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
13+
"> *© 2023, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
1414
"\n",
1515
"---"
1616
]
@@ -726,7 +726,7 @@
726726
"metadata": {},
727727
"outputs": [],
728728
"source": [
729-
"france = countries.loc[countries['name'] == 'France', 'geometry'].squeeze()"
729+
"france = countries.loc[countries['name'] == 'France', 'geometry'].item()"
730730
]
731731
},
732732
{
@@ -1110,6 +1110,9 @@
11101110
}
11111111
],
11121112
"metadata": {
1113+
"jupytext": {
1114+
"cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow"
1115+
},
11131116
"kernelspec": {
11141117
"display_name": "Python 3 (ipykernel)",
11151118
"language": "python",
@@ -1125,7 +1128,7 @@
11251128
"name": "python",
11261129
"nbconvert_exporter": "python",
11271130
"pygments_lexer": "ipython3",
1128-
"version": "3.10.6"
1131+
"version": "3.11.5"
11291132
},
11301133
"widgets": {
11311134
"application/vnd.jupyter.widget-state+json": {

_solved/05-spatial-operations-overlays.ipynb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"\n",
99
"\n",
1010
"> *DS Python for GIS and Geoscience* \n",
11-
"> *November, 2022*\n",
11+
"> *September, 2023*\n",
1212
">\n",
13-
"> *© 2022, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
13+
"> *© 2023, Joris Van den Bossche and Stijn Van Hoey (<mailto:jorisvandenbossche@gmail.com>, <mailto:stijnvanhoey@gmail.com>). Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
1414
"\n",
1515
"---"
1616
]
@@ -301,7 +301,7 @@
301301
},
302302
"outputs": [],
303303
"source": [
304-
"print(str(africa)[:1000])"
304+
"print(str(africa)[:100])"
305305
]
306306
},
307307
{
@@ -1411,9 +1411,6 @@
14111411
],
14121412
"metadata": {
14131413
"celltoolbar": "Nbtutor - export exercises",
1414-
"jupytext": {
1415-
"cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow"
1416-
},
14171414
"kernelspec": {
14181415
"display_name": "Python 3 (ipykernel)",
14191416
"language": "python",
@@ -1429,7 +1426,7 @@
14291426
"name": "python",
14301427
"nbconvert_exporter": "python",
14311428
"pygments_lexer": "ipython3",
1432-
"version": "3.10.6"
1429+
"version": "3.11.5"
14331430
},
14341431
"widgets": {
14351432
"application/vnd.jupyter.widget-state+json": {

_solved/10-introduction-raster.ipynb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"\n",
99
"\n",
1010
"> *DS Python for GIS and Geoscience* \n",
11-
"> *November, 2022*\n",
11+
"> *September, 2023*\n",
1212
">\n",
13-
"> *© 2022, Joris Van den Bossche and Stijn Van Hoey. Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
13+
"> *© 2023, Joris Van den Bossche and Stijn Van Hoey. Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
1414
"\n",
1515
"---"
1616
]
@@ -221,7 +221,7 @@
221221
" - `x`: the x coordinates of the data set\n",
222222
"- Each of these dimensions are defined by a __coordinate__ (1D) array\n",
223223
"- It contains 304407 (227\\*447\\*3) data values (stored as float32)\n",
224-
"- Other metadata, such as the spatial information) provided by the `tiff` are stored in the __`Attributes`__\n",
224+
"- Other metadata, such as some of the metadata provided by the `tiff` file are stored in the __`Attributes`__. The attributes is a dictionary.\n",
225225
"\n",
226226
"Looking to the data itself (click on the icons on the right), we can see these are Numpy arrays..."
227227
]
@@ -546,6 +546,13 @@
546546
"\n",
547547
"</div>"
548548
]
549+
},
550+
{
551+
"cell_type": "code",
552+
"execution_count": null,
553+
"metadata": {},
554+
"outputs": [],
555+
"source": []
549556
}
550557
],
551558
"metadata": {
@@ -568,7 +575,7 @@
568575
"name": "python",
569576
"nbconvert_exporter": "python",
570577
"pygments_lexer": "ipython3",
571-
"version": "3.10.6"
578+
"version": "3.11.5"
572579
}
573580
},
574581
"nbformat": 4,

_solved/11-xarray-intro.ipynb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"\n",
99
"\n",
1010
"> *DS Python for GIS and Geoscience* \n",
11-
"> *November, 2022*\n",
11+
"> *September, 2023*\n",
1212
">\n",
13-
"> *© 2022, Joris Van den Bossche and Stijn Van Hoey. Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
13+
"> *© 2023, Joris Van den Bossche and Stijn Van Hoey. Licensed under [CC BY 4.0 Creative Commons](http://creativecommons.org/licenses/by/4.0/)*\n",
1414
"\n",
1515
"---"
1616
]
@@ -426,14 +426,15 @@
426426
"\n",
427427
"**EXERCISE**:\n",
428428
"\n",
429-
"* Read in the file `./data/gent/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff` with xarray and assign the data to a new variable `tc_data`. \n",
429+
"* Read in the file `./data/gent/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff` with xarray in the original raw data dtype and assign the data to a new variable `tc_data`. \n",
430430
"* Inspect the display of `tc_data`. What are the different dimensions of the array? \n",
431431
"* Select only the *second* layer of `tc_data` and assign the output to a new variable `tc_g`.\n",
432432
"* Plot the second (green) layer.\n",
433433
"* Create a new array with each of the elements in the `tc_g` array with a value above 15000 set to a new value of 65535.\n",
434434
" \n",
435435
"<details><summary>Hints</summary>\n",
436436
"\n",
437+
"* Use the `mask_and_scale` parameter in the reader function to make sure the data type of tc_data is the same as the raw data.\n",
437438
"* To select a specific subset for a certain dimension of a DataArray, use the `.sel()` method. The argument name to use is the name of the dimension.\n",
438439
"* You can mask an array and set the masked values to another value with the `.where()` method. Check the help of the method for information on the keyword arguments. \n",
439440
"\n",
@@ -595,7 +596,7 @@
595596
},
596597
"outputs": [],
597598
"source": [
598-
"b4_data = xr.open_dataarray(\"./data/gent/raster/2020-09-17_Sentinel_2_L1C_B04.tiff\", \n",
599+
"b4_data = xr.open_dataarray(\"./data/gent/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff\", \n",
599600
" engine=\"rasterio\", mask_and_scale=False)"
600601
]
601602
},
@@ -662,11 +663,18 @@
662663
"outputs": [],
663664
"source": [
664665
"# Create the histogram plots\n",
665-
"fig, (ax0, ax1) = plt.subplots(1, 2, sharey=True)\n",
666+
"fig, (ax0, ax1) = plt.subplots(1, 2, sharey=True, figsize=(12, 4))\n",
666667
"b4_data.plot.hist(bins=30, log=True, ax=ax0)\n",
667668
"b4_data_f.plot.hist(bins=30, log=True, ax=ax1);"
668669
]
669670
},
671+
{
672+
"cell_type": "markdown",
673+
"metadata": {},
674+
"source": [
675+
"This is what the `mask_and_scale` option also does: it reads the nodata value from the metadata in the tiff file and uses this to mask the no-data value with a Nan value."
676+
]
677+
},
670678
{
671679
"cell_type": "markdown",
672680
"metadata": {},
@@ -1156,7 +1164,7 @@
11561164
"\n",
11571165
"The data set `./data/herstappe/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff` (assign to variable `herstappe_data`) has values ranging in between 0.11325, 0.8575. To improve the quality of the visualization, stretch __each of the layers individually__ to the values to the range 0. to 1. with a linear transformation: \n",
11581166
" \n",
1159-
"$$z_i=\\frac{x_i-\\min(x)}{\\max(x)-\\min(x)}$$\n",
1167+
"$$z_{i,scaled}=\\frac{z_i-\\min(z)}{\\max(z)-\\min(z)}$$\n",
11601168
"\n",
11611169
"Make a plot of the end result and compare with a plot of the original data. \n",
11621170
"\n",
@@ -1251,7 +1259,6 @@
12511259
"- Read the data file and assign to a variable `gent`.\n",
12521260
"- Try to plot it with the `imshow()` method.\n",
12531261
"- Convert the array to a float array and call it `gent_f`.\n",
1254-
"- Convert the max value of 65536 to np.nan (as we have done before).\n",
12551262
"- Now divide the array by 65536 to get our data in a [0-1] range.\n",
12561263
"- Plot the result with the `imshow()` method.\n",
12571264
"\n",
@@ -1303,9 +1310,8 @@
13031310
},
13041311
"outputs": [],
13051312
"source": [
1306-
"# Convert to float and make 65535 equal to Nan\n",
1307-
"gent_f = gent.astype(float)\n",
1308-
"gent_f = gent_f.where(gent != 65535)"
1313+
"# Convert to float\n",
1314+
"gent_f = gent.astype(float)"
13091315
]
13101316
},
13111317
{
@@ -1648,7 +1654,8 @@
16481654
"metadata": {
16491655
"celltoolbar": "Nbtutor - export exercises",
16501656
"jupytext": {
1651-
"cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow"
1657+
"cell_metadata_filter": "-run_control,-deletable,-editable,-jupyter,-slideshow",
1658+
"notebook_metadata_filter": "-jupytext.cell_metadata_filter"
16521659
},
16531660
"kernelspec": {
16541661
"display_name": "Python 3 (ipykernel)",
@@ -1665,7 +1672,7 @@
16651672
"name": "python",
16661673
"nbconvert_exporter": "python",
16671674
"pygments_lexer": "ipython3",
1668-
"version": "3.10.6"
1675+
"version": "3.11.5"
16691676
},
16701677
"widgets": {
16711678
"application/vnd.jupyter.widget-state+json": {

0 commit comments

Comments
 (0)