Welcome to the Python GDAL/OGR Cookbook!¶
This cookbook has simple code snippets on how to use the Python GDAL/OGR API. The web site is a project at GitHub and served by Github Pages. If you find missing recipes or mistakes in existing recipes please add an issue to the issue tracker.
For a detailed description of the whole Python GDAL/OGR API, see the useful API docs.
We heavily relied on Chris Garrard’s excellent Geoprocessing with Python using Open Source GIS and the official GDAL/OGR Python documentation. Another great source of examples is OGR’s autotest directory.
- GDAL/OGR General
- Geometry
- Create a Point
- Create a LineString
- Create a Polygon
- Create a Polygon with holes
- Create a MultiPoint
- Create a MultiLineString
- Create a MultiPolygon
- Create a GeometryCollection
- Create Geometry from WKT
- Create Geometry from GeoJSON
- Create Geometry from GML
- Create Geometry from WKB
- Count Points in a Geometry
- Count Geometries in a Geometry
- Iterate over Geometries in a Geometry
- Iterate over Points in a Geometry
- Buffer a Geometry
- Calculate Envelope of a Geometry
- Calculate the Area of a Geometry
- Calculate the Length of a Geometry
- Get the geometry type (as a string) from a Geometry
- Calculate intersection between two Geometries
- Calculate union between two Geometries
- Write Geometry to GeoJSON
- Write Geometry to WKT
- Write Geometry to KML
- Write Geometry to WKB
- Force polygon to multipolygon
- Quarter polygon and create centroids
- Vector Layers
- Delete a file
- Is Ogr Installed
- View Auto Generated Ogr Help
- Get List of Ogr Drivers Alphabetically (A- Z)
- Is Ogr Driver Available by Driver Name
- Force Ogr Use Named Driver
- Get Shapefile Feature Count
- Get All PostGIS layers in a PostgreSQL Database
- Get PostGIS Layer Feature Count By Layer Name
- Get all layers in an Esri File GeoDataBase
- Load data to memory
- Iterate over Features
- Get Geometry from each Feature in a Layer
- Filter by attribute
- Spatial Filter
- Get Shapefile Fields - Get the user defined fields
- Get Shapefile Fields and Types - Get the user defined fields
- Get PostGIS Layer Fields - Get the user defined fields
- Get PostGIS Layer Fields and Types - Get the user defined fields
- Get a Layer’s Capabilities
- Get WFS layers and iterate over features
- Set HTTP Proxy options before fetching a web datasource
- Read a CSV of Coordinates as an OGRVRTLayer
- Create a new Layer from the extent of an existing Layer
- Save the convex hull of all geometry from an input Layer to an output Layer
- Save centroids of input Layer to an output Layer
- Create a New Shapefile and Add Data
- Create a PostGIS table from WKT
- Filter and Select Input Shapefile to New Output Shapefile Like ogr2ogr CLI
- Merge OGR Layers
- Get a list of the street names in a OSM file
- Create fishnet grid
- Convert polygon shapefile to line shapefile
- Create point shapefile with attribute data
- Create buffer
- Convert vector layer to array
- Convert polygon to points
- Raster Layers
- Close a raster dataset
- Get Raster Metadata
- Get Raster Band
- Loop Through All Raster Bands
- Get Raster Band Information
- Polygonize a Raster Band
- Convert an OGR File to a Raster
- Clip a GeoTiff with Shapefile
- Calculate zonal statistics
- Raster to vector line
- Create raster from array
- Create least cost path
- Replace No Data Value of Raster with new value
- Projection
- API Tricks and Trapdoors