Gpkg

Struct Gpkg 

Source
pub struct Gpkg { /* private fields */ }
Expand description

GeoPackage connection wrapper for reading (and later writing) layers.

Implementations§

Source§

impl Gpkg

Source

pub fn open_read_only<P: AsRef<Path>>(path: P) -> Result<Self>

Open a GeoPackage in read-only mode.

Example:

use rusqlite_gpkg::Gpkg;

let gpkg = Gpkg::open_read_only("data/example.gpkg")?;
Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

Open a new or existing GeoPackage in read-write mode.

Example:

use rusqlite_gpkg::Gpkg;

let gpkg = Gpkg::open("data/example.gpkg")?;
Source

pub fn open_in_memory() -> Result<Self>

Create a new GeoPackage in memory.

Example:

use rusqlite_gpkg::Gpkg;

let gpkg = Gpkg::open_in_memory()?;
Source

pub fn register_srs( &self, srs_name: &str, srs_id: i32, organization: &str, organization_coordsys_id: i32, definition: &str, description: &str, ) -> Result<()>

Expert-only: register a spatial reference system in gpkg_spatial_ref_sys.

GeoPackage layers must reference a valid srs_id that already exists in gpkg_spatial_ref_sys. The GeoPackage spec requires a full SRS definition (notably the WKT definition and descriptive metadata). In practice, this data is often sourced from an external authority like EPSG, but this crate does not bundle or generate that catalog. As a result, callers must insert SRS entries themselves before creating layers, which is why this low-level helper exists.

This method performs a direct insert with all required columns and does no validation of the WKT or authority fields. Use only if you understand the GeoPackage SRS requirements and have authoritative metadata.

Example: register EPSG:3857 (Web Mercator / Pseudo-Mercator).

let gpkg = Gpkg::open_in_memory().expect("new gpkg");
let definition = r#"PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"],AUTHORITY["EPSG","3857"]]"#;
gpkg.register_srs(
    "WGS 84 / Pseudo-Mercator",
    3857,
    "EPSG",
    3857,
    definition,
    "Web Mercator / Pseudo-Mercator (EPSG:3857)",
).expect("register srs");
Source

pub fn list_layers(&self) -> Result<Vec<String>>

List the names of the layers.

Example:

use rusqlite_gpkg::Gpkg;

let gpkg = Gpkg::open_read_only("data/example.gpkg")?;
let layers = gpkg.list_layers()?;
Source

pub fn get_layer(&self, layer_name: &str) -> Result<GpkgLayer>

Load a layer definition and metadata by name.

Example:

use rusqlite_gpkg::Gpkg;

let gpkg = Gpkg::open_read_only("data/example.gpkg")?;
let layer = gpkg.get_layer("points")?;
Source

pub fn create_layer( &self, layer_name: &str, geometry_column: &str, geometry_type: GeometryType, geometry_dimension: Dimension, srs_id: u32, other_column_specs: &[ColumnSpec], ) -> Result<GpkgLayer>

Example:

use geo_types::Point;
use rusqlite_gpkg::{ColumnSpec, ColumnType, Gpkg, params};

let gpkg = Gpkg::open_in_memory()?;
let columns = vec![ColumnSpec {
    name: "name".to_string(),
    column_type: ColumnType::Varchar,
}];
let layer = gpkg.create_layer(
    "points",
    "geom",
    wkb::reader::GeometryType::Point,
    wkb::reader::Dimension::Xy,
    4326,
    &columns,
)?;
layer.insert(Point::new(1.0, 2.0), params!["alpha"])?;
Source

pub fn delete_layer(&self, layer_name: &str) -> Result<()>

Delete a layer.

Example:

use rusqlite_gpkg::Gpkg;

let gpkg = Gpkg::open("data/example.gpkg")?;
gpkg.delete_layer("points")?;
Source

pub fn to_bytes(&self) -> Result<Vec<u8>>

Dump the GeoPackage data to Vec<u8>.

This is intended for environments without filesystem access (for example, running in a web browser). You can serialize an in-memory GeoPackage and move the bytes over the wire or store them in browser storage.

Example:

use rusqlite_gpkg::Gpkg;

let gpkg = Gpkg::open_in_memory()?;
let bytes = gpkg.to_bytes()?;
Source

pub fn from_bytes<D: AsRef<[u8]>>(data: D) -> Result<Self>

Load the GeoPackage data from a dump.

This is intended for environments without filesystem access (for example, running in a web browser). Provide the bytes from Gpkg::to_bytes() to recreate an in-memory GeoPackage.

Example:

use rusqlite_gpkg::Gpkg;

let gpkg = Gpkg::open_in_memory()?;
let bytes = gpkg.to_bytes()?;
let restored = Gpkg::from_bytes(&bytes)?;

Trait Implementations§

Source§

impl Debug for Gpkg

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Gpkg

§

impl !RefUnwindSafe for Gpkg

§

impl !Send for Gpkg

§

impl !Sync for Gpkg

§

impl Unpin for Gpkg

§

impl !UnwindSafe for Gpkg

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.