Struct gdal::Dataset

source ·
pub struct Dataset { /* private fields */ }
Expand description

Wrapper around a GDALDataset object.

Represents both a vector dataset containing a collection of layers; and a raster dataset containing a collection of raster-bands.

Implementations

Returns the wrapped C pointer

Safety

This method returns a raw C pointer

Open a dataset at the given path with default options.

Open a dataset with extended options. See GDALOpenEx.

Flush all write cached data to disk.

See GDALFlushCache.

Creates a new Dataset by wrapping a C pointer

Safety

This method operates on a raw C pointer

Fetch the projection definition string for this dataset.

Set the projection reference string for this dataset.

Get the spatial reference system for this dataset.

Set the spatial reference system for this dataset.

Fetch the driver to which this dataset relates.

Fetch a band object for a dataset.

Applies to raster datasets, and fetches the rasterband at the given 1-based index.

Opens the root group of a multi-dim GDAL raster

Note

You must have opened the dataset with the GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER flag in order for it to work.

Builds overviews for the current Dataset. See GDALBuildOverviews.

Arguments
  • resampling - resampling method, as accepted by GDAL, e.g. "CUBIC"
  • overviews - list of overview decimation factors, e.g. &[2, 4, 8, 16, 32]
  • bands - list of bands to build the overviews for, or empty for all bands

Get the number of layers in this dataset.

Fetch a layer by index.

Applies to vector datasets, and fetches by the given 0-based index.

Fetch a layer by index.

Applies to vector datasets, and fetches by the given 0-based index.

Fetch a layer by name.

Fetch a layer by name.

Returns an iterator over the layers of the dataset.

Fetch the number of raster bands on this dataset.

Returns the raster dimensions: (width, height).

Creates a new layer. The LayerOptions struct implements Default, so you only need to specify those options that deviate from the default.

Examples

Create a new layer with an empty name, no spatial reference, and unknown geometry type:

let blank_layer = dataset.create_layer(Default::default()).unwrap();

Create a new named line string layer using WGS84:

let roads = dataset.create_layer(LayerOptions {
    name: "roads",
    srs: Some(&SpatialRef::from_epsg(4326).unwrap()),
    ty: gdal_sys::OGRwkbGeometryType::wkbLineString,
    ..Default::default()
}).unwrap();

Affine transformation called geotransformation.

This is like a linear transformation preserves points, straight lines and planes. Also, sets of parallel lines remain parallel after an affine transformation.

Arguments
  • transformation - coeficients of transformations

x-coordinate of the top-left corner pixel (x-offset) width of a pixel (x-resolution) row rotation (typically zero) y-coordinate of the top-left corner pixel column rotation (typically zero) height of a pixel (y-resolution, typically negative)

Get affine transformation coefficients.

x-coordinate of the top-left corner pixel (x-offset) width of a pixel (x-resolution) row rotation (typically zero) y-coordinate of the top-left corner pixel column rotation (typically zero) height of a pixel (y-resolution, typically negative)

For datasources which support transactions, this creates a transaction.

Because the transaction implements DerefMut, it can be used in place of the original Dataset to make modifications. All changes done after the start of the transaction are applied to the datasource when commit is called. They may be canceled by calling rollback instead, or by dropping the Transaction without calling commit.

Depending on the driver, using a transaction can give a huge performance improvement when creating a lot of geometry at once. This is because the driver doesn’t need to commit every feature to disk individually.

If starting the transaction fails, this function will return OGRErr::OGRERR_FAILURE. For datasources that do not support transactions, this function will always return OGRErr::OGRERR_UNSUPPORTED_OPERATION.

Limitations:

  • Datasources which do not support efficient transactions natively may use less efficient emulation of transactions instead; as of GDAL 3.1, this only applies to the closed-source FileGDB driver, which (unlike OpenFileGDB) is not available in a GDAL build by default.

  • At the time of writing, transactions only apply on vector layers.

  • Nested transactions are not supported.

  • If an error occurs after a successful start_transaction, the whole transaction may or may not be implicitly canceled, depending on the driver. For example, the PG driver will cancel it, but the SQLite and GPKG drivers will not.

Example:

fn create_point_grid(dataset: &mut Dataset) -> gdal::errors::Result<()> {
    use gdal::vector::Geometry;

    // Start the transaction.
    let mut txn = dataset.start_transaction()?;

    let mut layer = txn.create_layer(LayerOptions {
        name: "grid",
        ty: gdal_sys::OGRwkbGeometryType::wkbPoint,
        ..Default::default()
    })?;
    for y in 0..100 {
        for x in 0..100 {
            let wkt = format!("POINT ({} {})", x, y);
            layer.create_feature(Geometry::from_wkt(&wkt)?)?;
        }
    }

    // We got through without errors. Commit the transaction and return.
    txn.commit()?;
    Ok(())
}

Execute a SQL query against the Dataset. It is equivalent to calling GDALDatasetExecuteSQL. Returns a sql::ResultSet, which can be treated just as any other Layer.

Queries such as ALTER TABLE, CREATE INDEX, etc. have no sql::ResultSet, and return None, which is distinct from an empty sql::ResultSet.

Arguments
Example
use gdal::vector::sql;
use gdal::vector::LayerAccess;

let ds = Dataset::open(Path::new("fixtures/roads.geojson")).unwrap();
let query = "SELECT kind, is_bridge, highway FROM roads WHERE highway = 'pedestrian'";
let mut result_set = ds.execute_sql(query, None, sql::Dialect::DEFAULT).unwrap().unwrap();

assert_eq!(10, result_set.feature_count());

for feature in result_set.features() {
    let highway = feature
        .field("highway")
        .unwrap()
        .unwrap()
        .into_string()
        .unwrap();

    assert_eq!("pedestrian", highway);
}

Trait Implementations

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
Converts to this type from the input type.
Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.