osgeo.ogr module

osgeo.ogr.ApproximateArcAngles(double dfCenterX, double dfCenterY, double dfZ, double dfPrimaryRadius, double dfSecondaryAxis, double dfRotation, double dfStartAngle, double dfEndAngle, double dfMaxAngleStepSizeDegrees) Geometry
class osgeo.ogr.ArrowArray(*args)

Bases: object

Proxy of C++ ArrowArray class.

GetChildrenCount(ArrowArray self) GIntBig
GetLength(ArrowArray self) GIntBig
property thisown

The membership flag

class osgeo.ogr.ArrowArrayStream(*args, **kwargs)

Bases: object

Proxy of C++ ArrowArrayStream class.

GetNextRecordBatch(ArrowArrayStream self, char ** options=None) ArrowArray
GetSchema(ArrowArrayStream self) ArrowSchema
property thisown

The membership flag

class osgeo.ogr.ArrowSchema(*args)

Bases: object

Proxy of C++ ArrowSchema class.

GetChild(ArrowSchema self, int iChild) ArrowSchema
GetChildrenCount(ArrowSchema self) GIntBig
GetName(ArrowSchema self) char const *
property thisown

The membership flag

osgeo.ogr.BuildPolygonFromEdges(Geometry hLineCollection, int bBestEffort=0, int bAutoClose=0, double dfTolerance=0) Geometry
osgeo.ogr.CreateGeomCoordinatePrecision() GeomCoordinatePrecision
osgeo.ogr.CreateRangeFieldDomainDateTime(char const * name, char const * description, char const * min, bool minIsInclusive, char const * max, double maxIsInclusive) FieldDomain
class osgeo.ogr.DataSource(*args, **kwargs)

Bases: MajorObject

Python proxy of a vector GDALDataset.

Since GDAL 3.8, a DataSource can be used as a context manager. When exiting the context, the DataSource will be closed and features will be written to disk.

AbortSQL(DataSource self) OGRErr
Close(DataSource self) CPLErr

Closes opened dataset and releases allocated resources.

This method can be used to force the dataset to close when one more references to the dataset are still reachable. If Close is never called, the dataset will be closed automatically during garbage collection.

CommitTransaction(DataSource self) OGRErr
CopyLayer(DataSource self, Layer src_layer, char const * new_name, char ** options=None) Layer

OGRLayerH OGR_DS_CopyLayer(OGRDataSourceH hDS, OGRLayerH hSrcLayer, const char *pszNewName, char **papszOptions)

Duplicate an existing layer.

This function creates a new layer, duplicate the field definitions of the source layer and then duplicate each features of the source layer. The papszOptions argument can be used to control driver specific creation options. These options are normally documented in the format specific documentation. The source layer may come from another dataset.

Deprecated Use GDALDatasetCopyLayer() in GDAL 2.0

Parameters:
  • hDS -- handle to the data source where to create the new layer

  • hSrcLayer -- handle to the source layer.

  • pszNewName -- the name of the layer to create.

  • papszOptions -- a StringList of name=value options. Options are driver specific.

Returns:

a handle to the layer, or NULL if an error occurs.

Return type:

OGRLayerH

CreateLayer(DataSource self, char const * name, SpatialReference srs=None, OGRwkbGeometryType geom_type=wkbUnknown, char ** options=None) Layer

OGRLayerH OGR_DS_CreateLayer(OGRDataSourceH hDS, const char *pszName, OGRSpatialReferenceH hSpatialRef, OGRwkbGeometryType eType, char **papszOptions)

This function attempts to create a new layer on the data source with the indicated name, coordinate system, geometry type.

The papszOptions argument can be used to control driver specific creation options. These options are normally documented in the format specific documentation.

Deprecated Use GDALDatasetCreateLayer() in GDAL 2.0

Parameters:
  • hDS -- The dataset handle.pszName: the name for the new layer. This should ideally not match any existing layer on the datasource.

  • hSpatialRef -- handle to the coordinate system to use for the new layer, or NULL if no coordinate system is available. The driver might only increase the reference counter of the object to take ownership, and not make a full copy, so do not use OSRDestroySpatialReference(), but OSRRelease() instead when you are done with the object.

  • eType -- the geometry type for the layer. Use wkbUnknown if there are no constraints on the types geometry to be written.

  • papszOptions -- a StringList of name=value options. Options are driver specific, and driver information can be found at the following url:http://www.gdal.org/ogr_formats.html

