1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! PostGIS geometry type encoding/decoding.
//!
//! All geometry types implementing [GeozeroGeometry](crate::GeozeroGeometry) can be encoded as PostGIS EWKB geometry using [wkb::Encode](crate::wkb::Encode).
//!
//! Geometry types implementing [FromWkb](crate::wkb::FromWkb) can be decoded from PostGIS geometries using [wkb::Decode](crate::wkb::Decode).
/// PostGIS geometry type encoding/decoding for rust-postgres. Requires the `with-postgis-postgres` feature.
///
/// # PostGIS usage example with rust-postgres
///
/// Select and insert geo-types geometries:
/// ```
/// use geozero::wkb;
/// use postgres::{Client, NoTls};
///
/// # fn rust_geo_query() -> Result<(), postgres::error::Error> {
/// let mut client = Client::connect(&std::env::var("DATABASE_URL").unwrap(), NoTls)?;
///
/// let row = client.query_one(
/// "SELECT 'SRID=4326;POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))'::geometry",
/// &[],
/// )?;
///
/// let value: wkb::Decode<geo_types::Geometry<f64>> = row.get(0);
/// if let Some(geo_types::Geometry::Polygon(poly)) = value.geometry {
/// assert_eq!(
/// *poly.exterior(),
/// vec![(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)].into()
/// );
/// }
///
/// // Insert geometry
/// let geom: geo_types::Geometry<f64> = geo::Point::new(1.0, 3.0).into();
/// let _ = client.execute(
/// "INSERT INTO point2d (datetimefield,geom) VALUES(now(),ST_SetSRID($1,4326))",
/// &[&wkb::Encode(geom)],
/// );
/// # Ok(())
/// # }
///```
/// PostGIS geometry type encoding/decoding for SQLx. Requires the `with-postgis-sqlx` feature.
///
/// # PostGIS usage example with SQLx
///
/// Select and insert geo-types geometries with SQLx:
/// ```
/// use geozero::wkb;
/// use sqlx::postgres::PgPoolOptions;
/// # use std::env;
///
/// # async fn rust_geo_query() -> Result<(), sqlx::Error> {
/// let pool = PgPoolOptions::new()
/// .max_connections(5)
/// .connect(&env::var("DATABASE_URL").unwrap())
/// .await?;
///
/// let row: (wkb::Decode<geo_types::Geometry<f64>>,) =
/// sqlx::query_as("SELECT 'SRID=4326;POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))'::geometry")
/// .fetch_one(&pool)
/// .await?;
/// let value = row.0;
/// if let Some(geo_types::Geometry::Polygon(poly)) = value.geometry {
/// assert_eq!(
/// *poly.exterior(),
/// vec![(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)].into()
/// );
/// }
///
/// // Insert geometry
/// let geom: geo_types::Geometry<f64> = geo::Point::new(10.0, 20.0).into();
/// let _ = sqlx::query(
/// "INSERT INTO point2d (datetimefield,geom) VALUES(now(),ST_SetSRID($1,4326))",
/// )
/// .bind(wkb::Encode(geom))
/// .execute(&pool)
/// .await?;
/// # Ok(())
/// # }
/// ```
/// Postgis geometry type encoding for Diesel. Requires the `with-postgis-diesel` feature.
///
/// # PostGIS usage example with Diesel
///
/// Declare model and select Ewkb types directly with GeoZero and Diesel
///
/// ```
/// use diesel::pg::PgConnection;
/// use diesel::{Connection, QueryDsl, RunQueryDsl};
/// use diesel::prelude::*;
///
/// use geozero::wkb::Ewkb;
///
/// diesel::table! {
/// use diesel::sql_types::*;
/// use geozero::postgis::diesel::sql_types::*;
///
/// geometries (name) {
/// name -> Varchar,
/// geom -> Nullable<Geometry>,
/// }
/// }
///
/// #[derive(Queryable, Debug, Insertable)]
/// #[diesel(table_name = geometries)]
/// pub struct Geom {
/// pub name: String,
/// pub geom: Option<Ewkb<Vec<u8>>>,
/// }
///
/// pub fn establish_connection() -> PgConnection {
/// let database_url = std::env::var("DATABASE_URL").expect("Unable to find database url.");
/// PgConnection::establish(&database_url).unwrap()
/// }
///
/// # async fn rust_geo_query() -> Result<(), diesel::result::Error> {
/// let conn = &mut establish_connection();
///
/// let wkb = Ewkb(vec![
/// 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 64, 0, 0, 0, 0, 0, 0, 52, 192,
/// ]);
///
/// let insert_geometry = Geom {
/// name: "GeoZeroTest".to_string(),
/// geom: Some(wkb),
/// };
///
/// let inserted: Geom = diesel::insert_into(geometries::table)
/// .values(&insert_geometry)
/// .get_result(conn)
/// .expect("Unable to insert into postgis");
///
/// let geometry_vec: Vec<Geom> = geometries::dsl::geometries
/// .limit(10)
/// .load::<Geom>(conn)
/// .expect("Error loading geometries");
/// # Ok(())
/// # }
/// ```