georust / geozero

Zero-Copy reading and writing of geospatial data.
Apache License 2.0
352 stars 37 forks source link

derive FromSqlRow for Ewkb #93

Closed sunny-g closed 1 year ago

sunny-g commented 1 year ago

Am new to both geozero and diesel, but for the queries I was trying to write, I needed to add this derive to execute the following (truncated) code:

The diesel errors were kind of inscrutable, but from what I could surmise, the general issue is that while st_union_agg (which is a SQL function I defined to wrap the aggregate version of PostGIS' ST_Union) returns a geozero::postgis::diesel::sql_types::Geometry, an Ewkb cannot be obtained from the select statement without explicit conversion, selecting the Geometry column itself, or without this trait implementation.

Sorry I can't provide a better explanation.

#[derive(Queryable)]
struct CollectionDiff {
    pub start: DateTime<Utc>,
    pub end: DateTime<Utc>,
    pub updated: DateTime<Utc>,
    pub footprint: Ewkb,
}

stac::table
    .select((
        min(stac::observed).assume_not_null(),
        max(stac::observed).assume_not_null(),
        max(stac::updated).assume_not_null(),
        st_union_agg(stac::geometry),
    ))
    .get_result::<CollectionDiff>(&mut conn)?;
michaelkirk commented 1 year ago

I don't use diesel, but I'm a little surprised that you'd build an Ewkb from an entire row. I'd expect just a single field of the row to be the ewkb.

pka commented 1 year ago

@myaple ?

myaple commented 1 year ago

Sorry, complete oversight on my implementation. FromSqlRow allows you to use the FromSql implementation to convert a single value in a row (tuple) selection into the type. So, because all of my tests for this just used a struct instead of explicitly calling select on a tuple like @sunny-g did, I forgot to add the derive macro. My apologies @pka and thank you @sunny-g for putting this in.

pka commented 1 year ago

Thanks @sunny-g (and @myaple for checking)!

sunny-g commented 1 year ago

Thank you both! @pka @myaple

sunny-g commented 1 year ago

@michaelkirk I'm not, this example is getting an Ewkb from the union of the individual column.