Returns:

NULL is returned on failure, or a new OGRLayer handle on success.

Return type:

OGRLayerH

DeleteLayer(DataSource self, value) OGRErr

Delete the indicated layer from the datasource.

For more details: OGR_DS_DeleteLayer()

Parameters:

value (str | int) -- index or name of the layer to delete.

Returns:

osgeo.ogr.OGRERR_NONE on success, or osgeo.ogr.OGRERR_UNSUPPORTED_OPERATION if deleting layers is not supported for this datasource.

Return type:

int

Dereference()

For backwards compatibility only.

Destroy()

Once called, self has effectively been destroyed. Do not access. For backwards compatibility only

ExecuteSQL(self, statement, spatialFilter: ogr.Geometry = None, dialect: str | None = '', keep_ref_on_ds=False) ogr.Layer

Execute a SQL statement against the dataset

The result of a SQL query is:
  • None (or an exception if exceptions are enabled) for statements that are in error

  • or None for statements that have no results set,

  • or a ogr.Layer handle representing a results set from the query.

Note that this ogr.Layer is in addition to the layers in the data store and must be released with ReleaseResultSet() before the data source is closed (destroyed).

Starting with GDAL 3.7, this method can also be used as a context manager, as a convenient way of automatically releasing the returned result layer.

For more information on the SQL dialect supported internally by OGR review the OGR SQL document (OGR SQL dialect and SQLITE SQL dialect) Some drivers (i.e. Oracle and PostGIS) pass the SQL directly through to the underlying RDBMS.

The SQLITE dialect can also be used (SQL SQLite dialect)

Parameters:
  • statement -- the SQL statement to execute (e.g "SELECT * FROM layer")

  • spatialFilter -- a geometry which represents a spatial filter. Can be None

  • dialect -- allows control of the statement dialect. If set to None or empty string, the OGR SQL engine will be used, except for RDBMS drivers that will use their dedicated SQL engine, unless OGRSQL is explicitly passed as the dialect. The SQLITE dialect can also be used.

  • keep_ref_on_ds -- whether the returned layer should keep a (strong) reference on the current dataset. Cf example 2 for a use case.

Returns:

a ogr.Layer containing the results of the query, that will be automatically released when the context manager goes out of scope.

Return type:

ogr.Layer

Examples

  1. Use as a context manager:

>>> with ds.ExecuteSQL("SELECT * FROM layer") as lyr:
...     print(lyr.GetFeatureCount())
  1. Use keep_ref_on_ds=True to return an object that keeps a reference to its dataset:

>>> def get_sql_lyr():
...     return gdal.OpenEx("test.shp").ExecuteSQL("SELECT * FROM test", keep_ref_on_ds=True)
...
... with get_sql_lyr() as lyr:
...     print(lyr.GetFeatureCount())
FlushCache(DataSource self)
GetDriver(DataSource self) Driver

OGRSFDriverH OGR_DS_GetDriver(OGRDataSourceH hDS)

Returns the driver that the dataset was opened with.

NOTE: Starting with GDAL 2.0, it is NOT safe to cast the returned handle to OGRSFDriver*. If a C++ object is needed, the handle should be cast to GDALDriver*.

Deprecated Use GDALGetDatasetDriver() in GDAL 2.0

Parameters:

hDS -- handle to the datasource

Returns:

NULL if driver info is not available, or pointer to a driver owned by the OGRSFDriverManager.

Return type:

OGRSFDriverH

GetLayer(iLayer=0)

Return the layer given an index or a name

GetLayerByIndex(DataSource self, int index=0) Layer
GetLayerByName(DataSource self, char const * layer_name) Layer

OGRLayerH OGR_DS_GetLayerByName(OGRDataSourceH hDS, const char *pszLayerName)

Fetch a layer by name.

The returned layer remains owned by the OGRDataSource and should not be deleted by the application.

Deprecated Use GDALDatasetGetLayerByName() in GDAL 2.0

Parameters:
  • hDS -- handle to the data source from which to get the layer.

  • pszLayerName -- Layer the layer name of the layer to fetch.

Returns:

a handle to the layer, or NULL if the layer is not found or an error occurs.

Return type:

OGRLayerH

GetLayerCount(DataSource self) int

