|
| 1 | +.. _gdal_cli_from_python: |
| 2 | + |
| 3 | +================================================================================ |
| 4 | +How to use "gdal" CLI algorithms from Python |
| 5 | +================================================================================ |
| 6 | + |
| 7 | +Raster commands |
| 8 | +--------------- |
| 9 | + |
| 10 | +* Getting information on a raster dataset as JSON |
| 11 | + |
| 12 | +.. code-block:: python |
| 13 | +
|
| 14 | + from osgeo import gdal |
| 15 | + import json |
| 16 | +
|
| 17 | + gdal.UseExceptions() |
| 18 | + alg = gdal.GetGlobalAlgorithmRegistry()["raster"]["info"] |
| 19 | + alg["input"] = "byte.tif" |
| 20 | + alg.Run() |
| 21 | + info = json.loads(alg["output-string"]) |
| 22 | + print(info) |
| 23 | +
|
| 24 | +
|
| 25 | +* Converting a georeferenced netCDF file to cloud-optimized GeoTIFF |
| 26 | + |
| 27 | +.. code-block:: python |
| 28 | +
|
| 29 | + from osgeo import gdal |
| 30 | + gdal.UseExceptions() |
| 31 | + alg = gdal.GetGlobalAlgorithmRegistry()["raster"]["convert"] |
| 32 | + alg["input"] = "in.nc" |
| 33 | + alg["output"] = "out.tif" |
| 34 | + alg["output-format"] = "COG" |
| 35 | + alg["overwrite"] = True # if the output file may exist |
| 36 | + alg.Run() |
| 37 | + alg.Finalize() # ensure output dataset is closed |
| 38 | +
|
| 39 | +or |
| 40 | + |
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + from osgeo import gdal |
| 44 | + gdal.UseExceptions() |
| 45 | + alg = gdal.GetGlobalAlgorithmRegistry()["raster"]["convert"] |
| 46 | + alg.ParseRunAndFinalize(["--input=in.nc", "--output-format=COG", "--output=out.tif", "--overwrite"]) |
| 47 | +
|
| 48 | +
|
| 49 | +* Reprojecting a GeoTIFF file to a Deflate compressed tiled GeoTIFF file |
| 50 | + |
| 51 | +.. code-block:: python |
| 52 | +
|
| 53 | + from osgeo import gdal |
| 54 | + gdal.UseExceptions() |
| 55 | + alg = gdal.GetGlobalAlgorithmRegistry()["raster"]["reproject"] |
| 56 | + alg["input"] = "in.tif" |
| 57 | + alg["output"] = "out.tif" |
| 58 | + alg["dst-crs"] = "EPSG:4326" |
| 59 | + alg["creation-options"] = ["TILED=YES", "COMPRESS=DEFLATE"] |
| 60 | + alg["overwrite"] = True # if the output file may exist |
| 61 | + alg.Run() |
| 62 | + alg.Finalize() # ensure output dataset is closed |
| 63 | +
|
| 64 | +or |
| 65 | + |
| 66 | +.. code-block:: python |
| 67 | +
|
| 68 | + from osgeo import gdal |
| 69 | + gdal.UseExceptions() |
| 70 | + alg = gdal.GetGlobalAlgorithmRegistry()["raster"]["reproject"] |
| 71 | + alg.ParseRunAndFinalize(["--input=in.tif", "--output=out.tif", "--dst-crs=EPSG:4326", "--co=TILED=YES,COMPRESS=DEFLATE", "--overwrite"]) |
| 72 | +
|
| 73 | +
|
| 74 | +* Reprojecting a (possibly in-memory) dataset to a in-memory dataset |
| 75 | + |
| 76 | +.. code-block:: python |
| 77 | +
|
| 78 | + from osgeo import gdal |
| 79 | + gdal.UseExceptions() |
| 80 | + alg = gdal.GetGlobalAlgorithmRegistry()["raster"]["reproject"] |
| 81 | + alg["input"] = input_ds |
| 82 | + alg["output"] = "dummy_name" |
| 83 | + alg["output-format"] = "MEM" |
| 84 | + alg["dst-crs"] = "EPSG:4326" |
| 85 | + alg.Run() |
| 86 | + output_ds = alg["output"].GetDataset() |
| 87 | +
|
| 88 | +
|
| 89 | +
|
| 90 | +Vector commands |
| 91 | +--------------- |
| 92 | + |
| 93 | +* Getting information on a vector dataset as JSON |
| 94 | + |
| 95 | +.. code-block:: python |
| 96 | +
|
| 97 | + from osgeo import gdal |
| 98 | + import json |
| 99 | +
|
| 100 | + gdal.UseExceptions() |
| 101 | + alg = gdal.GetGlobalAlgorithmRegistry()["vector"]["info"] |
| 102 | + alg["input"] = "poly.gpkg" |
| 103 | + alg.Run() |
| 104 | + info = json.loads(alg["output-string"]) |
| 105 | + print(info) |
| 106 | +
|
| 107 | +
|
| 108 | +* Converting a shapefile to a GeoPackage |
| 109 | + |
| 110 | +.. code-block:: python |
| 111 | +
|
| 112 | + from osgeo import gdal |
| 113 | + gdal.UseExceptions() |
| 114 | + alg = gdal.GetGlobalAlgorithmRegistry()["vector"]["convert"] |
| 115 | + alg["input"] = "in.shp" |
| 116 | + alg["output"] = "out.gpkg" |
| 117 | + alg["overwrite"] = True # if the output file may exist |
| 118 | + alg.Run() |
| 119 | + alg.Finalize() # ensure output dataset is closed |
| 120 | +
|
| 121 | +or |
| 122 | + |
| 123 | +.. code-block:: python |
| 124 | +
|
| 125 | + from osgeo import gdal |
| 126 | + gdal.UseExceptions() |
| 127 | + alg = gdal.GetGlobalAlgorithmRegistry()["vector"]["convert"] |
| 128 | + alg.ParseRunAndFinalize(["--input=in.shp", "--output=out.gpkg", "--overwrite"]) |
0 commit comments