#gis #diesel #postgis

postgis_diesel

An extension for Diesel framework to support PostGIS geometry datatype

17 releases (stable)

new 3.0.0 May 4, 2025
2.5.0 Mar 15, 2025
2.4.1 Jul 21, 2024
2.3.0 Jan 7, 2024
0.1.0 Feb 21, 2021

#9 in Geospatial

Download history 3872/week @ 2025-01-15 4386/week @ 2025-01-22 4596/week @ 2025-01-29 6345/week @ 2025-02-05 5276/week @ 2025-02-12 4988/week @ 2025-02-19 5490/week @ 2025-02-26 3780/week @ 2025-03-05 4070/week @ 2025-03-12 4281/week @ 2025-03-19 3660/week @ 2025-03-26 3514/week @ 2025-04-02 3436/week @ 2025-04-09 3765/week @ 2025-04-16 3854/week @ 2025-04-23 3744/week @ 2025-04-30

15,490 downloads per month

MIT license

135KB
3.5K SLoC

PostGIS Diesel

Extension for Diesel framework to support PostGIS types. It provides support for both the postgres and, optionally, the sqlite backends. While the former is enabled by default with the postgres feature, the latter requires the sqlite feature to be enabled.

Example of Usage

To ensure that the Geometry type is in scope, read this guide and add postgis_diesel::sql_types::* to the import_types key in your diesel.toml file.

Assume that the table is defined like this:

CREATE EXTENSION IF NOT EXISTS postgis;
CREATE TABLE geometry_samples
(
    id         SERIAL                    PRIMARY KEY,
    point      geometry(Point,4326)      NOT NULL,
    linestring geometry(Linestring,4326) NOT NULL
);

Or, if you are using sqlite, like this:

CREATE TABLE geometry_samples
(
    id         SERIAL                    PRIMARY KEY,
    point      BLOB                      NOT NULL,
    linestring BLOB                      NOT NULL
);

Then Rust code (with either backends) may look like this:

#[macro_use]
extern crate diesel;

use postgis_diesel::operators::*;
use postgis_diesel::types::*;

#[derive(Insertable)]
#[diesel(table_name = geometry_samples)]
struct NewGeometrySample {
    point: Point,
    linestring: LineString<Point>,
}

#[derive(Queryable)]
struct GeometrySample {
    id: i32,
    point: Point,
    linestring: LineString<Point>,
}

table! {
    use postgis_diesel::sql_types::*;
    use diesel::sql_types::*;
    geometry_samples (id) {
        id -> Int4,
        point -> Geometry,
        linestring -> Geometry,
    }
}

See integration test for more complete example.

How to Remove Automatically Generated Types From Schema

  1. Generate schema file with diesel print-schema > src/full_schema.rs.
  2. Remove not required SQL types from it and save to src/schema.rs.
  3. Run diff -U6 src/full_schema.rs src/schema.rs > src/schema.patch.
  4. Add patch_file = "src/schema.patch" to diesel.toml.
  5. Remove src/full_schema.rs, check that diesel print-schema > src/schema.rs will not add Geometry type.

Example of patch file:

@@ -1,12 +1,9 @@
 // @generated automatically by Diesel CLI.
 
 pub mod sql_types {
-    #[derive(diesel::query_builder::QueryId, diesel::sql_types::SqlType)]
-    #[diesel(postgres_type(name = "geometry"))]
-    pub struct Geometry;
 
     #[derive(diesel::query_builder::QueryId, diesel::sql_types::SqlType)]
     #[diesel(postgres_type(name = "intensity"))]
     pub struct Intensity;
 
     #[derive(diesel::query_builder::QueryId, diesel::sql_types::SqlType)]
@@ -52,13 +49,12 @@
 
 diesel::table! {
     use diesel::sql_types::*;
     use postgis_diesel::sql_types::*;
     use super::sql_types::Intensity;
     use super::sql_types::Triggermethod;
-    use super::sql_types::Geometry;
 
     laps (activity_id, started_at, manual_track) {
         activity_id -> Uuid,
         started_at -> Timestamptz,
         total_time_seconds -> Float8,
         distance_meters -> Float8,

How to Run Tests

  1. Start Postgis DB
docker compose up
  1. Run tests
cargo test

Dependencies

~0.1–4MB
~79K SLoC