Using Orb and Custom Value Scanning and Marshalling
Inserting and retrieving PostGIS geometry types with Gorm can be challenging.為了解決此問題,用戶可以利用ORB庫並實現自定義SCAN()和Value()方法,以在GO類型和GORM期望的著名二進制(WKB)格式之間轉換。以下是一個示例:
byte)) 如果err! = nil { 返回錯誤 } g = gt。 (*ewkbgeompoint) 返回無 } func(g ewkbgeompoint)value()(驅動程序,value,錯誤){ b:= geom.point(g) bp:=& b ewkbpt:= ewkb.point {point:bp.setsrid(4326)} 返回ewkbpt.value() }type EWKBGeomPoint geom.Point func (g *EWKBGeomPoint) Scan(input interface{}) error { gt, err := ewkb.Unmarshal(input.([]byte)) if err != nil { return err } g = gt.(*EWKBGeomPoint) return nil } func (g EWKBGeomPoint) Value() (driver.Value, error) { b := geom.Point(g) bp := &b ewkbPt := ewkb.Point{Point: bp.SetSRID(4326)} return ewkbPt.Value() }tobs Indup canducation offorment
可以使用自定義列,可以將其遷移到自定義列中,可以自定義列列,該列是自定義的cription commentry colummentry列,類型:
*gormigrate.migration { { id:“ init”, 遷移:func(tx *gorm.db)錯誤{ 返回tx.createtable( 桌子..., )。錯誤 },, },, { id:“ tracks_except_geom”, 遷移:func(tx *gorm.db)錯誤{ 返回db.automigrate(track {})。錯誤 },, },, })。遷移()This migration ensures the geometry column is created before the rest of the table structure is set up.
Example Usage
err = db.Exec(`CREATE TABLE IF NOT EXISTS tracks ( id SERIAL PRIMARY KEY, geom geometry(POINT, 4326) NOT NULL );`).Error if err != nil { return err } err = gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{ { ID: "init", Migrate: func(tx *gorm.DB) error { return tx.CreateTable( Tables..., ).Error }, }, { ID: "tracks_except_geom", Migrate: func(tx *gorm.DB) error { return db.AutoMigrate(Track{}).Error }, }, }).Migrate()Once the custom Scan(), Value(), and migration steps are implemented, using the geometry type in a Gorm model becomes straightforward:type Track struct { Gorm.Model 幾何點ewkbgeompoint` gorm:“列:geom”` } [&& && && && && && &&華
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3