|
4 | 4 | How to use "gdal" CLI algorithms from Python
|
5 | 5 | ================================================================================
|
6 | 6 |
|
7 |
| -Raster commands |
8 |
| ---------------- |
| 7 | +Principles |
| 8 | +---------- |
9 | 9 |
|
10 |
| -* Getting information on a raster dataset as JSON |
| 10 | +"gdal" CLI algorithms are available as :py:class:`osgeo.gdal.Algorithm` instances. |
11 | 11 |
|
12 |
| -.. code-block:: python |
| 12 | +A convenient way to access an algorithm and run it is to use the :py:func:`osgeo.gdal.Run` |
| 13 | +function. |
| 14 | + |
| 15 | +.. py:function:: Run(alg, arguments={}, progress=None, **kwargs) |
| 16 | +
|
| 17 | + Run a GDAL algorithm and return it. |
| 18 | + |
| 19 | + .. versionadded: 3.11 |
13 | 20 |
|
14 |
| - from osgeo import gdal |
| 21 | + :param alg: Path to the algorithm or algorithm instance itself. For example "raster info", or ["raster", "info"] or "raster", "info". |
| 22 | + :type alg: str, list[str], tuple[str], Algorithm |
| 23 | + :param arguments: Input arguments of the algorithm. For example {"format": "json", "input": "byte.tif"} |
| 24 | + :type arguments: dict |
| 25 | + :param progress: Progress function whose arguments are a progress ratio, a string and a user data |
| 26 | + :type progress: callable |
| 27 | + :param kwargs: Instead of using the ``arguments`` parameter, it is possible to pass |
| 28 | + algorithm arguments directly as named parameters of gdal.Run(). |
| 29 | + If the named argument has dash characters in it, the corresponding |
| 30 | + parameter must replace them with an underscore character. |
| 31 | + For example ``dst_crs`` as a a parameter of gdal.Run(), instead of |
| 32 | + ``dst-crs`` which is the name to use on the command line. |
| 33 | + :rtype: a :py:class:`osgeo.gdal.Algorithm` instance |
15 | 34 |
|
16 |
| - gdal.UseExceptions() |
17 |
| - with gdal.run(["raster", "info"], {"input": "byte.tif"}) as info: |
18 |
| - print(info) |
19 | 35 |
|
| 36 | +If you do not need to access output value(s) of the algorithm, you can call |
| 37 | +``Run`` directly: |
| 38 | +``gdal.Run(["raster", "convert"], input="in.tif", output="out.tif")`` |
20 | 39 |
|
21 |
| -* Converting a georeferenced netCDF file to cloud-optimized GeoTIFF |
| 40 | +The name of algorithm arguments are the long names as documented in the |
| 41 | +documentation page of each algorithm. They can also be obtained with |
| 42 | +:py:meth:`osgeo.gdal.Algorithm.GetArgNames`. |
22 | 43 |
|
23 | 44 | .. code-block:: python
|
24 | 45 |
|
25 |
| - from osgeo import gdal |
| 46 | + >>> gdal.Algorithm("raster", "convert").GetArgNames() |
| 47 | + ['help', 'help-doc', 'version', 'json-usage', 'drivers', 'config', 'progress', 'output-format', 'open-option', 'input-format', 'input', 'output', 'creation-option', 'overwrite', 'append'] |
| 48 | +
|
| 49 | +For a command such as :ref:`gdal_raster_info_subcommand` that returns a JSON |
| 50 | +output, you can get the return value of :py:func:`osgeo.gdal.Run` and call the |
| 51 | +:py:meth:`osgeo.gdal.Algorithm.Output` method. |
| 52 | + |
| 53 | +.. code-block:: python |
26 | 54 |
|
27 |
| - gdal.UseExceptions() |
28 |
| - with gdal.run(["raster", "convert"], {"input": "in.tif", "output": "out.tif", "output-format": "COG", "overwrite": True}): |
29 |
| - pass |
| 55 | + alg = gdal.Run("raster", "info", input="byte.tif") |
| 56 | + info_as_dict = alg.Output() |
30 | 57 |
|
31 |
| -or |
| 58 | +If the return value is a dataset, :py:func:`osgeo.gdal.Run` can be used |
| 59 | +within a context manager, in which case :py:meth:`osgeo.gdal.Algorithm.Finalize` |
| 60 | +will be called at the exit of the context manager. |
32 | 61 |
|
33 | 62 | .. code-block:: python
|
34 | 63 |
|
35 |
| - from osgeo import gdal |
| 64 | + with gdal.Run("raster reproject", input=src_ds, output_format="MEM", |
| 65 | + dst_crs="EPSG:4326"}) as alg: |
| 66 | + values = alg.Output().ReadAsArray() |
36 | 67 |
|
37 |
| - gdal.UseExceptions() |
38 |
| - with gdal.run(["raster", "convert"], input="in.tif", output="out.tif", output_format="COG", overwrite=True): |
39 |
| - pass |
40 | 68 |
|
| 69 | +Raster commands examples |
| 70 | +------------------------ |
41 | 71 |
|
42 |
| -* Reprojecting a GeoTIFF file to a Deflate compressed tiled GeoTIFF file |
| 72 | +.. example:: |
| 73 | + :title: Getting information on a raster dataset as JSON |
43 | 74 |
|
44 |
| -.. code-block:: python |
| 75 | + .. code-block:: python |
45 | 76 |
|
46 |
| - from osgeo import gdal |
| 77 | + from osgeo import gdal |
47 | 78 |
|
48 |
| - gdal.UseExceptions() |
49 |
| - with gdal.run(["raster", "reproject"], {"input": "in.tif", "output": "out.tif", "dst-crs": "EPSG:4326", "creation-options": { "TILED": "YES", "COMPRESS": "DEFLATE"} }): |
50 |
| - pass |
| 79 | + gdal.UseExceptions() |
| 80 | + alg = gdal.Run("raster", "info", input="byte.tif") |
| 81 | + info_as_dict = alg.Output() |
51 | 82 |
|
52 | 83 |
|
53 |
| -* Reprojecting a (possibly in-memory) dataset to a in-memory dataset |
| 84 | +.. example:: |
| 85 | + :title: Converting a georeferenced netCDF file to cloud-optimized GeoTIFF |
54 | 86 |
|
55 |
| -.. code-block:: python |
| 87 | + .. code-block:: python |
56 | 88 |
|
57 |
| - from osgeo import gdal |
| 89 | + from osgeo import gdal |
58 | 90 |
|
59 |
| - gdal.UseExceptions() |
60 |
| - with gdal.run(["raster", "reproject"], {"input": "in.tif", "output-format": "MEM", "dst-crs": "EPSG:4326"}) as ds: |
61 |
| - print(ds.ReadAsArray()) |
| 91 | + gdal.UseExceptions() |
| 92 | + gdal.Run("raster", "convert", input="in.tif", output="out.tif", |
| 93 | + output_format="COG", overwrite=True) |
62 | 94 |
|
| 95 | + or |
63 | 96 |
|
64 |
| -Vector commands |
65 |
| ---------------- |
| 97 | + .. code-block:: python |
66 | 98 |
|
67 |
| -* Getting information on a vector dataset as JSON |
| 99 | + from osgeo import gdal |
68 | 100 |
|
69 |
| -.. code-block:: python |
| 101 | + gdal.UseExceptions() |
| 102 | + gdal.Run(["raster", "convert"], {"input": "in.tif", "output": "out.tif", "output-format": "COG", "overwrite": True}) |
70 | 103 |
|
71 |
| - from osgeo import gdal |
72 | 104 |
|
73 |
| - gdal.UseExceptions() |
74 |
| - with gdal.run(["raster", "info"], {"input": "poly.gpkg"}) as info: |
75 |
| - print(info) |
| 105 | +.. example:: |
| 106 | + :title: Reprojecting a GeoTIFF file to a Deflate-compressed tiled GeoTIFF file |
76 | 107 |
|
| 108 | + .. code-block:: python |
77 | 109 |
|
78 |
| -* Converting a shapefile to a GeoPackage |
| 110 | + from osgeo import gdal |
79 | 111 |
|
80 |
| -.. code-block:: python |
| 112 | + gdal.UseExceptions() |
| 113 | + gdal.Run("raster", "reproject", input="in.tif", output="out.tif", |
| 114 | + dst_crs="EPSG:4326", |
| 115 | + creation_options={ "TILED": "YES", "COMPRESS": "DEFLATE"}) |
| 116 | +
|
| 117 | +
|
| 118 | +.. example:: |
| 119 | + :title: Reprojecting a (possibly in-memory) dataset to a in-memory dataset |
| 120 | + |
| 121 | + .. code-block:: python |
| 122 | +
|
| 123 | + from osgeo import gdal |
| 124 | +
|
| 125 | + gdal.UseExceptions() |
| 126 | + with gdal.Run("raster", "reproject", input=src_ds, output_format="MEM", |
| 127 | + dst_crs="EPSG:4326"}) as alg: |
| 128 | + values = alg.Output().ReadAsArray() |
| 129 | +
|
| 130 | +
|
| 131 | +Vector commands examples |
| 132 | +------------------------ |
| 133 | + |
| 134 | + |
| 135 | +.. example:: |
| 136 | + :title: Getting information on a vector dataset as JSON |
| 137 | + |
| 138 | + .. code-block:: python |
| 139 | +
|
| 140 | + from osgeo import gdal |
| 141 | +
|
| 142 | + gdal.UseExceptions() |
| 143 | + alg = gdal.Run("raster", "info", input="poly.gpkg"}) |
| 144 | + info_as_dict = alg.Output() |
| 145 | +
|
| 146 | +
|
| 147 | +.. example:: |
| 148 | + :title: Converting a shapefile to a GeoPackage |
| 149 | + |
| 150 | + .. code-block:: python |
81 | 151 |
|
82 |
| - from osgeo import gdal |
| 152 | + from osgeo import gdal |
83 | 153 |
|
84 |
| - gdal.UseExceptions() |
85 |
| - with gdal.run(["raster", "convert"], {"input": "in.shp", "output": "out.gpkg", "overwrite": True}): |
86 |
| - pass |
| 154 | + gdal.UseExceptions() |
| 155 | + gdal.Run("raster", "convert", input="in.shp", output="out.gpkg", overwrite=True) |
0 commit comments