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

Generic geometry traits for EWKB/PostGIS output and support for Tiny WKB geometries #9

Merged
merged 44 commits into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6f3fd8c
Add rust-geo dependency
pka Sep 16, 2016
c8bdfb3
EWKB/PostGIS implemention for Point and LineString
pka Sep 16, 2016
266fb81
Implement ToGeo trait
pka Sep 17, 2016
d834d65
TWKB read support [WIP]
pka Sep 25, 2016
29dfe5c
Implement postgres::types::FromSql for TwkbPoint
pka Sep 26, 2016
7de3ada
Use TwkbPointType trait analogous to EWKB
pka Sep 26, 2016
36cb7fb
TwkbLineString implementation
pka Sep 26, 2016
7e61917
Move TWKB reader helper functions to module level
pka Sep 26, 2016
ff5216a
Common Point trait
pka Sep 27, 2016
2cc3909
Introduce generic geometry traits with implementation for TWKB geomet…
pka Sep 28, 2016
a28ba02
PartialEq for TWKB structs
pka Sep 29, 2016
05916cd
Native EWKB geometry structs
pka Sep 29, 2016
47d9acc
Improved iterator traits
pka Oct 11, 2016
589f645
Duplicate write_ewkb method removed
pka Oct 11, 2016
3b1316b
Extract EWKB read and write traits with implementation for TWKB
pka Oct 12, 2016
e0cfb85
Docu update
pka Oct 13, 2016
42a3c91
EWKB read tests from hex string
pka Oct 13, 2016
c0436d4
Class renaming / module export reorganization
pka Oct 14, 2016
df3ea6c
row.get_opt example
pka Oct 14, 2016
470c947
Support for XYZM point types
pka Oct 14, 2016
cd571f4
Store PointType in Ewkb structs
pka Oct 16, 2016
9477706
Set SRID of points in line when reading from WKB
pka Oct 16, 2016
3993dc9
EWKB Polygon read implementation
pka Oct 17, 2016
e059515
EWKB MultiLineString read implementation
pka Oct 17, 2016
169cbbb
Type aliases for ZM variants
pka Oct 18, 2016
89a21de
Remove rust-geo dependecy for now
pka Oct 18, 2016
e804d8e
Implement from_sql/to_sql for all point types
pka Oct 18, 2016
5e90f06
ewkb::LineString implementation as macro
pka Oct 19, 2016
ba5b50a
AsEwkbMultiLineString implementation
pka Oct 19, 2016
ff7fc5b
ewkb::MultiLineString implementation as macro
pka Oct 20, 2016
a9b06b6
Move EWKB adapter defintions into ewkb macros
pka Oct 20, 2016
b153227
Split macros
pka Oct 20, 2016
d951e4d
polygon implementation as macro
pka Oct 20, 2016
6a1025c
multipoint implementation
pka Oct 20, 2016
5ebaf7f
impl EwkbWrite for EwkbMultiPolygon<'a, P, I, L, K, T, J> (phew)
pka Oct 21, 2016
3677a53
Implement from_sql/to_sql for remaining ewkb types
pka Oct 21, 2016
c62f6db
twkb::Polygon implementation
pka Oct 21, 2016
63279a3
twkb multi-types implementation
pka Oct 21, 2016
be2c718
TWKB output for remaining types
pka Oct 21, 2016
3f8cbe2
Move PointType into ewkb module
pka Oct 23, 2016
f31d7b1
Add copyright headers
pka Oct 23, 2016
71ea7ff
Support for Geometry and GeometryCollection type
pka Oct 23, 2016
6b30bc7
Remove original implementation
pka Oct 23, 2016
a0e46b5
Make Point methods public
pka Oct 24, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,65 @@ An extension to rust-postgres, adds support for PostGIS.

- PostGIS type helper
- GCJ02 support (used offically in Mainland China)
- Type-safe SRID support
- Tiny WKB (TWKB) support

## HowTo
## Usage

```rust
use postgres::{Connection, SslMode};
use postgis::{Point, LineString, WGS84};
use postgis::ewkb;
use postgis::LineString;

fn main() {
// conn ....
let stmt = conn.prepare("SELECT * FROM busline").unwrap();
for row in stmt.query(&[]).unwrap() {
println!(">>>>>> {}", row.get::<_, LineString<Point>>("route"));
for row in &conn.query("SELECT * FROM busline", &[]).unwrap() {
let route: ewkb::LineString = row.get("route");
let last_stop = route.points().last().unwrap();
let _ = conn.execute("INSERT INTO stops (stop) VALUES ($1)", &[&last_stop]);
}
}
```

Handling NULL values:
```rust
let route = row.get_opt::<_, Option<ewkb::LineString>>("route");
match route.unwrap() {
Ok(Some(geom)) => { println!("{:?}", geom) }
Ok(None) => { /* Handle NULL value */ }
Err(err) => { println!("Error: {}", err) }
}
```

## Writing other geometry types into PostGIS

rust-postgis supports writing geometry types into PostGIS which implement the following traits:

* `Point`, `LineString`, ...
* `AsEwkbPoint`, `AsEwkbLineString`, ...

See the TWKB implementation as an example.

An example for reading a TWKB geometry and writing it back as EWKB:

```rust
use postgis::twkb;
use postgis::LineString;

for row in &conn.query("SELECT ST_AsTWKB(route) FROM busline", &[]).unwrap() {
let route: twkb::LineString = row.get(0);
let last_stop = route.points().last().unwrap();
let _ = conn.execute("INSERT INTO stops (stop) VALUES ($1)", &[&last_stop.as_ewkb()]);
}
```


## Unit tests

Unit tests which need a PostgreSQL connection are ignored by default.
To run the database tests, declare the connection in an environment variable `DBCONN`. Example:

export DBCONN=postgresql://user@localhost/testdb

Run the tests with

cargo test -- --ignored
32 changes: 32 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// FileName : error.rs
// Author : ShuYu Wang <[email protected]>
// Created : Wed May 27 01:45:41 2015 by ShuYu Wang
// Copyright : Feather Workshop (c) 2015
// Description : PostGIS helper
// Time-stamp: <2015-06-13 19:21:08 andelf>

use std;
use std::fmt;

#[derive(Debug, )]
pub enum Error {
Read(String),
Write(String),
Other(String)
}

impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{:?}", self)
}
}

impl std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Read(_) => "postgis error while reading",
Error::Write(_) => "postgis error while writing",
Error::Other(_) => "postgis unknown error"
}
}
}
Loading