pub struct Gpkg { /* private fields */ }Expand description
GeoPackage connection wrapper for reading (and later writing) layers.
Implementations§
Source§impl Gpkg
impl Gpkg
Sourcepub fn open_read_only<P: AsRef<Path>>(path: P) -> Result<Self>
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")?;Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
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")?;Sourcepub fn open_in_memory() -> Result<Self>
pub fn open_in_memory() -> Result<Self>
Create a new GeoPackage in memory.
Example:
use rusqlite_gpkg::Gpkg;
let gpkg = Gpkg::open_in_memory()?;Sourcepub fn register_srs(
&self,
srs_name: &str,
srs_id: i32,
organization: &str,
organization_coordsys_id: i32,
definition: &str,
description: &str,
) -> Result<()>
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");Sourcepub fn list_layers(&self) -> Result<Vec<String>>
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()?;Sourcepub fn get_layer(&self, layer_name: &str) -> Result<GpkgLayer>
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")?;Sourcepub 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>
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"])?;Sourcepub fn delete_layer(&self, layer_name: &str) -> Result<()>
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")?;Sourcepub fn to_bytes(&self) -> Result<Vec<u8>>
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()?;Sourcepub fn from_bytes<D: AsRef<[u8]>>(data: D) -> Result<Self>
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)?;