bun icon indicating copy to clipboard operation
bun copied to clipboard

how to use geometry type in postgis

Open boojongmin opened this issue 2 years ago • 9 comments

hi, I have a question when using postgis.

how to model mapping geometry type?

thanks you :)

boojongmin avatar Apr 23 '22 02:04 boojongmin

+1

imraan-go avatar Apr 23 '22 06:04 imraan-go

pgtype support some geometric types, e.g. https://pkg.go.dev/github.com/jackc/pgtype#Point

vmihailenco avatar Apr 23 '22 11:04 vmihailenco

thank you for your answer :)

boojongmin-gm avatar Apr 28 '22 04:04 boojongmin-gm

@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-gm avatar Apr 28 '22 05:04 boojongmin-gm

@boojongmin can you try

Point       pgtype.Point `bun:"type:geometry::point"`

If that does not work, please provide a program that reproduces the problem.

vmihailenco avatar Apr 28 '22 09:04 vmihailenco

@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},
	}
}

max107 avatar May 04 '22 18:05 max107

@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)

max107 avatar May 04 '22 18:05 max107

@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.

boojongmin-gm avatar May 19 '22 09:05 boojongmin-gm

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

tingold avatar Aug 23 '22 22:08 tingold