int OGR_DS_GetLayerCount(OGRDataSourceH hDS)

Get the number of layers in this data source.

Deprecated Use GDALDatasetGetLayerCount() in GDAL 2.0

Parameters:

hDS -- handle to the data source from which to get the number of layers.

Returns:

layer count.

Return type:

int

GetName(DataSource self) char const *

const char* OGR_DS_GetName(OGRDataSourceH hDS)

Returns the name of the data source.

This string should be sufficient to open the data source if passed to the same OGRSFDriver that this data source was opened with, but it need not be exactly the same string that was used to open the data source. Normally this is a filename.

Deprecated Use GDALGetDescription() in GDAL 2.0

Parameters:

hDS -- handle to the data source to get the name from.

Returns:

pointer to an internal name string which should not be modified or freed by the caller.

Return type:

str

GetRefCount(DataSource self) int

int OGR_DS_GetRefCount(OGRDataSourceH hDataSource)

GetStyleTable(DataSource self) StyleTable

OGRStyleTableH OGR_DS_GetStyleTable(OGRDataSourceH hDS)

Get style table.

GetSummaryRefCount(DataSource self) int

int OGR_DS_GetSummaryRefCount(OGRDataSourceH hDataSource)

Reference()

For backwards compatibility only.

Release()

Once called, self has effectively been destroyed. Do not access. For backwards compatibility only

ReleaseResultSet(self, sql_lyr: ogr.Layer)

Release ogr.Layer returned by ExecuteSQL() (when not called as an execution manager)

The sql_lyr object is invalidated after this call.

Parameters:

sql_lyr -- ogr.Layer got with ExecuteSQL()

RollbackTransaction(DataSource self) OGRErr
SetStyleTable(DataSource self, StyleTable table)

void OGR_DS_SetStyleTable(OGRDataSourceH hDS, OGRStyleTableH hStyleTable)

Set style table.

StartTransaction(DataSource self, int force=FALSE) OGRErr
SyncToDisk(DataSource self) OGRErr

OGRErr OGR_DS_SyncToDisk(OGRDataSourceH hDS)

Flush pending changes to disk.

See GDALDataset::FlushCache()

TestCapability(DataSource self, char const * cap) bool

int OGR_DS_TestCapability(OGRDataSourceH hDS, const char *pszCapability)

Test if capability is available.

One of the following data source capability names can be passed into this function, and a TRUE or FALSE value will be returned indicating whether or not the capability is available for this object.

ODsCCreateLayer: True if this datasource can create new layers.

ODsCDeleteLayer: True if this datasource can delete existing layers.

ODsCCreateGeomFieldAfterCreateLayer: True if the layers of this datasource support CreateGeomField() just after layer creation.

ODsCCurveGeometries: True if this datasource supports writing curve geometries. (GDAL 2.0). In that case, OLCCurveGeometries must also be declared in layers of that dataset.

The #define macro forms of the capability names should be used in preference to the strings themselves to avoid misspelling.

Deprecated Use GDALDatasetTestCapability() in GDAL 2.0

Parameters:
  • hDS -- handle to the data source against which to test the capability.

  • pszCapability -- the capability to test.

Returns:

TRUE if capability available otherwise FALSE.

Return type:

int

property name

p.q(const).char

Type:

name

property thisown

The membership flag

osgeo.ogr.DontUseExceptions()

Disable exceptions in all GDAL related modules (osgeo.gdal, osgeo.ogr, osgeo.osr, osgeo.gnm). Note: prior to GDAL 3.7, this only affected the calling module

class osgeo.ogr.Driver(*args, **kwargs)

Bases: MajorObject

Proxy of C++ OGRDriverShadow class.

CopyDataSource(Driver self, DataSource copy_ds, char const * utf8_path, char ** options=None) DataSource
CreateDataSource(Driver self, char const * utf8_path, char ** options=None) DataSource
DeleteDataSource(Driver self, char const * utf8_path) int
Deregister(Driver self)
GetName(Driver self) char const *
Open(Driver self, char const * utf8_path, int update=0) DataSource
Register(Driver self)
TestCapability(Driver self, char const * cap) bool
property name

p.q(const).char

Type:

name

property thisown

The membership flag

class osgeo.ogr.ExceptionMgr(useExceptions=True)

Bases: object

Context manager to manage Python Exception state for GDAL/OGR/OSR/GNM.

