|
479 | 479 | "cell_type": "markdown", |
480 | 480 | "metadata": {}, |
481 | 481 | "source": [ |
482 | | - "### Buffer\n", |
483 | | - "First we need to open the *Data Source*, printing the number of Layers." |
| 482 | + "### Basic programming patterns\n", |
| 483 | + "First, we open a *Data Source* and print its number of *Layers*. Note that we also globally enable GDAL Exceptions. By default, the GDAL/OGR Python bindings do not raise exceptions when errors occur. Instead, they return an error value such as `None` and write an error message to `sys.stdout`. You can enable exceptions by calling the `UseExceptions()` function. This is a recommended practice. See [this tutorial](https://pcjericks.github.io/py-gdalogr-cookbook/gdal_general.html#enable-python-exceptions)." |
484 | 484 | ] |
485 | 485 | }, |
486 | 486 | { |
|
493 | 493 | }, |
494 | 494 | "outputs": [], |
495 | 495 | "source": [ |
496 | | - "from osgeo import ogr\n", |
| 496 | + "from osgeo import ogr, gdal\n", |
| 497 | + "\n", |
| 498 | + "# Exceptions will get raised on anything >= gdal.CE_Failure\n", |
| 499 | + "gdal.UseExceptions()\n", |
| 500 | + "\n", |
497 | 501 | "ds = ogr.Open('../data/countries.gpkg')\n", |
498 | 502 | "print(ds)\n", |
499 | 503 | "print(ds.GetLayerCount())" |
|
502 | 506 | { |
503 | 507 | "cell_type": "markdown", |
504 | 508 | "metadata": {}, |
505 | | - "source": [ |
506 | | - "Next we have to fetch and open the *Layer*. NB for files, there is usually just one layer, index `0`, \n", |
507 | | - "within the Data Source (DS), but for example for a database DS, a Layer is refers to a concrete table)." |
508 | | - ] |
| 509 | + "source": "Next we fetch and open a *Layer*. NB for files like GeoJSON, there is usually just one layer, at index 0, within the Data Source (DS). Datasources like GeoPackages and databases in general may have multiple Layers, where a Layer refers to a concrete table." |
509 | 510 | }, |
510 | 511 | { |
511 | 512 | "cell_type": "code", |
|
525 | 526 | { |
526 | 527 | "cell_type": "markdown", |
527 | 528 | "metadata": {}, |
528 | | - "source": [ |
529 | | - "Show the schema of the layer and the definition of its geometry type:" |
530 | | - ] |
| 529 | + "source": "Show the schema of the Layer and the definition of its geometry type:" |
531 | 530 | }, |
532 | 531 | { |
533 | 532 | "cell_type": "code", |
|
1772 | 1771 | "name": "python", |
1773 | 1772 | "nbconvert_exporter": "python", |
1774 | 1773 | "pygments_lexer": "ipython3", |
1775 | | - "version": "3.11.4" |
| 1774 | + "version": "3.10.12" |
1776 | 1775 | }, |
1777 | 1776 | "vscode": { |
1778 | 1777 | "interpreter": { |
|
1781 | 1780 | } |
1782 | 1781 | }, |
1783 | 1782 | "nbformat": 4, |
1784 | | - "nbformat_minor": 2 |
| 1783 | + "nbformat_minor": 4 |
1785 | 1784 | } |
0 commit comments