paulmach / orb

Types and utilities for working with 2d geometry in Golang
MIT License
886 stars 103 forks source link

Can we use wkb.Scanner with orb.Geometry type ? #104

Closed bibich closed 2 years ago

bibich commented 2 years ago

Hi,

In my region table I have a geometry column geom that could be of different types : polygon, multipolygon I try to use the wkb.Scanner with a orb.Geometry var but it fails, when I use a orb.Polygon it's ok. Is it possible to scan a column as orb.Geometry or should we provide the right type. And then how to handle multi type columns ?

(I'm pretty new to go programming)

my code :

func (q *RegionQueries) GetRegion(code string) (models.Region, error) {
    region := models.Region{}
    query := `SELECT code, label, label_uc, ST_AsBinary(geom) FROM region WHERE code = $1`

    var geom orb.Geometry
    // var geom orb.Polygon // ok
    s := wkb.Scanner(&geom)
    q.QueryRow(
        context.Background(),
        query,
        code,
    ).Scan(&region.Code, &region.Label, &region.LabelUC, s)

    if s.Valid {
        region.Geom = s.Geometry
    } else {
        region.Geom = nil
    }

    // ...
}
paulmach commented 2 years ago

I think if you do s := wkb.Scanner(nil) it'll work as you expect

bibich commented 2 years ago

Yes. you're right. Thank you.