Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owned layers from a dataset that are Send #238

Merged
merged 7 commits into from Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/read_write_ogr_datetime.rs
@@ -1,3 +1,5 @@
use gdal::vector::LayerAccess;

fn run() -> gdal::errors::Result<()> {
use chrono::Duration;
use gdal::vector::{Defn, Feature, FieldDefn, FieldValue};
Expand Down
2 changes: 1 addition & 1 deletion examples/write_ogr.rs
@@ -1,5 +1,5 @@
use gdal::errors::Result;
use gdal::vector::{Defn, Feature, FieldDefn, FieldValue, Geometry, OGRFieldType};
use gdal::vector::{Defn, Feature, FieldDefn, FieldValue, Geometry, LayerAccess, OGRFieldType};
use gdal::Driver;
use std::fs;

Expand Down
33 changes: 30 additions & 3 deletions src/dataset.rs
Expand Up @@ -12,8 +12,7 @@ use crate::cpl::CslStringList;
use crate::errors::*;
use crate::raster::RasterCreationOption;
use crate::utils::{_last_cpl_err, _last_null_pointer_err, _path_to_c_string, _string};
use crate::vector::sql;
use crate::vector::Geometry;
use crate::vector::{sql, Geometry, OwnedLayer};
use crate::{
gdal_major_object::MajorObject, raster::RasterBand, spatial_ref::SpatialRef, vector::Layer,
Driver, Metadata,
Expand Down Expand Up @@ -453,6 +452,10 @@ impl Dataset {
unsafe { Layer::from_c_layer(self, c_layer) }
}

fn into_child_layer(self, c_layer: OGRLayerH) -> OwnedLayer {
unsafe { OwnedLayer::from_c_layer(self, c_layer) }
}

/// Get the number of layers in this dataset.
pub fn layer_count(&self) -> isize {
(unsafe { gdal_sys::OGR_DS_GetLayerCount(self.c_dataset) }) as isize
Expand All @@ -470,6 +473,18 @@ impl Dataset {
Ok(self.child_layer(c_layer))
}

/// Fetch a layer by index.
///
/// Applies to vector datasets, and fetches by the given
/// _0-based_ index.
pub fn into_layer(self, idx: isize) -> Result<OwnedLayer> {
let c_layer = unsafe { gdal_sys::OGR_DS_GetLayer(self.c_dataset, idx as c_int) };
if c_layer.is_null() {
return Err(_last_null_pointer_err("OGR_DS_GetLayer"));
}
Ok(self.into_child_layer(c_layer))
}

/// Fetch a layer by name.
pub fn layer_by_name(&self, name: &str) -> Result<Layer> {
let c_name = CString::new(name)?;
Expand All @@ -480,6 +495,16 @@ impl Dataset {
Ok(self.child_layer(c_layer))
}

/// Fetch a layer by name.
pub fn into_layer_by_name(self, name: &str) -> Result<OwnedLayer> {
let c_name = CString::new(name)?;
let c_layer = unsafe { gdal_sys::OGR_DS_GetLayerByName(self.c_dataset(), c_name.as_ptr()) };
if c_layer.is_null() {
return Err(_last_null_pointer_err("OGR_DS_GetLayerByName"));
}
Ok(self.into_child_layer(c_layer))
}

/// Returns an iterator over the layers of the dataset.
pub fn layers(&self) -> LayerIterator {
LayerIterator::with_dataset(self)
Expand Down Expand Up @@ -648,6 +673,7 @@ impl Dataset {
///
/// ```
/// # use gdal::{Dataset, LayerOptions};
/// # use gdal::vector::LayerAccess;
/// #
/// fn create_point_grid(dataset: &mut Dataset) -> gdal::errors::Result<()> {
/// use gdal::vector::Geometry;
Expand Down Expand Up @@ -713,6 +739,7 @@ impl Dataset {
/// # use gdal::Dataset;
/// # use std::path::Path;
/// 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'";
Expand Down Expand Up @@ -933,7 +960,7 @@ impl<'a> Drop for Transaction<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::vector::Geometry;
use crate::vector::{Geometry, LayerAccess};
use tempfile::TempPath;

macro_rules! fixture {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -7,6 +7,7 @@
//! ```
//! use std::path::Path;
//! use gdal::Dataset;
//! use gdal::vector::LayerAccess;
//!
//! let dataset = Dataset::open(Path::new("fixtures/roads.geojson")).unwrap();
//! let mut layer = dataset.layer(0).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/vector/defn.rs
@@ -1,6 +1,6 @@
use crate::spatial_ref::SpatialRef;
use crate::utils::{_last_null_pointer_err, _string};
use crate::vector::layer::Layer;
use crate::vector::LayerAccess;
use gdal_sys::{
self, OGRFeatureDefnH, OGRFieldDefnH, OGRFieldType, OGRGeomFieldDefnH, OGRwkbGeometryType,
};
Expand Down Expand Up @@ -55,7 +55,7 @@ impl Defn {
}
}

pub fn from_layer(lyr: &Layer) -> Defn {
pub fn from_layer<L: LayerAccess>(lyr: &L) -> Defn {
let c_defn = unsafe { gdal_sys::OGR_L_GetLayerDefn(lyr.c_layer()) };
Defn { c_defn }
}
Expand Down
5 changes: 2 additions & 3 deletions src/vector/feature.rs
@@ -1,7 +1,6 @@
use crate::utils::{_last_null_pointer_err, _string, _string_array};
use crate::vector::geometry::Geometry;
use crate::vector::layer::Layer;
use crate::vector::Defn;
use crate::vector::{Defn, LayerAccess};
use gdal_sys::{self, OGRErr, OGRFeatureH, OGRFieldType};
use libc::c_longlong;
use libc::{c_double, c_int};
Expand Down Expand Up @@ -487,7 +486,7 @@ impl<'a> Feature<'a> {
Ok(&self.geometry[idx])
}

pub fn create(&self, lyr: &Layer) -> Result<()> {
pub fn create<L: LayerAccess>(&self, lyr: &L) -> Result<()> {
let rv = unsafe { gdal_sys::OGR_L_CreateFeature(lyr.c_layer(), self.c_feature) };
if rv != OGRErr::OGRERR_NONE {
return Err(GdalError::OgrError {
Expand Down