Separate exception state is maintained for each module (gdal, ogr, etc), and this class appears independently in all of them. This is built in top of calls to the older UseExceptions()/DontUseExceptions() functions.

Example:

>>> print(gdal.GetUseExceptions())
0
>>> with gdal.ExceptionMgr():
...     # Exceptions are now in use
...     print(gdal.GetUseExceptions())
1
>>>
>>> # Exception state has now been restored
>>> print(gdal.GetUseExceptions())
0
osgeo.ogr.GeneralCmdLineProcessor(char ** papszArgv, int nOptions=0) char **
class osgeo.ogr.GeomCoordinatePrecision(*args, **kwargs)

Bases: object

Proxy of C++ OGRGeomCoordinatePrecisionShadow class.

GetFormatSpecificOptions(GeomCoordinatePrecision self, char const * formatName) char **
GetFormats(GeomCoordinatePrecision self) char **
GetMResolution(GeomCoordinatePrecision self) double
GetXYResolution(GeomCoordinatePrecision self) double
GetZResolution(GeomCoordinatePrecision self) double
Set(GeomCoordinatePrecision self, double xyResolution, double zResolution, double mResolution)
SetFormatSpecificOptions(GeomCoordinatePrecision self, char const * formatName, char ** formatSpecificOptions)
SetFromMeter(GeomCoordinatePrecision self, SpatialReference srs, double xyMeterResolution, double zMeterResolution, double mResolution)
property thisown

The membership flag

class osgeo.ogr.GeomTransformer(*args)

Bases: object

Proxy of C++ OGRGeomTransformerShadow class.

Transform(GeomTransformer self, Geometry src_geom) Geometry
property thisown

The membership flag

osgeo.ogr.GetDriver(int driver_number) Driver
osgeo.ogr.GetDriverByName(char const * name) Driver
osgeo.ogr.GetDriverCount() int
osgeo.ogr.GetGEOSVersionMajor() int
osgeo.ogr.GetGEOSVersionMicro() int
osgeo.ogr.GetGEOSVersionMinor() int
osgeo.ogr.GetNonLinearGeometriesEnabledFlag() int
osgeo.ogr.GetOpenDS(int ds_number) DataSource
osgeo.ogr.GetOpenDSCount() int
osgeo.ogr.GetUseExceptions() int
class osgeo.ogr.MajorObject(*args, **kwargs)

Bases: object

Proxy of C++ GDALMajorObjectShadow class.

GetDescription(MajorObject self) char const *
GetMetadata(domain='')
GetMetadataDomainList(MajorObject self) char **
GetMetadataItem(MajorObject self, char const * pszName, char const * pszDomain="") char const *
GetMetadata_Dict(MajorObject self, char const * pszDomain="") char **
GetMetadata_List(MajorObject self, char const * pszDomain="") char **
SetDescription(MajorObject self, char const * pszNewDesc)
SetMetadata(MajorObject self, char ** papszMetadata, char const * pszDomain="") CPLErr
SetMetadata(MajorObject self, char * pszMetadataString, char const * pszDomain="") CPLErr
SetMetadataItem(MajorObject self, char const * pszName, char const * pszValue, char const * pszDomain="") CPLErr
property thisown

The membership flag

osgeo.ogr.Open(char const * utf8_path, int update=0) DataSource
osgeo.ogr.OpenShared(char const * utf8_path, int update=0) DataSource
class osgeo.ogr.PreparedGeometry(*args, **kwargs)

Bases: object

Proxy of C++ OGRPreparedGeometryShadow class.

Contains(PreparedGeometry self, Geometry otherGeom) bool
Intersects(PreparedGeometry self, Geometry otherGeom) bool
property thisown

The membership flag

osgeo.ogr.RegisterAll()
osgeo.ogr.SetGenerate_DB2_V72_BYTE_ORDER(int bGenerate_DB2_V72_BYTE_ORDER) OGRErr
osgeo.ogr.SetNonLinearGeometriesEnabledFlag(int bFlag)
osgeo.ogr.TermProgress_nocb(double dfProgress, char const * pszMessage=None, void * pData=None) int
osgeo.ogr.UseExceptions()

Enable exceptions in all GDAL related modules (osgeo.gdal, osgeo.ogr, osgeo.osr, osgeo.gnm). Note: prior to GDAL 3.7, this only affected the calling module