bun
bun copied to clipboard
how to use geometry type in postgis
hi, I have a question when using postgis.
how to model mapping geometry
type?
thanks you :)
+1
pgtype support some geometric types, e.g. https://pkg.go.dev/github.com/jackc/pgtype#Point
thank you for your answer :)
@vmihailenco
Point pgtype.Point
I use point type but that is not working without cast(geometry::point)
how to use pgtype.Point type on gemetry type when insert sql?
thantk you
@boojongmin can you try
Point pgtype.Point `bun:"type:geometry::point"`
If that does not work, please provide a program that reproduces the problem.
@boojongmin-gm
how to use pgtype.Point type on gemetry type when insert sql?
you can use paulmach/orb
package.
package geotype
import (
"database/sql/driver"
"github.com/paulmach/orb"
"github.com/paulmach/orb/encoding/wkt"
)
type OrbGeometry struct {
orb.Geometry
}
var _ driver.Valuer = (*OrbGeometry)(nil)
func (tm *OrbGeometry) Value() (driver.Value, error) {
if tm.Geometry == nil {
return nil, nil
}
return wkt.MarshalString(tm.Geometry), nil
}
func NewPoint(lng, lat float64) OrbGeometry {
return OrbGeometry{
Geometry: orb.Point{lng, lat},
}
}
@vmihailenco
Schema creation (db.NewCreateTable().Model(m).Exec(ctx)
) is not work with ::point
.
Example
`bun:"geom,notnull,type:geometry(Point, 4326)::point" json:"-"`
Error
ERROR: syntax error at or near "::" (SQLSTATE=42601)
@vmihailenco
it is not working for me my code and error message is below.
type Building struct {
bun.BaseModel `bun:"table:tt,alias:t"`
Id int64 `bun:",pk"`
Point pgtype.Point `bun:"type:geometry::point"`
}
err := Db.NewSelect().
Column("*").
Model(r).
Where("id = ?", id).
Scan(ctx)
SELECT * FROM "tt" AS "p" WHERE (id = 1) *fmt.wrapError: sql: Scan error on column index 6, name "point": invalid format for point
error: sql: Scan error on column index 6, name "point": invalid format for point
would you check this?
thank you.
I've been using PostGIS+Bun+Orb for a while and just broke it into a separate project --> https://github.com/tingold/bunpostgis let me know if this helps