Sample scripts

The following are sample scripts intended to give an idea of how to use GDAL's Python interface. Please feel free to use them in your applications.

From GDAL 3.2, Python utility scripts Programs are located inside the osgeo_utils module. From GDAL 3.3, Python sample scripts are located inside the osgeo_utils.samples sub-module.

Warning

Several of the sample scripts on this page have not been updated since they were created and may use outdated coding styles or deprecated APIs.

Python Sample Scripts

The scripts provide examples of both raster and vector usage of the GDAL Python API.

  • assemblepoly.py: Script demonstrates how to assemble polygons from arcs. Demonstrates various aspects of OGR Python API.

  • build_jp2_from_xml.py: Build a JPEG2000 file from the XML structure dumped by dump_jp2.py.

  • classify.py: Demonstrates using numpy for simple range based classification of an image. This is only an example that has stuff hardcoded.

  • crs2crs2grid.py: A script to produce PROJ.4 grid shift files from HTDP program.

  • densify.py: Densifies linestrings by a tolerance.

  • dump_jp2.py: Dump JPEG2000 file structure.

  • epsg_tr.py: Script to create WKT and PROJ.4 dictionaries for EPSG GCS/PCS codes.

  • esri2wkt.py: Simple command line program for translating ESRI .prj files into WKT.

  • fft.py: Script to perform forward and inverse two-dimensional fast Fourier transform.

  • fix_gpkg.py: Fix invalid GeoPackage files.

  • gcps2ogr.py: Outputs GDAL GCPs as OGR points

  • gcps2vec.py: Convert GCPs to a point layer.

  • gcps2wld.py: Translate the set of GCPs on a file into first order approximation in world file format.

  • gdalbuildvrtofvrt.py: Create a 2-level hierarchy of VRTs for very large collections.

  • gdalchksum.py: Application to checksum a GDAL image file.

  • gdalcopyproj.py: Duplicate the geotransform, projection and/or GCPs from one raster dataset to another, which can be useful after performing image manipulations with other software that ignores or discards georeferencing metadata.

  • gdalfilter.py: Example script for applying kernel based filters to an image using GDAL. Demonstrates use of virtual files as an intermediate representation.

  • gdalident.py: Application to identify files by format.

  • gdalimport.py: Import a GDAL supported file to Tiled GeoTIFF, and build overviews.

  • gdal_auth.py: Application for Google web service authentication.

  • gdal_create_pdf.py: Create a PDF from a XML composition file.

  • gdal_lut.py: Read a LUT from a text file, and apply it to an image. Sort of a '1 band' version of pct2rgb.

  • gdal_minmax_location.py: returns the location where min/max values of a raster are hit.

  • gdal_mkdir.py: Create a virtual directory.

  • gdal_remove_towgs84.py: Remove TOWGS84[] clause from dataset SRS definitions.

  • gdal_rm.py: Delete a virtual file.

  • gdal_rmdir.py: Delete a virtual directory.

  • get_soundg.py: Script to copy the SOUNDG layer from an S-57 file to a Shapefile, splitting up features with MULTIPOINT geometries into many POINT features, and appending the point elevations as an attribute.

  • histrep.py: Module to extract data from many rasters into one output.

  • jpeg_in_tiff_extract.py: Extract a JPEG file from a JPEG-in-TIFF tile/strip.

  • load2odbc.py: Load ODBC table to an ODBC datastore. Uses direct SQL since the ODBC driver is read-only for OGR.

  • loslas2ntv2.py: Translate one or many LOS/LAS sets into an NTv2 datum shift grid file.

  • magphase.py: Example script computing magnitude and phase images from a complex image.

  • make_fuzzer_friendly_archive.py: Make fuzzer friendly archive (only works in DEBUG mode).

  • mkgraticule.py: Produce a graticule (grid) dataset.

  • ogr2vrt.py: Produce a graticule (grid) dataset.

  • ogrupdate.py: Update a target datasource with the features of a source datasource. Contrary to ogr2ogr, this script tries to match features between the datasources, to decide whether to create a new feature, or to update an existing one.

  • ogr_build_junction_table.py: Create junction tables for layers coming from GML datasources that reference other objects in _href fields

  • ogr_dispatch.py: Dispatch features into layers according to the value of some fields or the geometry type.

  • rel.py: Script to produce a shaded relief image from the elevation data. (similar functionality in gdaldem now)

  • tigerpoly.py: Script demonstrating how to assemble polygons from arcs in TIGER/Line datasource, writing results to a newly created shapefile.

  • tile_extent_from_raster.py: Generate the extent of each raster tile in a overview as a vector layer.

  • tolatlong.py: Script to read coordinate system and geotransformation matrix from input file and report latitude/longitude coordinates for the specified pixel.

  • validate_cloud_optimized_geotiff.py: Validate Cloud Optimized GeoTIFF file structure.

  • validate_geoparquet.py: Test compliance of GeoParquet file.

  • validate_gpkg.py: Test compliance of GeoPackage database w.r.t GeoPackage spec.

  • validate_jp2.py: Validate JPEG2000 file structure.

  • val_repl.py: Script to replace specified values from the input raster file with the new ones. May be useful in cases when you don't like value, used for NoData indication and want replace it with other value. Input file remains unchanged, results stored in other file.

  • vec_tr.py: Example of applying some algorithm to all the geometries in the file, such as a fixed offset.

  • vec_tr_spat.py: Example of using Intersect() to filter based on only those features that truly intersect a given rectangle. Easily extended to general polygon!

  • wcs_virtds_params.py: Generates MapServer WCS layer definition from a tileindex with mixed SRS.

Legacy Python Sample Scripts

The scripts below have been replaced by updated GDAL utilities (programs) that include additional functionality and improved documentation. This list is provided to help you locate equivalent functionality when upgrading GDAL.

Warning

These scripts are provided for reference only and are not guaranteed to follow current best practices. They may be removed in future versions of GDAL.

The following scripts have been replaced by equivalent functionality in GDAL, and have been removed from the repository.

  • hsv_merge.py: Merge greyscale image into RGB image as intensity in HSV space. Equivalent functionality available in gdal raster blend.

  • gdal_ls.py: Display the list of files in a virtual directory, like /vsicurl or /vsizip. Equivalent functionality available in gdal vsi list.

  • gdal_cp.py: Copy a virtual file. Equivalent functionality available in gdal dataset copy.

  • gdal_vrtmerge.py: Similar to gdal_merge, but produces a VRT file. Equivalent functionality available in gdal raster mosaic.

  • gdal2grd.py: Script to write out ASCII GRD rasters (used in Golden Software Surfer). from any source supported by GDAL. Equivalent functionality available in the GSAG -- Golden Software ASCII Grid File Format driver.

Note

For scripts that have been migrated to the new unified gdal command-line interface, you can run these in Python using gdal.Run. For example:

>>> import os
>>> from osgeo import gdal
>>> os.environ["AWS_NO_SIGN_REQUEST"] = "YES"
>>> gdal.Run("vsi", "list", filename="/vsis3/overturemaps-us-west-2/release/2025-10-22.0/theme=buildings/type=building").Output()
['part-00000-c5e0b5f2-08ff-4192-af19-c572ecc088f1-c000.zstd.parquet', ...]