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

Support geo types with georust/geo #245

Merged
merged 3 commits into from
Mar 12, 2017
Merged

Support geo types with georust/geo #245

merged 3 commits into from
Mar 12, 2017

Conversation

kestred
Copy link
Contributor

@kestred kestred commented Mar 10, 2017

This pull request implements part of #244. Supporting the default postgres geographic types with https://github.com/georust/rust-geo.

The types LINE and LSEG haven't been implemented yet because there is no obvious mapping in the current implementation of the geo crate. I hope to discuss this with the maintainer of that crate.

@kestred
Copy link
Contributor Author

kestred commented Mar 10, 2017

While implementing this, I had another idea; either an alternative or an additional implementation for geo types.

The geo types could be supported via tuples (eg. impl ToSql for (f64, f64)). This would allow queries to be written like:

let lat: f64 = 40.018782;
let lng: f64 = -105.278201;
db.execute("INSERT INTO places (loc) VALUES ($1)", &[(&lat, &lng)])

The type mappings using tuples COULD be:

Rust Type Postgres Type
(f64, f64) POINT
(f64, f64, f64) LINE
((f64, f64), (f64, f64)) LSEG, BOX
[(f64, f64), ...] OR Vec<(f64, f64)> PATH
[(f64, f64), ...] OR Vec<(f64, f64)> POLYGON

@sfackler
Copy link
Owner

Thanks! Could you add some tests? See here for reference: https://github.com/sfackler/rust-postgres/tree/master/postgres/tests/types

return Err("invalid message length".into());
}

// let _ = types::bool_from_sql(&raw[0..1])?; // is path open or closed
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it going to be problematic that we're discarding this information?

Copy link
Contributor Author

@kestred kestred Mar 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not cause a database error to occur.

To recover this information, a user would need to SELECT the value of isopen(mypath).

The more interesting part of the issue is with ToSql where there are two different ways to create a path: an open path path '[(1,2),(3,4)]' and a closed path path '((1,2),(3,4))'. The PR handles this by always inserting an open path. To insert a closed path, you would need to write pclose($1) and let Postgres flip that bit during statement evaluation. This behavior is documented in the README.

I opted for that behavior because:

@sfackler sfackler merged commit 33d45db into sfackler:master Mar 12, 2017
@sfackler
Copy link
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants